Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#126 refactor AuthInfo as a trait instead of a case class #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/scala/scalaoauth2/provider/AuthorizationHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
39 changes: 30 additions & 9 deletions src/main/scala/scalaoauth2/provider/DataHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
12 changes: 6 additions & 6 deletions src/main/scala/scalaoauth2/provider/GrantHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
}
Expand All @@ -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)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down