All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.programadorthi.routing.auth.Principal.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha02
Show newest version
/*
 * 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