small refactor
This commit is contained in:
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
@@ -6,4 +6,5 @@
|
|||||||
/gradle.properties
|
/gradle.properties
|
||||||
|
|
||||||
# Kotlin
|
# Kotlin
|
||||||
|
/.kotlin/
|
||||||
/build/
|
/build/
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ fun Application.module() {
|
|||||||
|
|
||||||
configureDatabase()
|
configureDatabase()
|
||||||
|
|
||||||
configureSecurity()
|
|
||||||
configureRouting()
|
|
||||||
|
|
||||||
configureSerialization()
|
configureSerialization()
|
||||||
configureValidation()
|
configureValidation()
|
||||||
configureException()
|
configureException()
|
||||||
|
|
||||||
|
configureSecurity()
|
||||||
|
configureRouting()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user