-
Notifications
You must be signed in to change notification settings - Fork 364
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
[SDK-1739] Recover and logout when throwing invalid_grant on Refresh Token #668
Changes from 4 commits
2a15d74
66d77e2
b66672b
30af7d7
19a92c9
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 |
---|---|---|
|
@@ -35,7 +35,8 @@ import { | |
DEFAULT_SCOPE, | ||
RECOVERABLE_ERRORS, | ||
DEFAULT_SESSION_CHECK_EXPIRY_DAYS, | ||
DEFAULT_AUTH0_CLIENT | ||
DEFAULT_AUTH0_CLIENT, | ||
INVALID_REFRESH_TOKEN_ERROR_MESSAGE | ||
} from './constants'; | ||
|
||
import { | ||
|
@@ -830,46 +831,56 @@ export default class Auth0Client { | |
|
||
const timeout = | ||
options.timeoutInSeconds || this.options.authorizeTimeoutInSeconds; | ||
const codeResult = await runIframe(url, this.domainUrl, timeout); | ||
|
||
if (stateIn !== codeResult.state) { | ||
throw new Error('Invalid state'); | ||
} | ||
try { | ||
const codeResult = await runIframe(url, this.domainUrl, timeout); | ||
|
||
const { | ||
scope, | ||
audience, | ||
redirect_uri, | ||
ignoreCache, | ||
timeoutInSeconds, | ||
...customOptions | ||
} = options; | ||
if (stateIn !== codeResult.state) { | ||
throw new Error('Invalid state'); | ||
} | ||
|
||
const tokenResult = await oauthToken( | ||
{ | ||
...this.customOptions, | ||
...customOptions, | ||
const { | ||
scope, | ||
audience, | ||
baseUrl: this.domainUrl, | ||
client_id: this.options.client_id, | ||
code_verifier, | ||
code: codeResult.code, | ||
grant_type: 'authorization_code', | ||
redirect_uri: params.redirect_uri, | ||
auth0Client: this.options.auth0Client | ||
} as OAuthTokenOptions, | ||
this.worker | ||
); | ||
redirect_uri, | ||
ignoreCache, | ||
timeoutInSeconds, | ||
...customOptions | ||
} = options; | ||
|
||
const tokenResult = await oauthToken( | ||
{ | ||
...this.customOptions, | ||
...customOptions, | ||
scope, | ||
audience, | ||
baseUrl: this.domainUrl, | ||
client_id: this.options.client_id, | ||
code_verifier, | ||
code: codeResult.code, | ||
grant_type: 'authorization_code', | ||
redirect_uri: params.redirect_uri, | ||
auth0Client: this.options.auth0Client | ||
} as OAuthTokenOptions, | ||
this.worker | ||
); | ||
|
||
const decodedToken = this._verifyIdToken(tokenResult.id_token, nonceIn); | ||
const decodedToken = this._verifyIdToken(tokenResult.id_token, nonceIn); | ||
|
||
return { | ||
...tokenResult, | ||
decodedToken, | ||
scope: params.scope, | ||
audience: params.audience || 'default' | ||
}; | ||
return { | ||
...tokenResult, | ||
decodedToken, | ||
scope: params.scope, | ||
audience: params.audience || 'default' | ||
}; | ||
} catch (e) { | ||
if (e.error === 'login_required') { | ||
this.logout({ | ||
localOnly: true | ||
}); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
private async _getTokenUsingRefreshToken( | ||
|
@@ -939,6 +950,15 @@ export default class Auth0Client { | |
if (e.message === MISSING_REFRESH_TOKEN_ERROR_MESSAGE) { | ||
return await this._getTokenFromIFrame(options); | ||
} | ||
// A refresh token was found, but is it no longer valid. | ||
// Fallback to an iframe. | ||
if ( | ||
e.message && | ||
e.message.indexOf(INVALID_REFRESH_TOKEN_ERROR_MESSAGE) > -1 | ||
) { | ||
return await this._getTokenFromIFrame(options); | ||
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. This introduces an additional request in Safari where we know up front it will return I do not think the SDK is currently tracking which browsers can use iframe, nor do I think it should so I wonder what you think about this extra request. 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 it's fine to leave it, we don't do browser detection for this elsewhere and it will work for other browsers that also block third-party cookies. |
||
} | ||
|
||
throw e; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -503,6 +503,10 @@ <h3 class="mb-5">Other switches</h3> | |
} else { | ||
_self.error = e; | ||
} | ||
|
||
if (e.error === 'login_required') { | ||
_self.isAuthenticated = false; | ||
} | ||
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. This catches the |
||
}); | ||
}, | ||
getTokenPopup: function (audience, scope, access_tokens) { | ||
|
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.
Maybe we can unify this
if
block with the conditional above it, rather than having two if's? Feels like we could do it with an or expression.