@@ -23,6 +23,7 @@ import io.ktor.server.request.*
23
23
import io.ktor.server.response.*
24
24
import io.ktor.server.sessions.*
25
25
import io.ktor.util.pipeline.*
26
+ import kotlinx.serialization.json.JsonObject
26
27
import kotlinx.serialization.json.JsonPrimitive
27
28
import kotlinx.serialization.json.buildJsonObject
28
29
import kotlinx.uuid.UUID
@@ -66,21 +67,18 @@ fun Application.configureSecurity() {
66
67
67
68
install(Authentication ) {
68
69
69
- bearer {
70
- bearer(" authenticated-bearer" ) {
71
- authenticate { tokenCredential ->
72
- if (securityUserTokenMapping.contains(tokenCredential.token)) {
73
- UserIdPrincipal (securityUserTokenMapping[tokenCredential.token].toString())
74
- } else {
75
- null
76
- }
70
+ bearer(" authenticated-bearer" ) {
71
+ authenticate { tokenCredential ->
72
+ if (securityUserTokenMapping.contains(tokenCredential.token)) {
73
+ UserIdPrincipal (securityUserTokenMapping[tokenCredential.token].toString())
74
+ } else {
75
+ null
77
76
}
78
77
}
79
78
}
80
79
81
80
session<LoginTokenSession >(" authenticated-session" ) {
82
81
validate { session ->
83
- // println("Validating: $session, [$securityUserTokenMapping]")
84
82
if (securityUserTokenMapping.contains(session.token)) {
85
83
UserIdPrincipal (securityUserTokenMapping[session.token].toString())
86
84
} else {
@@ -90,7 +88,13 @@ fun Application.configureSecurity() {
90
88
}
91
89
92
90
challenge {
93
- call.respond(HttpStatusCode .Unauthorized , " Login to continue." )
91
+ call.respond(
92
+ HttpStatusCode .Unauthorized , JsonObject (
93
+ mapOf (
94
+ " message" to JsonPrimitive (" Login Required" )
95
+ )
96
+ )
97
+ )
94
98
}
95
99
}
96
100
}
@@ -210,14 +214,14 @@ fun Application.auth() {
210
214
}
211
215
}
212
216
213
-
214
- fun PipelineContext <Unit , ApplicationCall >.getUserId () = call.principal<UserIdPrincipal >(" authenticated-session" )
215
- ? : call.principal<UserIdPrincipal >(" authenticated-bearer" )
216
- ? : throw UnauthorizedException (" Could not retrieve authorized user." )
217
+ fun PipelineContext <Unit , ApplicationCall >.getUserId () =
218
+ call.principal<UserIdPrincipal >(" authenticated-session" )
219
+ ? : call.principal<UserIdPrincipal >(" authenticated-bearer" )
220
+ ? : call.principal<UserIdPrincipal >() // bearer is registered with no name for some reason
221
+ ? : throw UnauthorizedException (" Could not find user authorization within request." )
217
222
218
223
fun PipelineContext <Unit , ApplicationCall >.getUserUUID () =
219
- runCatching { UUID (getUserId().name) }
220
- .getOrNull() ? : throw IllegalArgumentException (" Invalid user id" )
224
+ runCatching { UUID (getUserId().name) }.getOrElse { throw IllegalArgumentException (" Invalid user id: $it " ) }
221
225
222
226
fun PipelineContext <Unit , ApplicationCall >.getWalletId () =
223
227
runCatching {
@@ -231,8 +235,8 @@ fun PipelineContext<Unit, ApplicationCall>.getWalletService() =
231
235
WalletServiceManager .getWalletService(getUserUUID(), getWalletId())
232
236
233
237
fun PipelineContext <Unit , ApplicationCall >.getUsersSessionToken (): String? =
234
- call.sessions.get(LoginTokenSession ::class )?.token ? : call.request.authorization()
235
- ?.removePrefix(" Bearer " )
238
+ call.sessions.get(LoginTokenSession ::class )?.token
239
+ ? : call.request.authorization()? .removePrefix(" Bearer " )
236
240
237
241
fun getNftService () = WalletServiceManager .getNftService()
238
242
0 commit comments