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

Commit

Permalink
Make secured request and userawarerequest traits
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent committed Dec 28, 2019
1 parent 12976ca commit a00e861
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a00e861

Please sign in to comment.