Warden
A Ktor plugin that ensures that all your routes are protected by Warden, or explicitly exempt from authorization.
If a route responds successfully and no EnforcementPointKtor was invoked for the call, and the call was not ignored inline or by WardenConfiguration.routePriorityStack, the response is replaced with a 403.
Example installation:
install(Warden)
install(StatusPages) {
exception { call, cause ->
call.respondText("Not Authorized", status = HttpStatusCode.Forbidden)
}
} Content copied to clipboard
Once installed, you must then use the EnforcementPointKtor in each of your routes, or ignore the route.
Example routing:
routing {
warded {
route("/authorizationNotEnforced") {
get("") {
call.respondText("You should not be able to get me, you did not enforce authorization")
}
get("/Ignored") {
unwarded {
call.respondText("You should see me")
}
}
}
get("/authorizationEnforced") {
val accessRequest: AccessRequest = // build an access request
enforcementPointKtor.enforceAuthorization(accessRequest)
call.respondText("You should see me if you are authorized")
}
}
}Content copied to clipboard