commonMain.dev.programadorthi.routing.auth.Principal.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of auth-jvm Show documentation
Show all versions of auth-jvm Show documentation
An extensible and multiplatform routing system powered by Ktor
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.programadorthi.routing.auth
import kotlin.reflect.KClass
/**
* A marker interface indicating that a class represents credentials for authentication.
*/
public interface Credential
/**
* A marker interface indicating that a class represents an authenticated principal.
*/
public interface Principal
internal class CombinedPrincipal : Principal {
val principals: MutableList> = mutableListOf()
inline fun get(provider: String?): T? {
return get(provider, T::class)
}
@Suppress("UNCHECKED_CAST")
fun get(
provider: String?,
klass: KClass,
): T? {
return principals
.firstOrNull { (name, principal) ->
if (provider != null) {
name == provider && klass.isInstance(principal)
} else {
klass.isInstance(principal)
}
}?.second as? T
}
fun add(
provider: String?,
principal: Principal,
) {
principals.add(Pair(provider, principal))
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy