Skip to content
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

Bubble errors up from token fetch #232

Merged
merged 5 commits into from
Oct 9, 2017
Merged

Bubble errors up from token fetch #232

merged 5 commits into from
Oct 9, 2017

Conversation

dhardiman
Copy link
Contributor

I'm not sure if this is a pull request you'll want, as I'm not sure whether it's entirely correct behaviour, but I've noticed if a refresh token request gets a networking error, timeout, connection failure etc, then the OAuth mechanism tries to request new credentials rather than just notifying the client of an error. In my app that led us to erroneously log the user out rather than just ignore the error and wait for better network conditions.

This prevents connection errors and timeouts from trying to reauthenticate
@p2
Copy link
Owner

p2 commented Oct 8, 2017

Thanks, that's probably a good idea! I think it would be neater though if the callback would be:

if successParams:
    didAuthorize
else if error:
    didFail
else:
    registerIfNeeded

(comments inline)

Would that be an ok change?

@@ -106,11 +106,16 @@ open class OAuth2: OAuth2Base {

didAuthorizeOrFail = callback
logger?.debug("OAuth2", msg: "Starting authorization")
tryToObtainAccessTokenIfNeeded(params: params) { successParams in
tryToObtainAccessTokenIfNeeded(params: params) { successParams, error in
if let successParams = successParams {
self.didAuthorize(withParameters: successParams)
}
else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add else if let error = error here (without the case .nsError(_) part – I don't think that's needed).

if let successParams = successParams {
self.didAuthorize(withParameters: successParams)
}
else {
self.logger?.debug("OAuth2", msg: "Error obtaining token \(String(describing: error))")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryToObtainAccessTokenIfNeeded already logs the error, so this here will double-log the same error. I'd remove this one or use it to replace the less verbose one on line 194 (now 199) – could say "error refreshing token".

Appears we could have no error and no success params, so
makes sense to check each separately
@dhardiman
Copy link
Contributor Author

Comments addressed. Hopefully this makes more sense now 😄

Copy link
Owner

@p2 p2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes a lot of sense! Now just fix the spaces and also add yourself to the top of CONTRIBUTORS.md! (read CONTRIBUTING.md for details).

@@ -109,13 +109,11 @@ open class OAuth2: OAuth2Base {
tryToObtainAccessTokenIfNeeded(params: params) { successParams, error in
if let successParams = successParams {
self.didAuthorize(withParameters: successParams)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs here

@@ -196,7 +194,7 @@ open class OAuth2: OAuth2Base {
}
else {
if let err = error {
self.logger?.debug("OAuth2", msg: "\(err)")
self.logger?.debug("OAuth2", msg: "Error refreshing token: \(err)")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use tabs instead of spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ug, sorry, Xcode defaults kicking in. In our internal projects, we've got swift format as a build phase and I get used to not checking formatting :)

@p2
Copy link
Owner

p2 commented Oct 9, 2017

Perfect, thanks!!

@p2 p2 merged commit 056a5f9 into p2:master Oct 9, 2017
@KimKellerRasmussen
Copy link

Just downloaded the code... Did a forgetTokens to test my login flow. When I try to login using authorizeEmbedded I just get; "I don't have a refresh token, not trying to refresh" and no embedded login page. Rolledback to the previous version and everything worked again...

@p2
Copy link
Owner

p2 commented Oct 10, 2017

Ah crap of course! @dhardiman can you add a check in OAuth2.swift line 197 and 199 to ignore the OAuth2Error.noRefreshToken error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants