-
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
Basic authorization #95
Comments
Not sure if the output of /openapi.json is also helpful:
|
maybe you need to inherit the route provider as well like this: OpenAPIAuthenticatedRoute(this.ktorRoute.authenticate(authName) {}, this.provider.child(), this).throws(
APIException.apiException<BadPrincipalException>(HttpStatusCode.Unauthorized)
) Or use a named authenication. you could also change |
Ah wait: you used |
Hi @Wicpar , thanks for your quick reply!
Yeah, because I'm inside AuthProvider and don't have this.ktorRoute there, right?
Not 100% sure I already tried these, but I'll double check it in the office tomorrow. |
So, unfortunately neither the named authentication nor the inherited provider did change anything. Also changing A last suggestion that came to my mind is regarding the Ktor version. I saw you use 1.3.2 (https://github.com/papsign/Ktor-OpenAPI-Generator/blob/master/gradle.properties). I'm on 1.5.2. Are there any known incompatibilities? |
Not to my knowledge |
I have it setup like this: class OAuth2Provider(scopes: List<T>) : AuthProvider<A> {
override suspend fun getAuth(pipeline: PipelineContext<Unit, ApplicationCall>): A =
this@OAuth2Handler.getAuth(pipeline.call.principal()!!)
override fun apply(route: NormalOpenAPIRoute): OpenAPIAuthenticatedRoute<A> =
OpenAPIAuthenticatedRoute(route.ktorRoute.authenticate(authName) {}, route.provider.child(), this).throws(
APIException.apiException<BadPrincipalException>(HttpStatusCode.Unauthorized)
)
override val security: Iterable<Iterable<AuthProvider.Security<*>>> =
listOf(listOf(AuthProvider.Security(scheme, scopes)))
}
fun auth(apiRoute: NormalOpenAPIRoute, scopes: List<T>): OpenAPIAuthenticatedRoute<A> {
val authProvider = OAuth2Provider(scopes)
return authProvider.apply(apiRoute)
} |
Found the error! It wasn't on the side of implementing AuthHandler, but on the usage side. Don't want to be too harsh here, but that's kind of bad API or at least really easy to mess up. So the issue was, the Import of the get method still pointed to normal package instead of auth. I would have expected an compile error, because inside the auth-Block this points to OpenAPIAuthenticatedRoute and not to NormalOpenAPIRoute. Please don't get me wrong, you're still doing a great job and I like the project very much! Thank you very much for your quick feedback and help. I will now see that I implement my usecases, but I think nothing stands in the way now. :) Edit: Of course the third generic parameter was also missing, but especially if you have normal and auth routes in one file, you need both imports and I bet at leat I will mess it up :D Using explicit |
Got some last minor questions, not sure if I may open separate issues for them, please just let me know:
Code :
I would have expected the default values of created ResponseError instanced for example parameters to be present in the UI as well.
EDIT: Added openapi.json
|
Ah yes Indeed... For your questions: |
Do you have any feedback on 3)? |
Didn't see 3 you can simply edit the description in the status code class. 1 seems like a regression, i'll look into it |
@christiangroth 2 works properly when i copy your code, the one difference is that i don' t have a |
@christiangroth what is your jackson configuration for the server ? install(io.ktor.features.ContentNegotiation) {
jackson {
enable(
com.fasterxml.jackson.databind.DeserializationFeature.WRAP_EXCEPTIONS,
com.fasterxml.jackson.databind.DeserializationFeature.USE_BIG_INTEGER_FOR_INTS,
com.fasterxml.jackson.databind.DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS
)
enable(com.fasterxml.jackson.databind.SerializationFeature.WRAP_EXCEPTIONS, com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT)
setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
setDefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter().apply {
indentArraysWith(com.fasterxml.jackson.core.util.DefaultPrettyPrinter.FixedSpaceIndenter.instance)
indentObjectsWith(com.fasterxml.jackson.core.util.DefaultIndenter(" ", "\n"))
})
registerModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule())
}
} |
We don't use Jackson, but kotlinx-serialization instead. I took the code snippet from #42 to manage DataModel serialization. |
It looks like the origin of your issue, you'll have to debug that on your own as i know nothing of kotlinx/serialization. |
Yeah, I also thought that. I'll try to finde some time next week and come back to you / keep this issue updated. |
So I solved 2 and 3 by fixing my models. The DataModel inheritance was missing, so obviously I did not work for custom types. If you have any updates on the possible regression regarding 1) within the next days, that would be fine. Thx :) |
@christiangroth hey, i am also trying to figure out this issue. Is it possible to provide your full example? Thanks |
@hmmeral I'm not sure what you're missing, so I just copy the complete code again (just removed some internal details) :) Hope that helps. Definition side:
And the usage side:
|
Hi,
I'm trying to get a basic auth to work, unfortunately I'm not successful. Here is what I've done so far:
Installed Ktor Authorization feature:
Implemented my AuthProvider:
Implemented shortcut extension function for routing definitions:
Enhanced an existing route to use basic auth:
The result is, that my /version route exists, is listed in swagger-ui / openapi.json but is not authenticated. Also I don't get auth information in swagger-ui / openapi.json. I'm quite sure I'm close, but I don't get what I'm missing right now? Docs and examples are not that helpful at that point, neither existing issues ... or I just don't get it.
Thanks for your help, Chris
The text was updated successfully, but these errors were encountered: