Skip to content

Commit 16a31f3

Browse files
committed
feat(authorization): option to install status pages
1 parent 35a5518 commit 16a31f3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

authorization/src/main/kotlin/org/modelix/authorization/AuthorizationConfig.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ interface IModelixAuthorizationConfig {
3333
*/
3434
var debugEndpointsEnabled: Boolean
3535

36+
/**
37+
* NotLoggedInException and NoPermissionException will be turned into HTTP status codes 401 and 403
38+
*/
39+
var installStatusPages: Boolean
40+
3641
/**
3742
* The pre-shared key for the HMAC512 signature algorithm.
3843
* The environment variables MODELIX_JWT_SIGNATURE_HMAC512_KEY or MODELIX_JWT_SIGNATURE_HMAC512_KEY_FILE can be
@@ -94,6 +99,7 @@ class ModelixAuthorizationConfig : IModelixAuthorizationConfig {
9499
override var permissionChecksEnabled: Boolean? = PERMISSION_CHECKS_ENABLED
95100
override var generateFakeTokens: Boolean? = getBooleanFromEnv("MODELIX_GENERATE_FAKE_JWT")
96101
override var debugEndpointsEnabled: Boolean = true
102+
override var installStatusPages: Boolean = false
97103
override var hmac512Key: String? = null
98104
override var hmac384Key: String? = null
99105
override var hmac256Key: String? = null

authorization/src/main/kotlin/org/modelix/authorization/AuthorizationPlugin.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.ktor.server.auth.jwt.jwt
2323
import io.ktor.server.auth.principal
2424
import io.ktor.server.html.respondHtml
2525
import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders
26+
import io.ktor.server.plugins.statuspages.StatusPages
2627
import io.ktor.server.response.respond
2728
import io.ktor.server.response.respondText
2829
import io.ktor.server.routing.Route
@@ -98,6 +99,17 @@ object ModelixAuthorization : BaseRouteScopedPlugin<IModelixAuthorizationConfig,
9899
}
99100
}
100101

102+
if (config.installStatusPages) {
103+
application.install(StatusPages) {
104+
exception<NotLoggedInException> { call, cause ->
105+
call.respondText(text = "401: ${cause.message}", status = HttpStatusCode.Unauthorized)
106+
}
107+
exception<NoPermissionException> { call, cause ->
108+
call.respondText(text = "403: ${cause.message}", status = HttpStatusCode.Forbidden)
109+
}
110+
}
111+
}
112+
101113
if (config.debugEndpointsEnabled) {
102114
application.routing {
103115
authenticate(MODELIX_JWT_AUTH) {

model-server/src/main/kotlin/org/modelix/model/server/Main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ object Main {
176176
install(Routing)
177177
install(ModelixAuthorization) {
178178
permissionSchema = ModelServerPermissionSchema.SCHEMA
179+
installStatusPages = false
179180
}
180181
install(ForwardedHeaders)
181182
install(CallLogging) {

0 commit comments

Comments
 (0)