Support JWT
This commit is contained in:
parent
25318cd6de
commit
aa9d60e4d1
9 changed files with 96 additions and 19 deletions
25
api/src/lu/foyer/auth/AuthMiddleware.scala
Normal file
25
api/src/lu/foyer/auth/AuthMiddleware.scala
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package lu.foyer
|
||||
package auth
|
||||
|
||||
import zio.*
|
||||
import zio.http.*
|
||||
|
||||
object AuthMiddleware:
|
||||
def jwtAuthentication(realm: String): HandlerAspect[JwtTokenService, UserInfo] =
|
||||
HandlerAspect.interceptIncomingHandler {
|
||||
handler { (request: Request) =>
|
||||
request.header(Header.Authorization) match
|
||||
case Some(Header.Authorization.Bearer(token)) =>
|
||||
ZIO
|
||||
.serviceWithZIO[JwtTokenService](_.verify(token.value.asString))
|
||||
.map(UserInfo(_))
|
||||
.map(userInfo => (request, userInfo))
|
||||
.orElseFail(
|
||||
Response.unauthorized.addHeaders(Headers(Header.WWWAuthenticate.Bearer(realm)))
|
||||
)
|
||||
case _ =>
|
||||
ZIO.fail(
|
||||
Response.unauthorized.addHeaders(Headers(Header.WWWAuthenticate.Bearer(realm)))
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue