-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom auth on route #34
Comments
I have checked above issues but not able to understand how to implement it for my custom auth provider (it is like digest) and how to use it with routes inside apiRouting. That is to use it with all routes individually or can use it like above? |
To use it like you want up there you would:
private lass CustomAuthProvider(scopes: List<T>) : AuthProvider<A> {
override suspend fun getAuth(pipeline: PipelineContext<Unit, ApplicationCall>): A {
// return the object that represents an authenticated user, like the ktor authentication()
}
// use the ktor authentication on the route, the throw is optional but recommended if you want to handle errors properly
override fun apply(route: NormalOpenAPIRoute): OpenAPIAuthenticatedRoute<A> =
OpenAPIAuthenticatedRoute(route.ktorRoute.authenticate(authName) {}, route.provider.child(), this).throws(
APIException.apiException<BadPrincipalException>(HttpStatusCode.Unauthorized)
)
// tell OpenAPI how to generate the security definition you implemented
override val security: Iterable<Iterable<AuthProvider.Security<*>>> =
listOf(listOf(AuthProvider.Security(scheme, scopes)))
}
inline fun<T> NormalOpenAPIRoute.auth(provider: AuthProvider<T>, crossinline route: OpenAPIAuthenticatedRoute<T>.()->Unit = {}): OpenAPIAuthenticatedRoute<T> {
return provider.apply(this).apply {
route()
}
}
val authProvider = CustomAuthProvider() // you could also have the authProvider as an object instead
fun Routing.userRoute() {
apiRouting {
auth(authProvider) { // use it wherever you have a non authenticated route to create an authenticated one
route("users", Tags.USER) {
route("agent").get<Unit, UserAgentResponse>(
info("Get Agent", "Get agent used to get images from server"),
example = UserAgentResponse("qwerty")
) {
respond(UserAgentResponse(Helper.userAgent))
}
}
}
}
} |
It worked. Testing it now. |
Good afternoon, you can please a more detailed example of implementation, unfortunately I do not understand how to implement "getAuth" and "security" for jwt ? |
I am trying to do custom token based authentication on whole route but using ktor authentication is not working. How to add authentication method for openapi?
The text was updated successfully, but these errors were encountered: