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

Commit

Permalink
Alter the library methods to propagate an implicit ExecutionContext to
Browse files Browse the repository at this point in the history
solve issue #341.
  • Loading branch information
joaoraf committed May 13, 2015
1 parent 667ebb1 commit ddc150d
Show file tree
Hide file tree
Showing 89 changed files with 641 additions and 529 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import play.api.mvc.RequestHeader

import scala.collection.mutable
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.reflect.runtime.universe._

/**
Expand All @@ -53,7 +54,7 @@ class FakeIdentityService[I <: Identity](identities: (LoginInfo, I)*)
* @param loginInfo The login info to retrieve an identity.
* @return The retrieved identity or None if no identity could be retrieved for the given login info.
*/
def retrieve(loginInfo: LoginInfo): Future[Option[I]] = {
def retrieve(loginInfo: LoginInfo)(implicit ec: ExecutionContext): Future[Option[I]] = {
Future.successful(identities.find(_._1 == loginInfo).map(_._2))
}
}
Expand All @@ -76,7 +77,7 @@ class FakeAuthenticatorDAO[T <: StorableAuthenticator] extends AuthenticatorDAO[
* @param id The authenticator ID.
* @return The found authenticator or None if no authenticator could be found for the given ID.
*/
def find(id: String): Future[Option[T]] = {
def find(id: String)(implicit ec: ExecutionContext): Future[Option[T]] = {
Future.successful(data.get(id))
}

Expand All @@ -86,7 +87,7 @@ class FakeAuthenticatorDAO[T <: StorableAuthenticator] extends AuthenticatorDAO[
* @param authenticator The authenticator to add.
* @return The added authenticator.
*/
def add(authenticator: T): Future[T] = {
def add(authenticator: T)(implicit ec: ExecutionContext): Future[T] = {
data += (authenticator.id -> authenticator)
Future.successful(authenticator)
}
Expand All @@ -97,7 +98,7 @@ class FakeAuthenticatorDAO[T <: StorableAuthenticator] extends AuthenticatorDAO[
* @param authenticator The authenticator to update.
* @return The updated authenticator.
*/
def update(authenticator: T): Future[T] = {
def update(authenticator: T)(implicit ec: ExecutionContext): Future[T] = {
data += (authenticator.id -> authenticator)
Future.successful(authenticator)
}
Expand All @@ -108,7 +109,7 @@ class FakeAuthenticatorDAO[T <: StorableAuthenticator] extends AuthenticatorDAO[
* @param id The authenticator ID.
* @return An empty future.
*/
def remove(id: String): Future[Unit] = {
def remove(id: String)(implicit ec: ExecutionContext): Future[Unit] = {
data -= id
Future.successful(())
}
Expand Down Expand Up @@ -201,7 +202,7 @@ object FakeAuthenticator {
* @tparam T The type of the authenticator,
* @return A authenticator instance.
*/
def apply[T <: Authenticator](loginInfo: LoginInfo)(implicit env: Environment[_, T], requestHeader: RequestHeader): T = {
def apply[T <: Authenticator](loginInfo: LoginInfo)(implicit env: Environment[_, T], requestHeader: RequestHeader, ec: ExecutionContext): T = {
env.authenticatorService.create(loginInfo)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package com.mohiva.play.silhouette

import com.mohiva.play.silhouette.api._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.test.{ FakeHeaders, FakeRequest }

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.{ Await, Future }
import scala.language.{ implicitConversions, postfixOps }

Expand Down Expand Up @@ -53,8 +53,8 @@ package object test {
* @tparam T The type of the authenticator,
* @return A fake request.
*/
def withAuthenticator[T <: Authenticator](authenticator: T)(implicit env: Environment[_, T]): FakeRequest[A] = {
val rh = env.authenticatorService.init(authenticator)(f).map(v => env.authenticatorService.embed(v, f))
def withAuthenticator[T <: Authenticator](authenticator: T)(implicit env: Environment[_, T], ec: ExecutionContext): FakeRequest[A] = {
val rh = env.authenticatorService.init(authenticator)(f, ec).map(v => env.authenticatorService.embed(v, f))
new FakeRequest(
id = rh.id,
tags = rh.tags,
Expand All @@ -76,8 +76,8 @@ package object test {
* @tparam T The type of the authenticator,
* @return A fake request.
*/
def withAuthenticator[T <: Authenticator](loginInfo: LoginInfo)(implicit env: Environment[_, T]): FakeRequest[A] = {
withAuthenticator(FakeAuthenticator[T](loginInfo)(env, f))
def withAuthenticator[T <: Authenticator](loginInfo: LoginInfo)(implicit env: Environment[_, T], ec: ExecutionContext): FakeRequest[A] = {
withAuthenticator(FakeAuthenticator[T](loginInfo)(env, f, ec))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.mohiva.play.silhouette.impl.authenticators._
import org.specs2.matcher.JsonMatchers
import play.api.libs.json.Json
import play.api.test.{ FakeRequest, PlaySpecification, WithApplication }
import scala.concurrent.ExecutionContext.Implicits.global

/**
* Test case for the [[com.mohiva.play.silhouette.test]] helpers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package com.mohiva.play.silhouette.api
import play.api.i18n.Messages
import play.api.mvc.RequestHeader
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext

/**
* A trait to define Authorization objects that let you hook
Expand All @@ -40,7 +40,7 @@ trait Authorization[I <: Identity] {
* @param messages The messages for the current language.
* @return True if the user is authorized, false otherwise.
*/
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages): Future[Boolean]
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages, ec: ExecutionContext): Future[Boolean]
}

/**
Expand All @@ -61,7 +61,7 @@ object Authorization {
* @return The authorization.
*/
def unary_! : Authorization[I] = new Authorization[I] {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages): Future[Boolean] = {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages, ec: ExecutionContext): Future[Boolean] = {
self.isAuthorized(identity).map(x => !x)
}
}
Expand All @@ -73,7 +73,7 @@ object Authorization {
* @return The authorization.
*/
def &&(authorization: Authorization[I]): Authorization[I] = new Authorization[I] {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages): Future[Boolean] = {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages, ec: ExecutionContext): Future[Boolean] = {
val leftF = self.isAuthorized(identity)
val rightF = authorization.isAuthorized(identity)
for {
Expand All @@ -90,7 +90,7 @@ object Authorization {
* @return The authorization.
*/
def ||(authorization: Authorization[I]): Authorization[I] = new Authorization[I] {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages): Future[Boolean] = {
def isAuthorized(identity: I)(implicit request: RequestHeader, messages: Messages, ec: ExecutionContext): Future[Boolean] = {
val leftF = self.isAuthorized(identity)
val rightF = authorization.isAuthorized(identity)
for {
Expand Down
3 changes: 2 additions & 1 deletion silhouette/app/com/mohiva/play/silhouette/api/Provider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.mohiva.play.silhouette.api
import play.api.mvc.Request

import scala.concurrent.Future
import scala.concurrent.ExecutionContext

/**
* A marker interface for all providers.
Expand Down Expand Up @@ -54,5 +55,5 @@ trait RequestProvider extends Provider {
* @tparam B The type of the body.
* @return Some login info on successful authentication or None if the authentication was unsuccessful.
*/
def authenticate[B](request: Request[B]): Future[Option[LoginInfo]]
def authenticate[B](request: Request[B])(implicit ec: ExecutionContext): Future[Option[LoginInfo]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import play.api.i18n.Messages
import play.api.mvc.{ RequestHeader, Result }

import scala.concurrent.Future
import scala.concurrent.ExecutionContext

/**
* Can be mixed into the GlobalSettings object to define a global behaviour
Expand All @@ -37,7 +38,7 @@ trait SecuredSettings {
* @param messages The messages for the current language.
* @return The result to send to the client.
*/
def onNotAuthenticated(request: RequestHeader, messages: Messages): Option[Future[Result]] = None
def onNotAuthenticated(request: RequestHeader, messages: Messages)(implicit ec: ExecutionContext): Option[Future[Result]] = None

/**
* Called when a user is authenticated but not authorized.
Expand All @@ -48,5 +49,5 @@ trait SecuredSettings {
* @param messages The messages for the current language.
* @return The result to send to the client.
*/
def onNotAuthorized(request: RequestHeader, messages: Messages): Option[Future[Result]] = None
def onNotAuthorized(request: RequestHeader, messages: Messages)(implicit ec: ExecutionContext): Option[Future[Result]] = None
}
Loading

0 comments on commit ddc150d

Please sign in to comment.