Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 3e287d8

Browse files
committed
Update exceptions and generate encryption/sign key
1 parent c612464 commit 3e287d8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/main/kotlin/id/walt/web/Exceptions.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import kotlinx.serialization.SerialName
77
sealed class WebException(val status: HttpStatusCode, message: String) : Exception(message)
88

99
class UnauthorizedException(message: String) : WebException(HttpStatusCode.Unauthorized, message)
10+
class ForbiddenException(message: String) : WebException(HttpStatusCode.Forbidden, message)
1011

1112
@SerialName("InsufficientPermissions")
1213
class InsufficientPermissionsException(
1314
minimumRequired: AccountWalletPermissions,
1415
current: AccountWalletPermissions,
15-
) : WebException(HttpStatusCode.Unauthorized, "You do not have enough permissions to access this action. Minimum required permissions: $minimumRequired, your current permissions: $current")
16+
) : WebException(HttpStatusCode.Forbidden, "You do not have enough permissions to access this action. Minimum required permissions: $minimumRequired, your current permissions: $current")

src/main/kotlin/id/walt/web/controllers/AuthController.kt

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import id.walt.db.models.AccountWalletPermissions
66
import id.walt.service.WalletServiceManager
77
import id.walt.service.account.AccountsService
88
import id.walt.utils.RandomUtils
9+
import id.walt.web.ForbiddenException
910
import id.walt.web.InsufficientPermissionsException
1011
import id.walt.web.UnauthorizedException
1112
import id.walt.web.WebBaseRoutes.webWalletRoute
@@ -26,6 +27,7 @@ import io.ktor.util.pipeline.*
2627
import kotlinx.serialization.json.JsonObject
2728
import kotlinx.serialization.json.JsonPrimitive
2829
import kotlinx.serialization.json.buildJsonObject
30+
import kotlinx.uuid.SecureRandom
2931
import kotlinx.uuid.UUID
3032
import kotlinx.uuid.toJavaUUID
3133
import org.jetbrains.exposed.sql.and
@@ -47,12 +49,17 @@ fun generateToken() = RandomUtils.randomBase64UrlString(256)
4749

4850
data class LoginTokenSession(val token: String) : Principal
4951

52+
object AuthKeys {
53+
private val secureRandom = SecureRandom
54+
55+
// TODO make statically configurable for HA deployments
56+
val encryptionKey = secureRandom.nextBytes(16)
57+
val signKey = secureRandom.nextBytes(16)
58+
}
59+
5060
fun Application.configureSecurity() {
5161

5262
install(Sessions) {
53-
val encryptionKey = "uv4phoozeefoom7l".toByteArray()
54-
val signKey = "faungeenah5aewiL".toByteArray()
55-
5663
cookie<LoginTokenSession>("login") {
5764
//cookie.encoding = CookieEncoding.BASE64_ENCODING
5865

@@ -61,7 +68,7 @@ fun Application.configureSecurity() {
6168
// TODO cookie.secure = true
6269
cookie.maxAge = 1.days
6370
cookie.extensions["SameSite"] = "Strict"
64-
transform(SessionTransportTransformerEncrypt(encryptionKey, signKey))
71+
transform(SessionTransportTransformerEncrypt(AuthKeys.encryptionKey, AuthKeys.signKey))
6572
}
6673
}
6774

@@ -247,7 +254,7 @@ fun PipelineContext<Unit, ApplicationCall>.ensurePermissionsForWallet(required:
247254
val permissions = transaction {
248255
(AccountWalletMappings.select { (AccountWalletMappings.account eq userId) and (AccountWalletMappings.wallet eq walletId) }
249256
.firstOrNull()
250-
?: throw UnauthorizedException("This account does not have access to the specified wallet.")
257+
?: throw ForbiddenException("This account does not have access to the specified wallet.")
251258
)[AccountWalletMappings.permissions]
252259
}
253260

0 commit comments

Comments
 (0)