30 lines
827 B
Kotlin
30 lines
827 B
Kotlin
package com.oliver.kidio.backend.plugins
|
|
|
|
import com.oliver.kidio.backend.security.JwtService
|
|
import io.ktor.server.application.*
|
|
import io.ktor.server.auth.*
|
|
import io.ktor.server.auth.jwt.*
|
|
|
|
/**
|
|
* Repräsentiert den authentifizierten Benutzer im Request-Kontext.
|
|
*/
|
|
data class UserPrincipal(val userId: String) : Principal
|
|
|
|
fun Application.configureSecurity(jwtService: JwtService) {
|
|
install(Authentication) {
|
|
jwt("auth-jwt") {
|
|
realm = "Kidio Backend"
|
|
verifier(jwtService.verifier)
|
|
|
|
validate { credential ->
|
|
val userId = credential.payload.getClaim("userId").asString()
|
|
if (!userId.isNullOrEmpty()) {
|
|
UserPrincipal(userId)
|
|
} else {
|
|
null
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|