1
- package id.walt.web
1
+ package id.walt.web.controllers
2
2
3
3
// import id.walt.web.model.LoginRequestJson
4
+ import id.walt.db.models.AccountWalletMappings
5
+ import id.walt.db.models.AccountWalletPermissions
4
6
import id.walt.service.WalletServiceManager
5
7
import id.walt.service.account.AccountsService
6
8
import id.walt.utils.RandomUtils
9
+ import id.walt.web.InsufficientPermissionsException
10
+ import id.walt.web.UnauthorizedException
11
+ import id.walt.web.WebBaseRoutes.webWalletRoute
7
12
import id.walt.web.model.AccountRequest
8
13
import id.walt.web.model.EmailAccountRequest
9
14
import id.walt.web.model.LoginRequestJson
@@ -16,12 +21,15 @@ import io.ktor.server.application.*
16
21
import io.ktor.server.auth.*
17
22
import io.ktor.server.request.*
18
23
import io.ktor.server.response.*
19
- import io.ktor.server.routing.*
20
24
import io.ktor.server.sessions.*
21
25
import io.ktor.util.pipeline.*
22
26
import kotlinx.serialization.json.JsonPrimitive
23
27
import kotlinx.serialization.json.buildJsonObject
24
28
import kotlinx.uuid.UUID
29
+ import kotlinx.uuid.toJavaUUID
30
+ import org.jetbrains.exposed.sql.and
31
+ import org.jetbrains.exposed.sql.select
32
+ import org.jetbrains.exposed.sql.transactions.transaction
25
33
import kotlin.collections.set
26
34
import kotlin.time.Duration.Companion.days
27
35
@@ -93,8 +101,8 @@ val securityUserTokenMapping = HashMap<String, UUID>() // Token -> UUID
93
101
94
102
95
103
fun Application.auth () {
96
- routing {
97
- route(" r/ auth" , {
104
+ webWalletRoute {
105
+ route(" auth" , {
98
106
tags = listOf (" Authentication" )
99
107
}) {
100
108
post(" login" , {
@@ -227,3 +235,24 @@ fun PipelineContext<Unit, ApplicationCall>.getUsersSessionToken(): String? =
227
235
?.removePrefix(" Bearer " )
228
236
229
237
fun getNftService () = WalletServiceManager .getNftService()
238
+
239
+ fun PipelineContext <Unit , ApplicationCall >.ensurePermissionsForWallet (required : AccountWalletPermissions ): Boolean {
240
+ val userId = getUserUUID().toJavaUUID()
241
+ val walletId = getWalletId().toJavaUUID()
242
+
243
+ val permissions = transaction {
244
+ (AccountWalletMappings .select { (AccountWalletMappings .account eq userId) and (AccountWalletMappings .wallet eq walletId) }
245
+ .firstOrNull()
246
+ ? : throw UnauthorizedException (" This account does not have access to the specified wallet." )
247
+ )[AccountWalletMappings .permissions]
248
+ }
249
+
250
+ if (permissions.power >= required.power) {
251
+ return true
252
+ } else {
253
+ throw InsufficientPermissionsException (
254
+ minimumRequired = required,
255
+ current = permissions
256
+ )
257
+ }
258
+ }
0 commit comments