Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created November 9, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/86ebe22ad4ef3488e0891ef7da1d3d25 to your computer and use it in GitHub Desktop.
Save BetterProgramming/86ebe22ad4ef3488e0891ef7da1d3d25 to your computer and use it in GitHub Desktop.
class AuthorizedClientsAuthProvider(
private val config: Config
): AuthenticationProvider(config), KoinComponent {
private val authorizedClientsRepository by inject<AuthorizedClientsRepository>()
override suspend fun onAuthenticate(context: AuthenticationContext) {
context.call.request.headers[config.headerName]?.let { clientId ->
if(authorizedClientsRepository.exists(clientId)) {
context.principal(UserIdPrincipal(clientId))
} else {
throw UnauthorizedClientException("Authorization Failed")
}
} ?: throw UnauthorizedClientException("Authorization Request must be provided")
}
class Config internal constructor(name: String?) : AuthenticationProvider.Config(name) {
var headerName: String = "ClientId"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment