Skip to content

Commit

Permalink
update access token
Browse files Browse the repository at this point in the history
  • Loading branch information
fupelaqu committed Dec 7, 2023
1 parent 83d3f1b commit 21105b8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ThisBuild / organization := "app.softnetwork"

name := "account"

ThisBuild / version := "0.6.2"
ThisBuild / version := "0.6.2.1"

ThisBuild / scalaVersion := "2.12.18"

Expand Down
3 changes: 2 additions & 1 deletion common/src/main/protobuf/model/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ message AuthorizationCode {

message AccessToken {
option (scalapb.message).extends = "ProtobufDomainObject";
option (scalapb.message).extends = "VerificationExpirationDate";
option (scalapb.message).extends = "AccessTokenDecorator";
option (scalapb.message).companion_extends = "AccessTokenCompanion";
required string token = 1;
required string tokenType = 2 [default = "Bearer"];
optional string scope = 3;
required google.protobuf.Timestamp expirationDate = 4 [(scalapb.field).type = "java.time.Instant"];
required string refreshToken = 5;
optional google.protobuf.Timestamp refreshExpirationDate = 6 [(scalapb.field).type = "java.time.Instant"];
}

message Application {
Expand Down
7 changes: 6 additions & 1 deletion common/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ auth {

access-token {
expirationTime = 30
expirationTime = ${?AUTH_ACTIVATION_TOKEN_EXPIRATION_TIME}
expirationTime = ${?AUTH_ACCESS_TOKEN_EXPIRATION_TIME}
}

refresh-token {
expirationTime = 525600 // 60 * 24 * 365
expirationTime = ${?AUTH_REFRESH_TOKEN_EXPIRATION_TIME}
}

providers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ case class OAuthProvider(

case class AuthorizationCode(expirationTime: Int)

case class AccessToken(expirationTime: Int)
sealed trait Token {
def expirationTime: Int
}

case class AccessToken(expirationTime: Int) extends Token

case class RefreshToken(expirationTime: Int) extends Token

case class OAuthConfig(
path: String,
authorizationCode: AuthorizationCode,
accessToken: AccessToken,
refreshToken: RefreshToken,
providers: Map[String, OAuthProvider]
)
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ package object message {
access_token: String,
token_type: String,
expires_in: Int,
refresh_token: String
refresh_token: String,
refresh_token_expires_in: Option[Int] = None
)

case class OAuthSucceededResult(account: Account, application: Application)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ trait ExpirationToken extends ExpirationDate {
trait VerificationExpirationDate {
def expirationDate: Instant
final def expired: Boolean = Specification(ExpirationDateRule).isSatisfiedBy(this)
final def expiresIn: Int =
Math.max(0, (expirationDate.getEpochSecond - Instant.now().getEpochSecond).toInt)
}

case object ExpirationDateRule extends Rule[VerificationExpirationDate] {
Expand Down Expand Up @@ -81,7 +83,19 @@ trait AccessTokenCompanion extends ExpirationToken {
.withToken(generateToken(prefix))
.withExpirationDate(compute(OAuthSettings.accessToken.expirationTime))
.withRefreshToken(generateToken(prefix))
.withRefreshExpirationDate(compute(OAuthSettings.refreshToken.expirationTime))
.copy(scope = scope)
}

}

trait AccessTokenDecorator extends VerificationExpirationDate { _: AccessToken =>
private lazy val refreshVerification: Option[VerificationExpirationDate] =
refreshExpirationDate.map(exp =>
new VerificationExpirationDate {
override def expirationDate: Instant = exp
}
)
def refreshExpired: Boolean = refreshVerification.exists(_.expired)
def refreshExpiresIn: Option[Int] = refreshVerification.map(_.expiresIn)
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ trait AccountBehavior[T <: Account with AccountDecorator, P <: Profile]
case Some(account) if account.status.isActive =>
import cmd._
account.applications.find(
_.accessToken.map(_.refreshToken).getOrElse("") == sha256(refreshToken)
_.accessToken.exists(ac => ac.refreshToken == sha256(refreshToken) && !ac.expired)
) match {
case Some(application) =>
val accessToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ trait OAuthService[SD <: SessionData with SessionDataDecorator[SD]]
Tokens(
r.accessToken.token,
r.accessToken.tokenType.toLowerCase(),
AccountSettings.OAuthSettings.accessToken.expirationTime * 60,
r.accessToken.refreshToken
r.accessToken.expiresIn,
r.accessToken.refreshToken,
r.accessToken.refreshExpiresIn
)
)
case error: AccountErrorMessage =>
Expand All @@ -124,8 +125,9 @@ trait OAuthService[SD <: SessionData with SessionDataDecorator[SD]]
Tokens(
r.accessToken.token,
r.accessToken.tokenType.toLowerCase(),
AccountSettings.OAuthSettings.accessToken.expirationTime * 60,
r.accessToken.refreshToken
r.accessToken.expiresIn,
r.accessToken.refreshToken,
r.accessToken.refreshExpiresIn
)
)
case error: AccountErrorMessage =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ trait OAuthServiceEndpoints[SD <: SessionData with SessionDataDecorator[SD]]
Tokens(
r.accessToken.token,
r.accessToken.tokenType.toLowerCase(),
AccountSettings.OAuthSettings.accessToken.expirationTime * 60,
r.accessToken.refreshToken
r.accessToken.expiresIn,
r.accessToken.refreshToken,
r.accessToken.refreshExpiresIn
)
)
case error: AccountErrorMessage =>
Expand All @@ -180,8 +181,9 @@ trait OAuthServiceEndpoints[SD <: SessionData with SessionDataDecorator[SD]]
Tokens(
r.accessToken.token,
r.accessToken.tokenType.toLowerCase(),
AccountSettings.OAuthSettings.accessToken.expirationTime * 60,
r.accessToken.refreshToken
r.accessToken.expiresIn,
r.accessToken.refreshToken,
r.accessToken.refreshExpiresIn
)
)
case error: AccountErrorMessage =>
Expand Down

0 comments on commit 21105b8

Please sign in to comment.