Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #317 from akkie/providers
Browse files Browse the repository at this point in the history
Provider API enhancements
  • Loading branch information
akkie committed Apr 1, 2015
2 parents b625e0e + 72f082f commit 7ede3ce
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 76 deletions.
2 changes: 1 addition & 1 deletion project/BuildSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object BasicSettings extends AutoPlugin {

override def projectSettings = Seq(
organization := "com.mohiva",
version := "2.0",
version := "2.1-SNAPSHOT",
resolvers ++= Dependencies.resolvers,
scalaVersion := Dependencies.Versions.scalaVersion,
crossScalaVersions := Dependencies.Versions.crossScala,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ object FakeAuthenticator {
* A fake environment implementation.
*
* @param identities A list of (login info -> identity) pairs to return inside a Silhouette action.
* @param providers The list of authentication providers.
* @param requestProviders The list of request providers.
* @param eventBus The event bus implementation.
* @tparam I The type of the identity.
* @tparam T The type of the authenticator.
*/
case class FakeEnvironment[I <: Identity, T <: Authenticator: TypeTag](
identities: Seq[(LoginInfo, I)],
providers: Map[String, Provider] = Map(),
requestProviders: Seq[RequestProvider] = Seq(),
eventBus: EventBus = EventBus())
extends Environment[I, T] {

Expand Down
10 changes: 5 additions & 5 deletions silhouette/app/com/mohiva/play/silhouette/api/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ trait Environment[I <: Identity, T <: Authenticator] {
def authenticatorService: AuthenticatorService[T]

/**
* Gets the list of authentication providers.
* Gets the list of request providers.
*
* @return The list of authentication providers.
* @return The list of request providers.
*/
def providers: Map[String, Provider]
def requestProviders: Seq[RequestProvider]

/**
* The event bus implementation.
Expand All @@ -58,11 +58,11 @@ object Environment {
def apply[I <: Identity, T <: Authenticator](
identityServiceImpl: IdentityService[I],
authenticatorServiceImpl: AuthenticatorService[T],
providersImpl: Map[String, Provider],
requestProvidersImpl: Seq[RequestProvider],
eventBusImpl: EventBus) = new Environment[I, T] {
val identityService = identityServiceImpl
val authenticatorService = authenticatorServiceImpl
val providers = providersImpl
val requestProviders = requestProvidersImpl
val eventBus = eventBusImpl
}
}
14 changes: 1 addition & 13 deletions silhouette/app/com/mohiva/play/silhouette/api/Silhouette.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,7 @@ trait Silhouette[I <: Identity, A <: Authenticator] extends Controller with Logg
}
}

auth(requestProviders)
}

/**
* Gets the list of request providers.
*
* @return The list of request providers.
*/
private def requestProviders: Seq[RequestProvider] = {
env.providers.map {
case (id, provider: RequestProvider) => Some(provider)
case _ => None
}.flatten[RequestProvider].toSeq
auth(env.requestProviders)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class OAuth1Provider(
httpLayer: HTTPLayer,
service: OAuth1Service,
tokenSecretProvider: OAuth1TokenSecretProvider,
settings: OAuth1Settings)
val settings: OAuth1Settings)
extends SocialProvider with Logger {

/**
Expand All @@ -65,6 +65,11 @@ abstract class OAuth1Provider(
*/
type A = OAuth1Info

/**
* The settings type.
*/
type Settings = OAuth1Settings

/**
* Starts the authentication process.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ object OAuth2Info {
* @param stateProvider The state provider implementation.
* @param settings The provider settings.
*/
abstract class OAuth2Provider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProvider, settings: OAuth2Settings)
abstract class OAuth2Provider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProvider, val settings: OAuth2Settings)
extends SocialProvider with Logger {

/**
* The type of the auth info.
*/
type A = OAuth2Info

/**
* The settings type.
*/
type Settings = OAuth2Settings

/**
* A list with headers to send to the API.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ import scala.concurrent.Future
* @param service The OpenID service implementation.
* @param settings The OpenID provider settings.
*/
abstract class OpenIDProvider(httpLayer: HTTPLayer, service: OpenIDService, settings: OpenIDSettings)
abstract class OpenIDProvider(httpLayer: HTTPLayer, service: OpenIDService, val settings: OpenIDSettings)
extends SocialProvider with Logger {

/**
* The type of the auth info.
*/
type A = OpenIDInfo

/**
* The settings type.
*/
type Settings = OpenIDSettings

/**
* Starts the authentication process.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import com.mohiva.play.silhouette.api.util.ExtractableRequest
import com.mohiva.play.silhouette.api.{ LoginInfo, Provider }
import com.mohiva.play.silhouette.impl.exceptions.ProfileRetrievalException
import com.mohiva.play.silhouette.impl.providers.SocialProfileBuilder._
import org.apache.commons.lang3.reflect.TypeUtils
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.{ RequestHeader, Result }

import scala.concurrent.Future
import scala.reflect.ClassTag

/**
* The base interface for all social providers.
Expand All @@ -37,6 +39,18 @@ trait SocialProvider extends Provider with SocialProfileBuilder {
*/
type A <: AuthInfo

/**
* The settings type.
*/
type Settings

/**
* Gets the provider settings.
*
* @return The provider settings.
*/
def settings: Settings

/**
* Authenticates the user and returns the auth information.
*
Expand Down Expand Up @@ -82,6 +96,32 @@ trait SocialProvider extends Provider with SocialProfileBuilder {
}
}

/**
* A registry that holds and provides access to all social provider implementations.
*
* @param providers The list of social providers.
*/
case class SocialProviderRegistry(providers: Seq[SocialProvider]) {

/**
* Gets a specific provider by its type.
*
* @tparam T The type of the provider.
* @return Some specific provider type or None if no provider for the given type could be found.
*/
def get[T: ClassTag]: Option[T] = {
providers.find(p => TypeUtils.isInstance(p, implicitly[ClassTag[T]].runtimeClass)).map(_.asInstanceOf[T])
}

/**
* Gets a specific provider by its ID.
*
* @param id The ID of the provider to return.
* @return Some social provider or None if no provider for the given ID could be found.
*/
def get(id: String): Option[SocialProvider] = providers.find(_.id == id)
}

/**
* The social profile contains all the data returned from the social providers after authentication.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ abstract class LinkedInProvider(
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ abstract class TwitterProvider(
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ abstract class XingProvider(
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ abstract class ClefProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProv
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ abstract class DropboxProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StateP
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ abstract class FacebookProvider(httpLayer: HTTPLayer, stateProvider: OAuth2State
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import scala.concurrent.Future
* @see https://developer.foursquare.com/overview/responses
* @see https://developer.foursquare.com/docs/explore
*/
abstract class FoursquareProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProvider, protected val settings: OAuth2Settings)
abstract class FoursquareProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProvider, settings: OAuth2Settings)
extends OAuth2Provider(httpLayer, stateProvider, settings) {

/**
Expand All @@ -49,9 +49,7 @@ abstract class FoursquareProvider(httpLayer: HTTPLayer, stateProvider: OAuth2Sta
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ abstract class GitHubProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StatePr
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ abstract class GoogleProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StatePr
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ abstract class InstagramProvider(httpLayer: HTTPLayer, stateProvider: OAuth2Stat
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ abstract class LinkedInProvider(httpLayer: HTTPLayer, stateProvider: OAuth2State
type Content = JsValue

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ abstract class VKProvider(httpLayer: HTTPLayer, stateProvider: OAuth2StateProvid
type Content = (JsValue, OAuth2Info)

/**
* Gets the provider ID.
*
* @return The provider ID.
* The provider ID.
*/
val id = ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ class SilhouetteSpec extends PlaySpecification with Mockito with JsonMatchers {
lazy val env = Environment[FakeIdentity, FakeAuthenticator](
mock[IdentityService[FakeIdentity]],
mock[AuthenticatorService[FakeAuthenticator]],
Map(),
Seq(),
new EventBus
)

Expand Down Expand Up @@ -861,18 +861,18 @@ class SilhouetteSpec extends PlaySpecification with Mockito with JsonMatchers {
/**
* A non request provider.
*/
lazy val nonRequestProvider = mock[Provider]
lazy val nonRequestProvider = mock[RequestProvider]

/**
* The Silhouette environment.
*/
override lazy val env = Environment[FakeIdentity, FakeAuthenticator](
mock[IdentityService[FakeIdentity]],
mock[AuthenticatorService[FakeAuthenticator]],
Map(
"token-request" -> tokenRequestProvider,
"basic-auth-request" -> basicAuthRequestProvider,
"non-request" -> nonRequestProvider
Seq(
tokenRequestProvider,
basicAuthRequestProvider,
nonRequestProvider
),
new EventBus
)
Expand Down
Loading

0 comments on commit 7ede3ce

Please sign in to comment.