Support JWT
This commit is contained in:
parent
25318cd6de
commit
aa9d60e4d1
9 changed files with 96 additions and 19 deletions
29
api/src/lu/foyer/auth/JwtScalaTokenService.scala
Normal file
29
api/src/lu/foyer/auth/JwtScalaTokenService.scala
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue