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

com.lightningkite.lightningserver.auth.UserAccess.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.lightningserver.auth

import com.lightningkite.lightningserver.exceptions.BadRequestException
import com.lightningkite.lightningserver.exceptions.ForbiddenException
import kotlinx.serialization.KSerializer

/**
 * Information fo authentication purposes about how to access users, however they are stored.
 */
interface UserAccess {
    val serializer: KSerializer
    val idSerializer: KSerializer
    val authInfo: AuthInfo
    fun id(user: USER): ID
    suspend fun byId(id: ID): USER
    suspend fun anonymous(): USER = throw ForbiddenException("Anonymous users not permitted.")
}

interface UserEmailAccess : UserAccess {
    suspend fun byEmail(email: String): USER
}

interface UserPhoneAccess : UserAccess {
    suspend fun byPhone(phone: String): USER
}

interface UserPasswordAccess : UserAccess {
    suspend fun byUsername(username: String, password: String): USER
    fun hashedPassword(user: USER): String
}

interface UserExternalServiceAccess : UserAccess {
    suspend fun byExternalService(oauth: ExternalProfile): USER
}

fun  UserEmailAccess.asExternal(): UserExternalServiceAccess =
    object : UserExternalServiceAccess, UserAccess by this {
        override suspend fun byExternalService(oauth: ExternalProfile): USER {
            return [email protected](
                oauth.email ?: throw BadRequestException("No verified email found in external service")
            )
        }
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy