foyer-dsi-assure-2035/api/src/lu/foyer/auth/JwtScalaTokenService.scala
Paul-Henri Froidmont aa9d60e4d1
Support JWT
2025-11-04 13:42:19 +01:00

29 lines
961 B
Scala

package lu.foyer
package auth
import java.time.Clock
import java.util.Base64
import zio.*
import zio.json.*
import lu.foyer.auth.JwtScalaTokenService.Claims
case class JwtScalaTokenService() extends JwtTokenService:
implicit val clock: Clock = Clock.systemUTC
// Doesn't actually verify, we just pretend for simplicity
override def verify(token: String): Task[String] =
token.split('.').drop(1).headOption match
case Some(base64) =>
new String(Base64.getDecoder().decode(base64)).fromJson[Claims] match
case Right(claims) => ZIO.succeed(claims.sub)
case Left(error) =>
ZIO.logWarning(s"Failed to parse JWT claims : $error") *>
ZIO.fail(new Exception("Invalid token"))
case None => ZIO.fail(new Exception("Invalid token"))
object JwtScalaTokenService:
val live = ZLayer.succeed(JwtScalaTokenService())
final private[auth] case class Claims(sub: String) derives JsonDecoder