com.lightningkite.lightningserver.auth.UserAccess.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
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")
)
}
}