Skip to content

Commit

Permalink
Simplify return value for JwtService.isTokenValid
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Apr 22, 2024
1 parent 13a2ac3 commit 26cc439
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,12 @@ final case class AuthenticatorLive(
case Some(KnoraJWTTokenCredentialsV2(jwtToken)) =>
ZIO
.fail(BadCredentialsException(BAD_CRED_NOT_VALID))
.whenZIO(jwtService.validateToken(jwtToken).map(!_))
.when(!jwtService.isTokenValid(jwtToken))
.as(true)
case Some(KnoraSessionCredentialsV2(sessionToken)) =>
ZIO
.fail(BadCredentialsException(BAD_CRED_NOT_VALID))
.whenZIO(
jwtService.validateToken(sessionToken).map(!_),
)
.when(!jwtService.isTokenValid(sessionToken))
.as(true)
case None =>
ZIO.fail(BadCredentialsException(BAD_CRED_NONE_SUPPLIED))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ trait JwtService {
* @param token the JWT.
* @return a [[Boolean]].
*/
def validateToken(token: String): Task[Boolean]
def isTokenValid(token: String): Boolean

/**
* Extracts the encoded user IRI. This method also makes sure that the required headers and claims are present.
Expand Down Expand Up @@ -159,8 +159,7 @@ final case class JwtServiceLive(
* @param token the JWT.
* @return a [[Boolean]].
*/
override def validateToken(token: String): Task[Boolean] =
ZIO.succeed(!cache.contains(token) && decodeToken(token).isDefined)
override def isTokenValid(token: String): Boolean = !cache.contains(token) && decodeToken(token).isDefined

/**
* Extracts the encoded user IRI. This method also makes sure that the required headers and claims are present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object DspIngestClientLiveSpecLayers {
new JwtService {
override def createJwtForDspIngest(): UIO[Jwt] = ZIO.succeed(Jwt("mock-jwt-string-value", Long.MaxValue))
override def createJwt(user: User, content: Map[String, JsValue]): UIO[Jwt] = unsupported
override def validateToken(token: String): Task[Boolean] = unsupported
override def isTokenValid(token: String): Boolean = throw new UnsupportedOperationException("not implemented")
override def extractUserIriFromToken(token: String): Task[Option[IRI]] = unsupported
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object JwtServiceLiveSpec extends ZIOSpecDefault {
test("validate a self issued token") {
for {
token <- JwtService(_.createJwt(user))
isValid <- JwtService(_.validateToken(token.jwtString))
isValid <- ZIO.serviceWith[JwtService](_.isTokenValid(token.jwtString))
} yield assertTrue(isValid)
},
test("fail to validate an invalid token") {
Expand Down Expand Up @@ -219,7 +219,7 @@ object JwtServiceLiveSpec extends ZIOSpecDefault {
for {
secret <- ZIO.serviceWith[JwtConfig](_.secret)
token = JwtZIOJson.encode("""{"typ":"JWT","alg":"HS256"}""", claim.toJson, secret, JwtAlgorithm.HS256)
isValid <- JwtService(_.validateToken(token))
isValid <- ZIO.serviceWith[JwtService](_.isTokenValid(token))
} yield assertTrue(!isValid)
}
},
Expand Down

0 comments on commit 26cc439

Please sign in to comment.