Support JWT

This commit is contained in:
Paul-Henri Froidmont 2025-11-04 13:42:19 +01:00
parent 25318cd6de
commit aa9d60e4d1
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
9 changed files with 96 additions and 19 deletions

View file

@ -3,6 +3,8 @@ package lu.foyer
import zio.*
import zio.schema.Schema
import lu.foyer.auth.UserInfo
final case class Entity[T](entityId: String, data: T, version: Long)
final case class Event[T](entityId: String, data: T, eventId: String)
@ -28,13 +30,14 @@ trait CommandHandler[+Command, +Event, +State]:
def commandSchema: Schema[?]
trait CommandHandlerCreate[Command: Schema, Event] extends CommandHandler[Command, Event, Nothing]:
def onCommand(entityId: String, command: Command): IO[JsonApiError | Throwable, Event]
def onCommand(entityId: String, command: Command): ZIO[UserInfo, JsonApiError | Throwable, Event]
val isCreate = true
val commandSchema = summon[Schema[Command]]
trait CommandHandlerUpdate[Command: Schema, Event, State]
extends CommandHandler[Command, Event, State]:
def onCommand(entityId: String, state: State, command: Command) : IO[JsonApiError | Throwable, Event]
def onCommand(entityId: String, state: State, command: Command)
: ZIO[UserInfo, JsonApiError | Throwable, Event]
val isCreate = false
val commandSchema = summon[Schema[Command]]
@ -45,7 +48,7 @@ class CommandEngine[Command, Event, State](
val stateRepo: StateRepository[State]):
def handleCommand(command: Command, name: String, entityId: String)
: IO[JsonApiError | Throwable, (lu.foyer.Event[Event], lu.foyer.Entity[State])] =
: ZIO[UserInfo, JsonApiError | Throwable, (lu.foyer.Event[Event], lu.foyer.Entity[State])] =
for
handler <- ZIO
.succeed(handlers.find(_.name == name))
@ -70,7 +73,7 @@ class CommandEngine[Command, Event, State](
entityId: String,
entityOption: Option[Entity[State]],
handler: CommandHandler[Command, Event, State]
): IO[JsonApiError | Throwable, (Event, Option[State])] = (entityOption, handler) match
): ZIO[UserInfo, JsonApiError | Throwable, (Event, Option[State])] = (entityOption, handler) match
case (None, h) if !h.isUpdate =>
h.asInstanceOf[CommandHandlerCreate[Command, Event]]
.onCommand(entityId, command)

View file

@ -0,0 +1,7 @@
package lu.foyer
package auth
import zio.*
trait JwtTokenService:
def verify(token: String): Task[String]

View file

@ -0,0 +1,4 @@
package lu.foyer
package auth
final case class UserInfo(subject: String)

View file

@ -3,6 +3,7 @@ package contracts
import zio.*
import lu.foyer.auth.UserInfo
import lu.foyer.clients.*
object ContractHandlers:
@ -92,11 +93,11 @@ class ApproveHandler(employeeService: EmployeeService)
val name = "approve"
def onCommand(entityId: String, state: ContractState.Pending, command: ContractCommand.Approve)
: Task[ContractEvent.Approved] =
: RIO[UserInfo, ContractEvent.Approved] =
for
user <- ZIO.succeed("") // TODO current user
user <- ZIO.service[UserInfo]
employee <- ZIO
.fromEither(employeeService.fetchOne(user))
.fromEither(employeeService.fetchOne(user.subject))
.mapError(new IllegalArgumentException(_))
yield ContractEvent.Approved(employee)
@ -110,11 +111,11 @@ class RejectHandler(employeeService: EmployeeService)
val name = "reject"
def onCommand(entityId: String, state: ContractState.Pending, command: ContractCommand.Reject)
: Task[ContractEvent.Rejected] =
: RIO[UserInfo, ContractEvent.Rejected] =
for
user <- ZIO.succeed("") // TODO current user
user <- ZIO.service[UserInfo]
employee <- ZIO
.fromEither(employeeService.fetchOne(user))
.fromEither(employeeService.fetchOne(user.subject))
.mapError(new IllegalArgumentException(_))
yield ContractEvent.Rejected(employee, command.comment)