diff --git a/src/main/scala/scalaoauth2/provider/AuthorizationHandler.scala b/src/main/scala/scalaoauth2/provider/AuthorizationHandler.scala index ef35d81..16eff7f 100644 --- a/src/main/scala/scalaoauth2/provider/AuthorizationHandler.scala +++ b/src/main/scala/scalaoauth2/provider/AuthorizationHandler.scala @@ -132,4 +132,20 @@ trait AuthorizationHandler[U] { */ def findAuthInfoByRefreshToken(refreshToken: String): Future[Option[AuthInfo[U]]] + /** + * Creates authorized information. + * + * Client credential, Password and Implicit Grant call this method. + * + * @param request Request sent by client + * @param user Authorized user + * @param clientId Authorized clientId + * @param scope Authorized scope + * @param redirectUri Authorized redirectUri + * @return Return authorized information + */ + def createAuthInfo(request: AuthorizationRequest, user: U, clientId: Option[String], scope: Option[String], redirectUri: Option[String]): AuthInfo[U] = { + AuthInfo(user, clientId, scope, redirectUri) + } + } diff --git a/src/main/scala/scalaoauth2/provider/DataHandler.scala b/src/main/scala/scalaoauth2/provider/DataHandler.scala index 4b96ebc..8fa5c1a 100644 --- a/src/main/scala/scalaoauth2/provider/DataHandler.scala +++ b/src/main/scala/scalaoauth2/provider/DataHandler.scala @@ -10,12 +10,12 @@ trait DataHandler[U] extends AuthorizationHandler[U] with ProtectedResourceHandl /** * Access token * - * @param token Access token is used to authentication. + * @param token Access token is used to authentication. * @param refreshToken Refresh token is used to re-issue access token. - * @param scope Inform the client of the scope of the access token issued. - * @param lifeSeconds Life of the access token since its creation. In seconds. - * @param createdAt Access token is created date. - * @param params Additional parameters to add information/restriction on given Access token. + * @param scope Inform the client of the scope of the access token issued. + * @param lifeSeconds Life of the access token since its creation. In seconds. + * @param createdAt Access token is created date. + * @param params Additional parameters to add information/restriction on given Access token. */ case class AccessToken(token: String, refreshToken: Option[String], scope: Option[String], lifeSeconds: Option[Long], createdAt: Date, params: Map[String, String] = Map.empty[String, String]) { def isExpired: Boolean = expiresIn.exists(_ < 0) @@ -29,12 +29,33 @@ case class AccessToken(token: String, refreshToken: Option[String], scope: Optio } } +object AuthInfo { + def apply[U](user: U, clientId: Option[String], scope: Option[String], redirectUri: Option[String]): AuthInfo[U] = { + DefaultAuthInfo(user, clientId, scope, redirectUri) + } +} + /** * Authorized information * - * @param user Authorized user which is registered on system. - * @param clientId Using client id which is registered on system. - * @param scope Inform the client of the scope of the access token issued. + * @param user Authorized user which is registered on system. + * @param clientId Using client id which is registered on system. + * @param scope Inform the client of the scope of the access token issued. + * @param redirectUri This value is used by Authorization Code Grant. + */ +trait AuthInfo[+U] { + def user: U + def clientId: Option[String] + def scope: Option[String] + def redirectUri: Option[String] +} + +/** + * Default Authorized information + * + * @param user Authorized user which is registered on system. + * @param clientId Using client id which is registered on system. + * @param scope Inform the client of the scope of the access token issued. * @param redirectUri This value is used by Authorization Code Grant. */ -case class AuthInfo[+U](user: U, clientId: Option[String], scope: Option[String], redirectUri: Option[String]) +case class DefaultAuthInfo[+U](user: U, clientId: Option[String], scope: Option[String], redirectUri: Option[String]) extends AuthInfo[U] \ No newline at end of file diff --git a/src/main/scala/scalaoauth2/provider/GrantHandler.scala b/src/main/scala/scalaoauth2/provider/GrantHandler.scala index 3227743..f173d9e 100644 --- a/src/main/scala/scalaoauth2/provider/GrantHandler.scala +++ b/src/main/scala/scalaoauth2/provider/GrantHandler.scala @@ -36,7 +36,7 @@ trait GrantHandler { }.map(createGrantHandlerResult(authInfo, _)) } - protected def shouldRefreshAccessToken(token: AccessToken) = token.isExpired + protected def shouldRefreshAccessToken(token: AccessToken): Boolean = token.isExpired protected def createGrantHandlerResult[U](authInfo: AuthInfo[U], accessToken: AccessToken) = GrantHandlerResult( authInfo, @@ -78,7 +78,7 @@ class Password extends GrantHandler { handler.findUser(maybeValidatedClientCred, passwordRequest).flatMap { maybeUser => val user = maybeUser.getOrElse(throw new InvalidGrant("username or password is incorrect")) val scope = passwordRequest.scope - val authInfo = AuthInfo(user, maybeValidatedClientCred.map(_.clientId), scope, None) + val authInfo = handler.createAuthInfo(passwordRequest, user, maybeValidatedClientCred.map(_.clientId), scope, None) issueAccessToken(handler, authInfo) } @@ -95,7 +95,7 @@ class ClientCredentials extends GrantHandler { handler.findUser(maybeValidatedClientCred, clientCredentialsRequest).flatMap { optionalUser => val user = optionalUser.getOrElse(throw new InvalidGrant("client_id or client_secret or scope is incorrect")) - val authInfo = AuthInfo(user, Some(clientId), scope, None) + val authInfo = handler.createAuthInfo(clientCredentialsRequest, user, Some(clientId), scope, None) issueAccessToken(handler, authInfo) } @@ -124,7 +124,7 @@ class AuthorizationCode extends GrantHandler { val f = issueAccessToken(handler, authInfo) for { accessToken <- f - deleteResult <- handler.deleteAuthCode(code) + _ <- handler.deleteAuthCode(code) } yield accessToken } } @@ -140,7 +140,7 @@ class Implicit extends GrantHandler { handler.findUser(maybeValidatedClientCred, implicitRequest).flatMap { maybeUser => val user = maybeUser.getOrElse(throw new InvalidGrant("user cannot be authenticated")) val scope = implicitRequest.scope - val authInfo = AuthInfo(user, Some(clientId), scope, None) + val authInfo = handler.createAuthInfo(implicitRequest, user, Some(clientId), scope, None) issueAccessToken(handler, authInfo) } @@ -154,7 +154,7 @@ class Implicit extends GrantHandler { /** * Implicit grant must not return refresh token */ - protected override def createGrantHandlerResult[U](authInfo: AuthInfo[U], accessToken: AccessToken) = + protected override def createGrantHandlerResult[U](authInfo: AuthInfo[U], accessToken: AccessToken): GrantHandlerResult[U] = super.createGrantHandlerResult(authInfo, accessToken).copy(refreshToken = None) } diff --git a/src/test/scala/scalaoauth2/provider/AuthorizationCodeSpec.scala b/src/test/scala/scalaoauth2/provider/AuthorizationCodeSpec.scala index 8073b25..45cfe67 100644 --- a/src/test/scala/scalaoauth2/provider/AuthorizationCodeSpec.scala +++ b/src/test/scala/scalaoauth2/provider/AuthorizationCodeSpec.scala @@ -5,8 +5,8 @@ import org.scalatest._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.time._ -import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future class AuthorizationCodeSpec extends FlatSpec with ScalaFutures with OptionValues { diff --git a/src/test/scala/scalaoauth2/provider/ProtectedResourceSpec.scala b/src/test/scala/scalaoauth2/provider/ProtectedResourceSpec.scala index 849e534..5b13c9b 100644 --- a/src/test/scala/scalaoauth2/provider/ProtectedResourceSpec.scala +++ b/src/test/scala/scalaoauth2/provider/ProtectedResourceSpec.scala @@ -2,8 +2,8 @@ package scalaoauth2.provider import java.util.Date -import org.scalatest._ import org.scalatest.Matchers._ +import org.scalatest._ import org.scalatest.concurrent.ScalaFutures import scala.concurrent.ExecutionContext.Implicits.global