small refactor

This commit is contained in:
2025-08-17 09:52:46 +02:00
parent 9c363d4f4c
commit dcb490ae31
6 changed files with 11 additions and 19 deletions

1
backend/.gitignore vendored
View File

@@ -6,4 +6,5 @@
/gradle.properties /gradle.properties
# Kotlin # Kotlin
/.kotlin/
/build/ /build/

View File

@@ -13,10 +13,10 @@ fun Application.module() {
configureDatabase() configureDatabase()
configureSecurity()
configureRouting()
configureSerialization() configureSerialization()
configureValidation() configureValidation()
configureException() configureException()
configureSecurity()
configureRouting()
} }

View File

@@ -8,12 +8,6 @@ enum class Role(
children = listOf(USER), children = listOf(USER),
); );
fun hasChild(
role: Role,
): Boolean {
return children.contains(role)
}
companion object { companion object {
val ALL = entries.toList() val ALL = entries.toList()
} }

View File

@@ -26,8 +26,7 @@ import com.infendro.account.model.repository.SessionRepository
import com.infendro.account.util.Hasher import com.infendro.account.util.Hasher
import com.infendro.account.util.OTP import com.infendro.account.util.OTP
import com.infendro.account.util.SecureHasher import com.infendro.account.util.SecureHasher
import io.ktor.server.sessions.CurrentSession import io.ktor.server.sessions.*
import io.ktor.server.sessions.clear
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
class AccountService( class AccountService(
@@ -161,7 +160,7 @@ class AccountService(
.singleOrNull { AccountTable.id eq id } .singleOrNull { AccountTable.id eq id }
?: throw NotFoundException() ?: throw NotFoundException()
if (!principal.role.hasChild(account.role)) if (account.role !in principal.role.children)
throw ForbiddenException() throw ForbiddenException()
accountRepository.delete(account) accountRepository.delete(account)
@@ -176,10 +175,10 @@ class AccountService(
.singleOrNull { AccountTable.id eq id } .singleOrNull { AccountTable.id eq id }
?: throw NotFoundException() ?: throw NotFoundException()
if (!principal.role.hasChild(account.role)) if (account.role !in principal.role.children)
throw ForbiddenException() throw ForbiddenException()
if (!principal.role.hasChild(request.role)) if (request.role !in principal.role.children)
throw ForbiddenException() throw ForbiddenException()
accountRepository.update(account) { accountRepository.update(account) {

View File

@@ -20,8 +20,7 @@ object SecureHasher {
} }
fun generateSalt(): String { fun generateSalt(): String {
return ByteArray(16) return random.nextBytes(16)
.also { random.nextBytes(it) }
.let { Hex.encode(it).decodeToString() } .let { Hex.encode(it).decodeToString() }
} }
} }

View File

@@ -7,8 +7,7 @@ object TokenGenerator {
private val random = PRNG.HMAC private val random = PRNG.HMAC
fun generate(): String { fun generate(): String {
val bytes = ByteArray(32) return random.nextBytes(32)
.also { random.nextBytes(it) } .let { Hex.encode(it).decodeToString() }
return Hex.encode(bytes).toString()
} }
} }