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

com.lightningkite.lightningserver.auth.ModelInfo.ext.kt Maven / Gradle / Ivy

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

import com.lightningkite.lightningdb.*
import com.lightningkite.lightningserver.db.ModelInfo
import com.lightningkite.lightningserver.exceptions.UnauthorizedException
import kotlinx.serialization.KSerializer

fun > T.userPasswordAccess(
    newUser: suspend (email: String, hashedPassword: String) -> USER
): UserPasswordAccess where USER : HasId, USER : HasPassword, T : ModelInfo, USER : HasEmail {
    val info = this
    return object : UserPasswordAccess {
        override suspend fun byUsername(username: String, password: String): USER {
            val lowercased = username.lowercase()
            return info.collection().findOne(Condition.OnField(HasEmailFields.email(), Condition.Equal(lowercased)))
                ?: newUser(lowercased, password.secureHash()).let { info.collection().insertOne(it)!! }
        }

        override fun hashedPassword(user: USER): String = user.hashedPassword

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id
    }
}

fun > T.userEmailAccess(
    newUser: suspend (email: String) -> USER
): UserEmailAccess where USER : HasId, T : ModelInfo, USER : HasEmail {
    val info = this
    return object : UserEmailAccess {
        override suspend fun byEmail(email: String): USER {
            val lowercased = email.lowercase()
            return info.collection().findOne(Condition.OnField(HasEmailFields.email(), Condition.Equal(lowercased)))
                ?: newUser(lowercased).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id
    }
}

fun > T.userPhoneAccess(
    newUser: suspend (phone: String) -> USER
): UserPhoneAccess where USER : HasId, T : ModelInfo, USER : HasPhoneNumber {
    val info = this
    return object : UserPhoneAccess {
        override suspend fun byPhone(phone: String): USER {
            val cleaned = phone.filter { it.isDigit() }
            return info.collection()
                .findOne(Condition.OnField(HasPhoneNumberFields.phoneNumber(), Condition.Equal(cleaned))) ?: newUser(
                cleaned
            ).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id
    }
}

fun > T.userEmailPhoneAccess(
    newEmailUser: suspend (email: String) -> USER,
    newPhoneUser: suspend (phone: String) -> USER,
): UserPhoneAccess where USER : HasId, T : ModelInfo, USER : HasPhoneNumber, USER : HasEmail {
    val info = this
    return object : UserPhoneAccess, UserEmailAccess {
        override suspend fun byEmail(email: String): USER {
            val lowercased = email.lowercase()
            return info.collection().findOne(Condition.OnField(HasEmailFields.email(), Condition.Equal(lowercased)))
                ?: newEmailUser(lowercased).let { info.collection().insertOne(it)!! }
        }

        override suspend fun byPhone(phone: String): USER {
            val cleaned = phone.filter { it.isDigit() }
            return info.collection()
                .findOne(Condition.OnField(HasPhoneNumberFields.phoneNumber(), Condition.Equal(cleaned)))
                ?: newPhoneUser(cleaned).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id
    }
}

@JvmName("userEmailAccessMaybe")
fun > T.userEmailAccess(
    anonymous: suspend () -> USER,
    newUser: suspend (email: String) -> USER
): UserEmailAccess where USER : HasId, T : ModelInfo, USER : HasMaybeEmail {
    val info = this
    return object : UserEmailAccess {
        override suspend fun byEmail(email: String): USER {
            val lowercased = email.lowercase()
            return info.collection()
                .findOne(Condition.OnField(HasMaybeEmailFields.email(), Condition.Equal(lowercased))) ?: newUser(
                lowercased
            ).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id

        override suspend fun anonymous(): USER = anonymous()
    }
}

@JvmName("userPhoneAccessMaybe")
fun > T.userPhoneAccess(
    anonymous: suspend () -> USER,
    newUser: suspend (phone: String) -> USER
): UserPhoneAccess where USER : HasId, T : ModelInfo, USER : HasMaybePhoneNumber {
    val info = this
    return object : UserPhoneAccess {
        override suspend fun byPhone(phone: String): USER {
            val cleaned = phone.filter { it.isDigit() }
            return info.collection()
                .findOne(Condition.OnField(HasMaybePhoneNumberFields.phoneNumber(), Condition.Equal(cleaned)))
                ?: newUser(cleaned).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id

        override suspend fun anonymous(): USER = anonymous()
    }
}

@JvmName("userEmailPhoneAccessMaybe")
fun > T.userEmailPhoneAccess(
    anonymous: suspend () -> USER,
    newEmailUser: suspend (email: String) -> USER,
    newPhoneUser: suspend (phone: String) -> USER,
): UserPhoneAccess where USER : HasId, T : ModelInfo, USER : HasMaybePhoneNumber, USER : HasMaybeEmail {
    val info = this
    return object : UserPhoneAccess, UserEmailAccess {
        override suspend fun byEmail(email: String): USER {
            val lowercased = email.lowercase()
            return info.collection()
                .findOne(Condition.OnField(HasMaybeEmailFields.email(), Condition.Equal(lowercased))) ?: newEmailUser(
                lowercased
            ).let { info.collection().insertOne(it)!! }
        }

        override suspend fun byPhone(phone: String): USER {
            val cleaned = phone.filter { it.isDigit() }
            return info.collection()
                .findOne(Condition.OnField(HasMaybePhoneNumberFields.phoneNumber(), Condition.Equal(cleaned)))
                ?: newPhoneUser(cleaned).let { info.collection().insertOne(it)!! }
        }

        override val serializer: KSerializer
            get() = info.serialization.serializer
        override val idSerializer: KSerializer
            get() = info.serialization.idSerializer
        override val authInfo: AuthInfo
            get() = info.serialization.authInfo

        override suspend fun byId(id: ID): USER = info.collection().get(id) ?: throw UnauthorizedException()

        override fun id(user: USER): ID = user._id

        override suspend fun anonymous(): USER = anonymous()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy