-
Notifications
You must be signed in to change notification settings - Fork 2k
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
clear token on oauth2.0 error #315
Changes from all commits
49d5a51
954ac7a
72145e2
e675d17
c541a9a
a8ac232
9eea19c
d73cfac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
oauth2.0_error_codes <- c( | ||
400, | ||
401 | ||
) | ||
|
||
oauth2.0_errors <- c( | ||
"invalid_request", | ||
"invalid_client", | ||
"invalid_grant", | ||
"unauthorized_client", | ||
"unsupported_grant_type", | ||
"invalid_scope" | ||
) | ||
|
||
# This implements error checking according to the OAuth2.0 | ||
# specification: https://tools.ietf.org/html/rfc6749#section-5.2 | ||
known_oauth2.0_error <- function(response) { | ||
if (status_code(response) %in% oauth2.0_error_codes) { | ||
content <- content(response) | ||
if (content$error %in% oauth2.0_errors) { | ||
return(TRUE) | ||
} | ||
} | ||
FALSE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,9 +215,14 @@ Token2.0 <- R6::R6Class("Token2.0", inherit = Token, list( | |
!is.null(self$credentials$refresh_token) | ||
}, | ||
refresh = function() { | ||
self$credentials <- refresh_oauth2.0(self$endpoint, self$app, | ||
cred <- refresh_oauth2.0(self$endpoint, self$app, | ||
self$credentials, self$params$user_params) | ||
self$cache() | ||
if (is.null(cred)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the logic here could be made more clear. Maybe: creds <- refresh_oauth2.0(self$endpoint, self$app,
self$credentials, self$params$user_params)
if (is.null(creds) {
remove_cached_token(self)
warning("Unable refresh token", call. = FALSE)
} else {
self$credentials <- creds
self$cache()
}
self ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe the warning should occur in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think the warning should probably occur in |
||
remove_cached_token(self) | ||
} else { | ||
self$credentials <- cred | ||
self$cache() | ||
} | ||
self | ||
}, | ||
sign = function(method, url) { | ||
|
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.
I would rather this threw an error, to be consistent with the
stop_for_status()
belowThere 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.
But maybe you're trying to separate this into known errors and unknown errors? I'd like to hear your thinking about this.
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.
My thinking was that here in
refresh_oauth2.0
our goal is to get new, valid credentials using our refresh token (obviously), and OAuth2 provides a defined mechanism to inform us of that this can't happen - by returninginvalid_grant
in the error response.Separating this out from
stop_for_status
allows us to still catch any errors that occur usingstop_for_status
- 404s, redirects, internal server errors, etc. All of these are errors that prevent us from getting refreshed credentials, but there isn't any specific information in them that tells us what to do.Contrast that with the OAuth2 error(s) that tell us we have an invalid token, which is why our logic is to clear the token from our cache.
Taking it a step further, the OAuth2 spec is pretty clear that
invalid_grant
is the error we should look for in the case of the refresh token. I've currently written this in such a way thathas_oauth2.0_error
will returnTRUE
even in the case of OAuth2 errors that aren't specific to the refresh token.I did opt to cast a broader net, but there's an argument to be made that this function should be called
has_oauth2.0_refresh_token_error(response)
and only returnTRUE
wheninvalid_grant
is present.Thoughts?