add basic credentials types
parent
5d0847db0d
commit
636b3c7034
|
|
@ -12,12 +12,11 @@ inline fun <reified T : Any> ConfigLoaderBuilder.addStringDecoder(crossinline de
|
||||||
|
|
||||||
fun ConfigLoaderBuilder.resolveAccountsFrom(path: Path): ConfigLoaderBuilder {
|
fun ConfigLoaderBuilder.resolveAccountsFrom(path: Path): ConfigLoaderBuilder {
|
||||||
val accounts = loadAccounts(path)
|
val accounts = loadAccounts(path)
|
||||||
addStringDecoder { value ->
|
return addStringDecoder { value ->
|
||||||
accounts.account[value].expect {
|
accounts[value].expect {
|
||||||
"The account $value could not be found. " +
|
"The account $value could not be found. " +
|
||||||
"Looked into path $path. " +
|
"Looked into path $path. " +
|
||||||
"Available accounts: ${accounts.account.map { it.key }.joinToString(",")}"
|
"Available accounts: ${accounts.map { it.key }.joinToString(",")}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TODO()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,21 @@ package de.itkl.modConfiguration.loaders
|
||||||
|
|
||||||
import com.sksamuel.hoplite.ConfigLoaderBuilder
|
import com.sksamuel.hoplite.ConfigLoaderBuilder
|
||||||
import com.sksamuel.hoplite.addPathSource
|
import com.sksamuel.hoplite.addPathSource
|
||||||
|
import de.itkl.modConfiguration.types.Account
|
||||||
import de.itkl.modConfiguration.types.Accounts
|
import de.itkl.modConfiguration.types.Accounts
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
|
||||||
fun loadAccounts(path: Path): Accounts {
|
internal fun loadAccounts(path: Path): Map<String, Account> {
|
||||||
return ConfigLoaderBuilder.default()
|
val loadedAccounts: Accounts = ConfigLoaderBuilder.default()
|
||||||
.addPathSource(path)
|
.addPathSource(path)
|
||||||
.build()
|
.build()
|
||||||
.loadConfigOrThrow()
|
.loadConfigOrThrow()
|
||||||
|
|
||||||
|
return loadedAccounts.account.mapValues { (name, accountDef) ->
|
||||||
|
Account(
|
||||||
|
name = name,
|
||||||
|
login = accountDef.login,
|
||||||
|
password = accountDef.password,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,15 @@ data class Account(
|
||||||
val password: String,
|
val password: String,
|
||||||
) {
|
) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Account(login='$login')"
|
return "Account($name, login='$login', password='***')"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class AccountDef(
|
internal data class AccountDef(
|
||||||
val login: String,
|
val login: String,
|
||||||
val password: String,
|
val password: String,
|
||||||
) {
|
) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Account(login='$login')"
|
return "Account(login='$login', password='***')"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
package de.itkl.modConfiguration.types
|
package de.itkl.modConfiguration.types
|
||||||
|
|
||||||
data class Accounts(
|
internal data class Accounts(
|
||||||
val account: Map<String, AccountDef>,
|
val account: Map<String, AccountDef>,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,36 @@
|
||||||
package de.itkl.module
|
package de.itkl.module
|
||||||
|
|
||||||
sealed class Credential {
|
import de.itkl.moduleCore.types.Expirable
|
||||||
abstract val id: String
|
import de.itkl.moduleCore.types.MaybeExpirable
|
||||||
abstract val notes: String
|
import kotlinx.datetime.Instant
|
||||||
|
|
||||||
|
sealed interface Credential : MaybeExpirable {
|
||||||
|
val id: String
|
||||||
|
val notes: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sealed interface Token : Credential
|
||||||
|
|
||||||
data class UsernameAndPassword(
|
data class UsernameAndPassword(
|
||||||
override val id: String,
|
override val id: String,
|
||||||
override val notes: String,
|
override val notes: String,
|
||||||
val username: String,
|
val username: String,
|
||||||
val password: String,
|
val password: String,
|
||||||
) : Credential()
|
) : Credential
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class TokenString(val value: String)
|
||||||
|
|
||||||
|
data class BearerToken(
|
||||||
|
val tokenString: TokenString,
|
||||||
|
override val id: String,
|
||||||
|
override val notes: String,
|
||||||
|
override val expiresAt: Instant,
|
||||||
|
) : Token, Expirable
|
||||||
|
|
||||||
|
data class RefreshToken(
|
||||||
|
val tokenString: TokenString,
|
||||||
|
override val id: String,
|
||||||
|
override val notes: String,
|
||||||
|
override val expiresAt: Instant,
|
||||||
|
) : Token, Expirable
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
package de.itkl.module
|
package de.itkl.module
|
||||||
|
|
||||||
class CredentialManager {
|
interface CredentialManager {
|
||||||
fun find(id: String): Credential? {
|
fun find(id: String): Credential?
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun add(credential: Credential) {}
|
fun add(credential: Credential) {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package de.itkl.moduleCore.types
|
||||||
|
|
||||||
|
import kotlinx.datetime.Clock
|
||||||
|
import kotlinx.datetime.Instant
|
||||||
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
interface Expirable : MaybeExpirable {
|
||||||
|
val expiresAt: Instant
|
||||||
|
val isExpired: Boolean get() = expiresAt < Clock.System.now()
|
||||||
|
val expiresIn: Duration get() = expiresAt - Clock.System.now()
|
||||||
|
override val expiration: Expirable get() = this
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MaybeExpirable {
|
||||||
|
val expiration: Expirable? get() = null
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue