Does anyone know how to add an intercepter after t...
# ktor
b
Does anyone know how to add an intercepter after the authentication feature? Need it for an custom Authorization feature
u
I made a custom authentication provider for that
but not sure its the best solution
b
Could you share an example?
u
Untitled_kt.kt
based on the BasicAuthenticationProvider
but for reading the info from a cookie
example_kt.kt
and thats the usage code
b
thx
I ended up modifying the pricipal in the
validate
function:
Copy code
install(Authentication) {
        jwt {
            verifier(jwkProvider, jwkIssuer)
            realm = jwkRealm
            validate { credentials ->
                if (credentials.payload.audience.contains(jwkAudience)) {
                    val account = httpClient.get<Account>(accountEndpoint) {
                        header("Authorization", request.header("Authorization"))
                    }

                    KKPrincipal(credentials.payload, account)
                } else null
            }
        }
    }
s
@Uriel Salischiker why not to use Sessions plus Authentication features to have cookie auth out-of-box:
Copy code
install(Sessions) {
    cookie<CookieClass>("CookieName") {
        // configure cookie provider: path, httpOnly, secure and so on
    }
}

install(Authentication) {
    session<CookieClass>("AuthorizationName") {
        challenge {
            appendWWWAuthenticateHeader(call)
            call.respond(HttpStatusCode.Unauthorized)
        }
        validate {
            // it here points to you CookieClass - extract data and may be call db to find user 
        }
    }
}
u
Ohh, tgat works when the cookie is not a session cookie but contains a url encoded string which then contains a access token?
@Sergey Akhapkin
s
I don't sure I completely understood your question, but I don't see any restrictions to a content of cookie. When cookie provider is configured, you need to provide serialize/deserialize methods from/to CookieClass to/from String.
Copy code
fun CookieSessionBuilder<CookieClass>.init() {
    ...
    serializer = object : SessionSerializer {
        override fun deserialize(text: String): Any = ...
        override fun serialize(session: Any): String = ...
    }
}