|
|
|
@ -1,12 +1,18 @@
|
|
|
|
|
package org.tbasket.jwt
|
|
|
|
|
|
|
|
|
|
import pdi.jwt.JwtAlgorithm
|
|
|
|
|
import pdi.jwt.algorithms.JwtUnknownAlgorithm
|
|
|
|
|
import zio.*
|
|
|
|
|
import zio.stream.*
|
|
|
|
|
import zio.http.*
|
|
|
|
|
import zio.http.ServerConfig.LeakDetectionLevel
|
|
|
|
|
import zio.http.model.{Method, Status}
|
|
|
|
|
import zio.stream.*
|
|
|
|
|
|
|
|
|
|
import java.nio.file.Files
|
|
|
|
|
import java.nio.file.{Files, Path}
|
|
|
|
|
import java.security.{KeyFactory, PrivateKey}
|
|
|
|
|
import java.security.spec.{KeySpec, PKCS8EncodedKeySpec, RSAPrivateKeySpec, X509EncodedKeySpec}
|
|
|
|
|
import java.time.Duration
|
|
|
|
|
import scala.util.chaining.scalaUtilChainingOps
|
|
|
|
|
|
|
|
|
|
object Main extends ZIOAppDefault:
|
|
|
|
|
|
|
|
|
@ -18,11 +24,9 @@ object Main extends ZIOAppDefault:
|
|
|
|
|
case Some(oorPort) => ZIO.dieMessage(s"'$oorPort' is out of range.'")
|
|
|
|
|
case None => ZIO.dieMessage("given argument is not a valid integer")
|
|
|
|
|
|
|
|
|
|
private val app = Http.collectZIO[Request][Any, Throwable, Response] {
|
|
|
|
|
private val app = Http.collectZIO[Request] {
|
|
|
|
|
case r @ Method.GET -> _ / "jwt" =>
|
|
|
|
|
ZIO.attempt(JwtGenerator)
|
|
|
|
|
.flatMap(_.generateTokenResponse(r))
|
|
|
|
|
.catchAll(e => ZIO.die(e))
|
|
|
|
|
ZIO.serviceWithZIO[JwtGenerator](_.generateTokenResponse(r))
|
|
|
|
|
case _ =>
|
|
|
|
|
ZIO.succeed(Response(status = Status.NotFound))
|
|
|
|
|
}
|
|
|
|
@ -32,11 +36,15 @@ object Main extends ZIOAppDefault:
|
|
|
|
|
.port(port)
|
|
|
|
|
.leakDetection(LeakDetectionLevel.PARANOID)
|
|
|
|
|
|
|
|
|
|
val pbytes = Files.readAllBytes(Path.of("/home/maxime/tmp/key.pcqks"))
|
|
|
|
|
val key: PrivateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(pbytes))
|
|
|
|
|
|
|
|
|
|
val generator = new JwtGenerator(Duration.ofDays(15), key, JwtAlgorithm.RS256)
|
|
|
|
|
val configLayer = ServerConfig.live(config)
|
|
|
|
|
(Server.install(
|
|
|
|
|
app
|
|
|
|
|
) *> Console.printLine(s"JWT AppToken open on port $port") *> ZIO.never)
|
|
|
|
|
.provide(configLayer, Server.live)
|
|
|
|
|
.provide(configLayer, Server.live, ZLayer.succeed(generator))
|
|
|
|
|
|
|
|
|
|
val run =
|
|
|
|
|
ZIO.serviceWithZIO[ZIOAppArgs](args => port(args.getArgs))
|
|
|
|
|