|
| 1 | +package no.nav.hjelpemidler |
| 2 | + |
| 3 | +import io.ktor.http.auth.AuthScheme |
| 4 | +import io.ktor.http.auth.HttpAuthHeader |
| 5 | +import io.ktor.server.application.ApplicationCall |
| 6 | +import io.ktor.server.auth.AuthenticationConfig |
| 7 | +import io.ktor.server.auth.AuthenticationContext |
| 8 | +import io.ktor.server.auth.AuthenticationFailedCause |
| 9 | +import io.ktor.server.auth.AuthenticationFunction |
| 10 | +import io.ktor.server.auth.AuthenticationProvider |
| 11 | +import io.ktor.server.auth.Principal |
| 12 | +import io.ktor.server.auth.UnauthorizedResponse |
| 13 | +import io.ktor.server.auth.parseAuthorizationHeader |
| 14 | +import io.ktor.server.request.ApplicationRequest |
| 15 | +import io.ktor.server.response.respond |
| 16 | + |
| 17 | +fun AuthenticationConfig.token( |
| 18 | + name: String? = null, |
| 19 | + configure: TokenAuthenticationProvider.Config.() -> Unit, |
| 20 | +) { |
| 21 | + val provider = TokenAuthenticationProvider(TokenAuthenticationProvider.Config(name).apply(configure)) |
| 22 | + register(provider) |
| 23 | +} |
| 24 | + |
| 25 | +fun ApplicationRequest.tokenCredentials(): TokenCredential? { |
| 26 | + when (val authHeader = parseAuthorizationHeader()) { |
| 27 | + is HttpAuthHeader.Single -> { |
| 28 | + if (!authHeader.authScheme.equals(AuthScheme.Bearer, ignoreCase = true)) { |
| 29 | + return null |
| 30 | + } |
| 31 | + return TokenCredential(authHeader.blob) |
| 32 | + } |
| 33 | + else -> return null |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class TokenAuthenticationProvider internal constructor(config: Config) : AuthenticationProvider(config) { |
| 38 | + internal val authenticationFunction = config.authenticationFunction |
| 39 | + |
| 40 | + override suspend fun onAuthenticate(context: AuthenticationContext) { |
| 41 | + val call = context.call |
| 42 | + val credentials = call.request.tokenCredentials() |
| 43 | + val principal = credentials?.let { authenticationFunction(call, it) } |
| 44 | + val cause = when { |
| 45 | + credentials == null -> AuthenticationFailedCause.NoCredentials |
| 46 | + principal == null -> AuthenticationFailedCause.InvalidCredentials |
| 47 | + else -> null |
| 48 | + } |
| 49 | + if (cause != null) { |
| 50 | + @Suppress("NAME_SHADOWING") |
| 51 | + context.challenge("", cause) { challenge, call -> |
| 52 | + call.respond( |
| 53 | + UnauthorizedResponse( |
| 54 | + HttpAuthHeader.Parameterized( |
| 55 | + AuthScheme.Bearer, |
| 56 | + mapOf("realm" to "Ktor Server") |
| 57 | + ) |
| 58 | + ) |
| 59 | + ) |
| 60 | + challenge.complete() |
| 61 | + } |
| 62 | + } |
| 63 | + if (principal != null) { |
| 64 | + context.principal(principal) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + class Config internal constructor(name: String?) : AuthenticationProvider.Config(name) { |
| 69 | + internal var authenticationFunction: AuthenticationFunction<TokenCredential> = { |
| 70 | + throw NotImplementedError( |
| 71 | + "Token validate function is not specified. Use token { validate { ... } } to fix." |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + fun validate(body: suspend ApplicationCall.(TokenCredential) -> Principal?) { |
| 76 | + authenticationFunction = body |
| 77 | + } |
| 78 | + |
| 79 | + fun validate(expectedToken: String) = validate { |
| 80 | + when (it.token) { |
| 81 | + expectedToken -> TokenPrincipal(it.token) |
| 82 | + else -> null |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +data class TokenCredential(val token: String) |
| 89 | + |
| 90 | +data class TokenPrincipal(val token: String) : Principal |
0 commit comments