/
JWTService.scala
45 lines (38 loc) · 1.17 KB
/
JWTService.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.auth
import spray.json.JsValue
import zio._
import scala.concurrent.duration
import org.knora.webapi._
import org.knora.webapi.config._
import org.knora.webapi.routing.JWTHelper
final case class JWTService(secret: String, longevity: duration.Duration, issuer: String) {
/**
* Creates a new JWT token for a specific user and holds some additional
* content.
*
* @param id the user's IRI.
* @param content containing additional information.
*/
def newToken(id: IRI, content: Map[String, JsValue]): UIO[String] =
ZIO.succeed {
JWTHelper.createToken(
userIri = id,
secret = secret,
longevity = longevity,
issuer = issuer,
content = content
)
}
}
object JWTService {
val layer: ZLayer[AppConfig, Nothing, JWTService] =
ZLayer {
for {
config <- ZIO.service[AppConfig]
} yield JWTService(config.jwtSecretKey, config.jwtLongevityAsDuration, config.knoraApi.externalKnoraApiHostPort)
}
}