https://kotlinlang.org logo
#ktor
Title
# ktor
j

Jonas Trollvik

07/04/2022, 11:49 AM
Hi, Does anyone have a clue if it's possible to retrieve client principal when using the StatusPage plugin? The AuthContext does not seem to exist in call.attributes
a

Aleksei Tirman [JB]

07/04/2022, 1:31 PM
Could you please describe your problem in more detail?
j

Jonas Trollvik

07/04/2022, 1:51 PM
Currently I would like to present different pages for different roles when serving a 404 page. Code for setting up StatusPages and securing routes are shown here
Copy code
install(StatusPages) {
    status(HttpStatusCode.NotFound) { call, status ->
        val principal = call.principal<JWTPrincipal>() // Always null here

        // Also tried to find the context like this: Key is never found
        val attributeKey = call.attributes.allKeys.find { attr -> attr.name == "AuthContext" }
    }
}

routing {
    authenticate("auth-jwt") {
        // Routes here
    }
}
Might be because the routes are given a principal but at the point of the 404 page the context will already be dropped?
However I could not find any documentation on how to set up an authentication block outside the routing block
a

Aleksei Tirman [JB]

07/04/2022, 3:56 PM
So the scenario is that a user sends a credentials to a nonexistent route and you want to authenticate him and customize the 404 page?
j

Jonas Trollvik

07/04/2022, 4:38 PM
Well the credentials are already sent using a jwt header
But I would like to show custom links depending on the user permissions
Using a common HTML header for the whole app which depends on the principal being present
I guess it doesn't matter which form of authentication you're using, cookies, basic auth etc, the principal wouldn't be there in the statuspage handler
a

Aleksei Tirman [JB]

07/05/2022, 9:02 AM
You have to authenticate a user first to get a principal and I don’t see a way of doing in the StatusPages’s handler because the API that allows to trigger an authentication process is internal.
j

Jonas Trollvik

07/05/2022, 1:47 PM
Ah okey, so the auth plugin is tied to the routing plugin pretty much then. It would be nice if it could be used with other plugins as well.
2 Views