Skip to content

Commit

Permalink
feat(authorization): option to install status pages
Browse files Browse the repository at this point in the history
  • Loading branch information
slisson committed Nov 20, 2024
1 parent aa38e90 commit b2bdfd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ interface IModelixAuthorizationConfig {
*/
var debugEndpointsEnabled: Boolean

/**
* NotLoggedInException and NoPermissionException will be turned into HTTP status codes 401 and 403
*/
var installStatusPages: Boolean

/**
* The pre-shared key for the HMAC512 signature algorithm.
* The environment variables MODELIX_JWT_SIGNATURE_HMAC512_KEY or MODELIX_JWT_SIGNATURE_HMAC512_KEY_FILE can be
Expand Down Expand Up @@ -109,6 +114,7 @@ class ModelixAuthorizationConfig : IModelixAuthorizationConfig {
override var permissionChecksEnabled: Boolean? = PERMISSION_CHECKS_ENABLED
override var generateFakeTokens: Boolean? = getBooleanFromEnv("MODELIX_GENERATE_FAKE_JWT")
override var debugEndpointsEnabled: Boolean = true
override var installStatusPages: Boolean = false
override var hmac512Key: String? = null
override var hmac384Key: String? = null
override var hmac256Key: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import io.ktor.server.auth.jwt.jwt
import io.ktor.server.auth.principal
import io.ktor.server.html.respondHtml
import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
Expand Down Expand Up @@ -112,6 +113,17 @@ object ModelixAuthorization : BaseRouteScopedPlugin<IModelixAuthorizationConfig,
}
}

if (config.installStatusPages) {
application.install(StatusPages) {
exception<NotLoggedInException> { call, cause ->
call.respondText(text = "401: ${cause.message}", status = HttpStatusCode.Unauthorized)
}
exception<NoPermissionException> { call, cause ->
call.respondText(text = "403: ${cause.message}", status = HttpStatusCode.Forbidden)
}
}
}

if (config.debugEndpointsEnabled) {
application.routing {
authenticate(MODELIX_JWT_AUTH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ object Main {
install(Routing)
install(ModelixAuthorization) {
permissionSchema = ModelServerPermissionSchema.SCHEMA
installStatusPages = false
}
install(ForwardedHeaders)
install(CallLogging) {
Expand Down

0 comments on commit b2bdfd3

Please sign in to comment.