Hi All, I'm trying to write a helper to authenticate WebSockets - similar to how the HTTP authentication feature won't call my route until the user has authenticated but for web sockets.
I've created a feature into which I'll bundle my authentication logic (wait for the client to send an authenticate command with a JWT token, validate it and store the principle) and created the function below. I have a couple of questions about this approach:
1. Is this a good approach or is there a better way to achieve this?
2. Is there a way to re-use the JWT validation logic from
ktor-auth-jwt
? It all appears to be private.
fun Route.authenticatedWebSocket(protocol: String? = null, handler: suspend DefaultWebSocketServerSession.() -> Unit){
webSocket(protocol) {
if(application.feature(MercuryWebSocketAuthenticationFeature.Feature).authenticateWebSocket(this)){
handler.invoke(this)
}
}
}
Cheers