main.dev.neeffect.nee.security.oauth.OauthConfigModule.kt Maven / Gradle / Ivy
package dev.neeffect.nee.security.oauth
import dev.neeffect.nee.effects.time.HasteTimeProvider
import dev.neeffect.nee.effects.time.TimeProvider
import dev.neeffect.nee.security.User
import dev.neeffect.nee.security.UserRole
import dev.neeffect.nee.security.jwt.JwtConfig
import dev.neeffect.nee.security.jwt.JwtConfigurationModule
import dev.neeffect.nee.security.jwt.SimpleUserCoder
import dev.neeffect.nee.security.jwt.UserCoder
import dev.neeffect.nee.security.state.ServerVerifier
import io.ktor.client.HttpClient
import io.ktor.client.features.json.JacksonSerializer
import io.ktor.client.features.json.JsonFeature
import io.vavr.collection.Seq
import io.vavr.kotlin.list
import java.security.KeyPair
import java.security.SecureRandom
import java.util.Random
import java.util.UUID
abstract class OauthConfigModule(
val config: OauthConfig,
val jwtConfig: JwtConfig
) {
open val randomGenerator: Random by lazy {
SecureRandom()
}
open val baseTimeProvider: TimeProvider by lazy {
HasteTimeProvider()
}
open val keyPair: KeyPair by lazy {
ServerVerifier.generateKeyPair()
}
open val serverVerifier: ServerVerifier by lazy {
ServerVerifier(rng = this.randomGenerator, keyPair = keyPair)
}
open val httpClient by lazy {
HttpClient() {
install(JsonFeature) { // TODO - move it so that it is tested (it was not)
serializer = JacksonSerializer()
}
}
}
open val jwtConfigModule: JwtConfigurationModule by lazy {
object : JwtConfigurationModule(jwtConfig, baseTimeProvider) {
override val userCoder: UserCoder = [email protected]
}
}
abstract val userEncoder: (OauthProviderName, OauthResponse) -> USER
abstract val userRoles: (OauthProviderName, OauthResponse) -> Seq
abstract val userCoder: UserCoder
}
open class SimpleOauthConfigModule(
config: OauthConfig,
jwtConfig: JwtConfig
) : OauthConfigModule(config, jwtConfig) {
override val userCoder: UserCoder = SimpleUserCoder()
override val userEncoder: (OauthProviderName, OauthResponse) -> User = { provider, oauthResponse ->
val uuid = UUID(randomGenerator.nextLong(), randomGenerator.nextLong())
val roles = userRoles(provider, oauthResponse)
User(
uuid,
"${provider.providerName}:${oauthResponse.subject}",
roles.toList(),
oauthResponse.displayName.getOrElse(oauthResponse.subject)
)
}
override val userRoles: (OauthProviderName, OauthResponse) -> Seq = { _, _ ->
list(oauthUser)
}
val oauthUser = UserRole("oauthUser")
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy