-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KTOR-7644 Make re-auth status codes configurable #4420
base: 3.1.0-eap
Are you sure you want to change the base?
Conversation
90a256c
to
d6e7166
Compare
a65ff10
to
9ae3d49
Compare
d6e7166
to
2034352
Compare
Some services use 403 instead of 401. Changing them might be impossible. With this change Ktor can flexibly work with any broken service.
2034352
to
5f0ca66
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pull request! Seems useful to me
Let's discuss an alternative approach before we merge this.
if (origin.request.attributes.contains(AuthCircuitBreaker)) return@on origin | ||
|
||
var call = origin | ||
|
||
val candidateProviders = HashSet(providers) | ||
|
||
while (call.response.status == HttpStatusCode.Unauthorized) { | ||
while (call.response.status in pluginConfig.reAuthStatusCodes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking of broken services, I've seen some APIs returning 200 OK
with actual status code in a body. We could provide a lambda instead of a list of statuses:
var isUnauthorized: (HttpResponse) -> Boolean = { it.status == HttpStatusCode.Unauthorized }
@e5l, @bjhham, @marychatte, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds much better than the list. I've pushed that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@osipxd
I like the idea with lambda!
returning 200 OK with actual status code in a body
Do you mean it could be used like this?
isUnauthorized = { it.bodyAsText() == "401" }
If yes, then isUnauthorized
function should be suspend
ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you!
@bjhham, @marychatte, could you also take a look?
* [providers] - list of auth providers to use. | ||
* [isUnauthorized] - lambda function to control whether a response is unauthorized and should trigger a re-auth. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something else: this refers to variables that are actually defined on AuthConfig. Shouldn't the docstring be defined there instead or at least refer to the full path [AuthConfig.providers]
? I just copied the existing code, but maybe this should be fixed, too.
Thanks for the PR! Can we also please add tests for it? |
Sure, added one and switched to a suspend lambda. |
Some services use 403 instead of 401. Changing them might be impossible. With this change Ktor can flexibly work with any broken service.