Skip to content

Commit

Permalink
feat: make account identifiers en- & decodable with central registry
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Oct 11, 2024
1 parent 09ad48d commit 667563f
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package id.walt.ktorauthnz.accounts

import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.methods.AuthenticationMethod
import id.walt.ktorauthnz.methods.data.AuthMethodStoredData
import id.walt.ktorauthnz.sessions.AuthSession
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package id.walt.ktorauthnz.accounts

import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.methods.AuthenticationMethod
import id.walt.ktorauthnz.methods.data.AuthMethodStoredData

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package id.walt.ktorauthnz.accounts

import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.UsernameIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.UsernameIdentifier
import id.walt.ktorauthnz.methods.AuthenticationMethod
import id.walt.ktorauthnz.methods.TOTP
import id.walt.ktorauthnz.methods.UserPass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package id.walt.ktorauthnz.accounts.identifiers

import id.walt.ktorauthnz.accounts.identifiers.methods.*

object AccountIdentifierRegistry {

private val defaultIdentifiers =
listOf(EmailIdentifier, JWTIdentifier, LDAPIdentifier, OIDCIdentifier, RADIUSIdentifier, UsernameIdentifier)

private val factories: MutableMap<String, AccountIdentifier.AccountIdentifierFactory<out AccountIdentifier>> =
defaultIdentifiers.associateBy { it.identifierName }.toMutableMap()

fun registerAccountIdentifier(identifierFactory: AccountIdentifier.AccountIdentifierFactory<out AccountIdentifier>) =
factories.set(identifierFactory.identifierName, identifierFactory)

fun getAccountIdentifier(type: String, accountIdentifierDataString: String): AccountIdentifier {
val factory = factories[type] ?: error("No such account identifier known")

return factory.fromAccountIdentifierDataString(accountIdentifierDataString)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package id.walt.ktorauthnz.accounts.identifiers
package id.walt.ktorauthnz.accounts.identifiers.methods

import id.walt.ktorauthnz.KtorAuthnzManager
import kotlinx.serialization.Serializable

@Suppress("EqualsOrHashCode")
@Serializable
abstract class AccountIdentifier(val identifierName: String) {

override fun toString(): String = "[$identifierName: ${getString()}]"
abstract fun getString(): String

abstract override fun hashCode(): Int
override fun toString(): String = "[$identifierName: ${toDataString()}]"
abstract fun toDataString(): String

override fun equals(other: Any?): Boolean {
if (other !is AccountIdentifier) return false
Expand All @@ -19,5 +19,9 @@ abstract class AccountIdentifier(val identifierName: String) {
}

fun resolveToAccountId() = KtorAuthnzManager.accountStore.lookupAccountUuid(this)

abstract class AccountIdentifierFactory<T : AccountIdentifier>(val identifierName: String) {
abstract fun fromAccountIdentifierDataString(dataString: String): T
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.Serializable

@Serializable
data class EmailIdentifier(val email: String) : AccountIdentifier("email") {
override fun toDataString() = email

companion object : AccountIdentifierFactory<EmailIdentifier>("email") {
override fun fromAccountIdentifierDataString(dataString: String): EmailIdentifier = EmailIdentifier(dataString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.Serializable

@Serializable
data class JWTIdentifier(val subject: String) : AccountIdentifier("jwt") {
override fun toDataString() = subject

companion object : AccountIdentifierFactory<JWTIdentifier>("jwt") {
override fun fromAccountIdentifierDataString(dataString: String) = JWTIdentifier(dataString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class LDAPIdentifier(val host: String, val name: String) : AccountIdentifier("ldap") {
override fun toDataString() = Json.encodeToString(this)

companion object : AccountIdentifierFactory<LDAPIdentifier>("ldap") {
override fun fromAccountIdentifierDataString(dataString: String) = Json.decodeFromString<LDAPIdentifier>(dataString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

data class OIDCIdentifier(val host: String, val name: String) : AccountIdentifier("oidc") {
override fun toDataString() = Json.encodeToString(this)

companion object : AccountIdentifierFactory<OIDCIdentifier>("oidc") {
override fun fromAccountIdentifierDataString(dataString: String) = Json.decodeFromString<OIDCIdentifier>(dataString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class RADIUSIdentifier(val host: String, val name: String) : AccountIdentifier("radius") {
override fun toDataString() = Json.encodeToString(this)

companion object : AccountIdentifierFactory<RADIUSIdentifier>("radius") {
override fun fromAccountIdentifierDataString(dataString: String) = Json.decodeFromString<RADIUSIdentifier>(dataString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package id.walt.ktorauthnz.accounts.identifiers.methods

import kotlinx.serialization.Serializable

@Serializable
data class UsernameIdentifier(val name: String) : AccountIdentifier("username") {
override fun toDataString() = name

companion object : AccountIdentifierFactory<UsernameIdentifier>("username") {
override fun fromAccountIdentifierDataString(dataString: String) = UsernameIdentifier(dataString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.KtorAuthnzManager
import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.methods.data.AuthMethodStoredData
import id.walt.ktorauthnz.sessions.AuthSession
import id.walt.ktorauthnz.sessions.AuthSessionStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.EmailIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.EmailIdentifier
import id.walt.ktorauthnz.exceptions.authCheck
import id.walt.ktorauthnz.methods.data.EmailPassStoredData
import id.walt.ktorauthnz.sessions.AuthSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package id.walt.ktorauthnz.methods
import com.nimbusds.jose.JWSObject
import com.nimbusds.jose.crypto.MACVerifier
import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.JWTIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.JWTIdentifier
import id.walt.ktorauthnz.exceptions.authCheck
import id.walt.ktorauthnz.methods.config.JwtAuthConfiguration
import id.walt.ktorauthnz.sessions.AuthSessionInformation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.LDAPIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.LDAPIdentifier
import id.walt.ktorauthnz.exceptions.authFailure
import id.walt.ktorauthnz.methods.config.LDAPConfiguration
import id.walt.ktorauthnz.sessions.AuthSession
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.OIDCIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.OIDCIdentifier
import id.walt.ktorauthnz.methods.config.OidcAuthConfiguration
import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.client.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.RADIUSIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.RADIUSIdentifier
import id.walt.ktorauthnz.exceptions.authCheck
import id.walt.ktorauthnz.methods.config.RADIUSConfiguration
import id.walt.ktorauthnz.sessions.AuthSession
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.UsernameIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.UsernameIdentifier
import id.walt.ktorauthnz.exceptions.authCheck
import id.walt.ktorauthnz.methods.data.UserPassStoredData
import id.walt.ktorauthnz.sessions.AuthSession
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package id.walt.ktorauthnz.methods

import id.walt.ktorauthnz.accounts.identifiers.AccountIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.AccountIdentifier
import id.walt.ktorauthnz.sessions.AuthSession
import io.ktor.http.*
import io.ktor.server.application.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package id.walt.ktorauthnz.methods.virtual

import id.walt.ktorauthnz.AuthContext
import id.walt.ktorauthnz.KtorAuthnzManager
import id.walt.ktorauthnz.accounts.identifiers.EmailIdentifier
import id.walt.ktorauthnz.accounts.identifiers.methods.EmailIdentifier
import id.walt.ktorauthnz.methods.data.FlowAmendmentData
import id.walt.ktorauthnz.sessions.SessionManager
import io.github.smiley4.ktorswaggerui.dsl.routing.post
Expand Down

0 comments on commit 667563f

Please sign in to comment.