From a00e8617e35d9bcddaec34b641237b9eaa6ca864 Mon Sep 17 00:00:00 2001 From: Will Sargent Date: Sat, 28 Dec 2019 14:58:57 -0800 Subject: [PATCH 1/3] Make secured request and userawarerequest traits --- .../api/actions/SecuredAction.scala | 44 +++++++++++++++--- .../api/actions/UserAwareAction.scala | 45 ++++++++++++++++--- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala index 7267ec7a5..6070b99f1 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala @@ -29,17 +29,51 @@ import play.api.{ Configuration, Environment => PlayEnv } import scala.concurrent.{ ExecutionContext, Future } +/** + * A request header that only allows access if an identity is authenticated and authorized. + * + * @tparam E The type of the environment. + */ +trait SecuredRequestHeader[E <: Env] extends RequestHeader { + /** + * @return The identity implementation. + */ + def identity: E#I + + /** + * @return The authenticator implementation. + */ + def authenticator: E#A +} + /** * A request that only allows access if an identity is authenticated and authorized. * - * @param identity The identity implementation. - * @param authenticator The authenticator implementation. - * @param request The current request. * @tparam E The type of the environment. * @tparam B The type of the request body. */ -case class SecuredRequest[E <: Env, B](identity: E#I, authenticator: E#A, request: Request[B]) - extends WrappedRequest(request) +trait SecuredRequest[E <: Env, +B] extends Request[B] with SecuredRequestHeader[E] + +object SecuredRequest { + /** + * A request that only allows access if an identity is authenticated and authorized. + * + * @param identity The identity implementation. + * @param authenticator The authenticator implementation. + * @param request The current request. + * @tparam E The type of the environment. + * @tparam B The type of the request body. + */ + def apply[E <: Env, B](identity: E#I, authenticator: E#A, request: Request[B]): SecuredRequest[E, B] = { + new DefaultSecuredRequest(identity, authenticator, request) + } +} + +class DefaultSecuredRequest[E <: Env, B] ( + val identity: E#I, + val authenticator: E#A, + request: Request[B] +) extends WrappedRequest(request) with SecuredRequest[E, B] /** * Request handler builder implementation to provide the foundation for secured request handlers. diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala index f2c4ba171..43aeba182 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala @@ -31,14 +31,47 @@ import scala.concurrent.{ ExecutionContext, Future } /** * A request that adds maybe the identity and maybe the authenticator for the current call. * - * @param identity Some identity implementation if authentication was successful, None otherwise. - * @param authenticator Some authenticator implementation if authentication was successful, None otherwise. - * @param request The current request. * @tparam E The type of the environment. - * @tparam B The type of the request body. */ -case class UserAwareRequest[E <: Env, B](identity: Option[E#I], authenticator: Option[E#A], request: Request[B]) - extends WrappedRequest(request) +trait UserAwareRequestHeader[E <: Env] extends RequestHeader { + /** + * @return Some identity implementation if authentication was successful, None otherwise. + */ + def identity: Option[E#I] + + /** + * @return Some authenticator implementation if authentication was successful, None otherwise. + */ + def authenticator: Option[E#A] +} + +trait UserAwareRequest[E <: Env, +B] extends Request[B] with UserAwareRequestHeader[E] + +object UserAwareRequest { + + /** + * A request that adds maybe the identity and maybe the authenticator for the current call. + * + * @param identity Some identity implementation if authentication was successful, None otherwise. + * @param authenticator Some authenticator implementation if authentication was successful, None otherwise. + * @param request The current request. + * @tparam E The type of the environment. + * @tparam B The type of the request body. + */ + def apply[E <: Env, B]( + identity: Option[E#I], + authenticator: Option[E#A], + request: Request[B]): UserAwareRequest[E, B] = { + new DefaultUserAwareRequest(identity, authenticator, request) + } + +} + +class DefaultUserAwareRequest[E <: Env, B] ( + val identity: Option[E#I], + val authenticator: Option[E#A], + request: Request[B] +) extends WrappedRequest(request) with UserAwareRequest[E, B] /** * Request handler builder implementation to provide the foundation for user-aware request handlers. From b801a51b5b89530df2d6203abec80ccd36b7d984 Mon Sep 17 00:00:00 2001 From: Will Sargent Date: Sat, 28 Dec 2019 15:26:13 -0800 Subject: [PATCH 2/3] reformat --- .../com/mohiva/play/silhouette/api/actions/SecuredAction.scala | 2 +- .../mohiva/play/silhouette/api/actions/UserAwareAction.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala index 6070b99f1..91c678c93 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala @@ -69,7 +69,7 @@ object SecuredRequest { } } -class DefaultSecuredRequest[E <: Env, B] ( +class DefaultSecuredRequest[E <: Env, B]( val identity: E#I, val authenticator: E#A, request: Request[B] diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala index 43aeba182..04349104f 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala @@ -67,7 +67,7 @@ object UserAwareRequest { } -class DefaultUserAwareRequest[E <: Env, B] ( +class DefaultUserAwareRequest[E <: Env, B]( val identity: Option[E#I], val authenticator: Option[E#A], request: Request[B] From 37f60dd8807e4c760f45503dd538837d713b90c9 Mon Sep 17 00:00:00 2001 From: Will Sargent Date: Tue, 21 Jan 2020 09:31:49 -0800 Subject: [PATCH 3/3] Add unapply methods --- .../silhouette/api/actions/SecuredAction.scala | 18 +++++++++++++++++- .../api/actions/UserAwareAction.scala | 17 ++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala index 91c678c93..11c68a24f 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/SecuredAction.scala @@ -67,12 +67,28 @@ object SecuredRequest { def apply[E <: Env, B](identity: E#I, authenticator: E#A, request: Request[B]): SecuredRequest[E, B] = { new DefaultSecuredRequest(identity, authenticator, request) } + + /** + * Unapply method for secured request. + * + * @param securedRequest the secured request. + * @tparam E The type of the environment. + * @tparam B The type of the request body. + */ + def unapply[E <: Env, B](securedRequest: SecuredRequest[E, B]): Option[(E#I, E#A, Request[B])] = { + securedRequest match { + case dsr: DefaultSecuredRequest[E, B] => + Some((dsr.identity, dsr.authenticator, dsr.request)) + case sr: SecuredRequest[E, B] => + Some((sr.identity, sr.authenticator, sr)) + } + } } class DefaultSecuredRequest[E <: Env, B]( val identity: E#I, val authenticator: E#A, - request: Request[B] + val request: Request[B] ) extends WrappedRequest(request) with SecuredRequest[E, B] /** diff --git a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala index 04349104f..87e94fde7 100644 --- a/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala +++ b/silhouette/app/com/mohiva/play/silhouette/api/actions/UserAwareAction.scala @@ -65,12 +65,27 @@ object UserAwareRequest { new DefaultUserAwareRequest(identity, authenticator, request) } + /** + * Unapply method for user aware request. + * + * @param userAwareRequest the user aware request. + * @tparam E The type of the environment. + * @tparam B The type of the request body. + */ + def unapply[E <: Env, B](userAwareRequest: UserAwareRequest[E, B]): Option[(Option[E#I], Option[E#A], Request[B])] = { + userAwareRequest match { + case duar: DefaultUserAwareRequest[E, B] => + Some((duar.identity, duar.authenticator, duar.request)) + case uar: UserAwareRequest[E, B] => + Some((uar.identity, uar.authenticator, uar)) + } + } } class DefaultUserAwareRequest[E <: Env, B]( val identity: Option[E#I], val authenticator: Option[E#A], - request: Request[B] + val request: Request[B] ) extends WrappedRequest(request) with UserAwareRequest[E, B] /**