Skip to content

Commit

Permalink
fix(authenticator): retry token refresh on error
Browse files Browse the repository at this point in the history
We retry the refresh if:
- The server returns any 500 status code
- The request does not resolve and an error is thrown because the network connection is broken
  • Loading branch information
velrest authored and kaldras committed Sep 6, 2019
1 parent f2ed636 commit 63cd8d3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ Prefix of the authentication token. Default is `Bearer`.
Name of the `login_hint` query paramter which is being forwarded to the authorization server if it is present.
This option allows overriding the default name `login_hint`.

**amountOfRetries** \<Number\> (optional)
Amount of retries should be made if the request to fetch a new token fails. Default is `3`.

**retryTimeout** \<Number\> (optional)
Timeout in milliseconds between each retry if a token refresh should fail. Default is `3000`.

## Contributing

### Installation
Expand Down
45 changes: 29 additions & 16 deletions addon/authenticators/oidc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import BaseAuthenticator from "ember-simple-auth/authenticators/base";
import { computed } from "@ember/object";
import { run } from "@ember/runloop";
import { later, cancel } from "@ember/runloop";
import { inject as service } from "@ember/service";
import RSVP from "rsvp";
import Configuration from "ember-simple-auth/configuration";
import config from "ember-simple-auth-oidc/config";
import getAbsoluteUrl from "ember-simple-auth-oidc/utils/absoluteUrl";
import { isServerError, isAbortError, isTimeoutError } from "ember-ajax/errors";

const {
host,
Expand All @@ -14,7 +15,9 @@ const {
clientId,
refreshLeeway,
authPrefix,
expiresIn
expiresIn,
amountOfRetries,
retryTimeout
} = config;

const getUrl = endpoint => `${getAbsoluteUrl(host)}${endpoint}`;
Expand Down Expand Up @@ -102,19 +105,29 @@ export default BaseAuthenticator.extend({
* @param {String} refresh_token The refresh token
* @returns {Object} The parsed response data
*/
async _refresh(refresh_token) {
let data = await this.get("ajax").post(getUrl(tokenEndpoint), {
responseType: "application/json",
contentType: "application/x-www-form-urlencoded",
data: {
refresh_token,
client_id: clientId,
grant_type: "refresh_token",
redirect_uri: this.redirectUri
async _refresh(refresh_token, retryCount = 0) {
try {
let data = await this.get("ajax").post(getUrl(tokenEndpoint), {
responseType: "application/json",
contentType: "application/x-www-form-urlencoded",
data: {
refresh_token,
client_id: clientId,
grant_type: "refresh_token",
redirect_uri: this.redirectUri
}
});
return this._handleAuthResponse(data);
} catch (e) {
if (
(isServerError(e) || isAbortError(e) || isTimeoutError(e)) &&
retryCount < amountOfRetries - 1
) {
later(this, this._refresh, refresh_token, retryCount + 1, retryTimeout);
} else {
throw e;
}
});

return this._handleAuthResponse(data);
}
},

/**
Expand All @@ -131,10 +144,10 @@ export default BaseAuthenticator.extend({
}

if (this._upcomingRefresh) {
run.cancel(this._upcomingRefresh);
cancel(this._upcomingRefresh);
this._upcomingRefresh = null;
}
this._upcomingRefresh = run.later(
this._upcomingRefresh = later(
this,
async token => {
this.trigger("sessionDataUpdated", await this._refresh(token));
Expand Down
4 changes: 3 additions & 1 deletion addon/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export default Object.assign(
refreshLeeway: 1000 * 30,
tokenPropertyName: "access_token",
authHeaderName: "Authorization",
authPrefix: "Bearer"
authPrefix: "Bearer",
amountOfRetries: 3,
retryTimeout: 3000
},
config["ember-simple-auth-oidc"] || {}
);
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4277,7 +4277,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@~4.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -7022,7 +7022,7 @@ import-lazy@^2.1.0:
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -7899,7 +7899,7 @@ libnpm@^2.0.1:
read-package-json "^2.0.13"
stringify-package "^1.0.0"

libnpmaccess@*, libnpmaccess@^3.0.1:
libnpmaccess@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/libnpmaccess/-/libnpmaccess-3.0.1.tgz#5b3a9de621f293d425191aa2e779102f84167fa8"
integrity sha512-RlZ7PNarCBt+XbnP7R6PoVgOq9t+kou5rvhaInoNibhPO7eMlRfS0B8yjatgn2yaHIwWNyoJDolC/6Lc5L/IQA==
Expand Down Expand Up @@ -7928,7 +7928,7 @@ libnpmhook@^5.0.2:
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmorg@*, libnpmorg@^1.0.0:
libnpmorg@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/libnpmorg/-/libnpmorg-1.0.0.tgz#979b868c48ba28c5820e3bb9d9e73c883c16a232"
integrity sha512-o+4eVJBoDGMgRwh2lJY0a8pRV2c/tQM/SxlqXezjcAg26Qe9jigYVs+Xk0vvlYDWCDhP0g74J8UwWeAgsB7gGw==
Expand All @@ -7953,7 +7953,7 @@ libnpmpublish@^1.1.0:
semver "^5.5.1"
ssri "^6.0.1"

libnpmsearch@*, libnpmsearch@^2.0.0:
libnpmsearch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/libnpmsearch/-/libnpmsearch-2.0.0.tgz#de05af47ada81554a5f64276a69599070d4a5685"
integrity sha512-vd+JWbTGzOSfiOc+72MU6y7WqmBXn49egCCrIXp27iE/88bX8EpG64ST1blWQI1bSMUr9l1AKPMVsqa2tS5KWA==
Expand All @@ -7962,7 +7962,7 @@ libnpmsearch@*, libnpmsearch@^2.0.0:
get-stream "^4.0.0"
npm-registry-fetch "^3.8.0"

libnpmteam@*, libnpmteam@^1.0.1:
libnpmteam@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/libnpmteam/-/libnpmteam-1.0.1.tgz#ff704b1b6c06ea674b3b1101ac3e305f5114f213"
integrity sha512-gDdrflKFCX7TNwOMX1snWojCoDE5LoRWcfOC0C/fqF7mBq8Uz9zWAX4B2RllYETNO7pBupBaSyBDkTAC15cAMg==
Expand Down Expand Up @@ -8250,7 +8250,7 @@ lodash._basefor@^3.0.0:
resolved "https://registry.yarnpkg.com/lodash._basefor/-/lodash._basefor-3.0.3.tgz#7550b4e9218ef09fad24343b612021c79b4c20c2"
integrity sha1-dVC06SGO8J+tJDQ7YSAhx5tMIMI=

lodash._baseindexof@*, lodash._baseindexof@^3.0.0:
lodash._baseindexof@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
Expand Down Expand Up @@ -8281,12 +8281,12 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*, lodash._bindcallback@^3.0.0:
lodash._bindcallback@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*, lodash._cacheindexof@^3.0.0:
lodash._cacheindexof@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
Expand All @@ -8300,7 +8300,7 @@ lodash._createassigner@^3.0.0:
lodash._isiterateecall "^3.0.0"
lodash.restparam "^3.0.0"

lodash._createcache@*, lodash._createcache@^3.0.0:
lodash._createcache@^3.0.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
Expand Down Expand Up @@ -8333,7 +8333,7 @@ lodash._escapestringchar@~2.3.0:
resolved "https://registry.yarnpkg.com/lodash._escapestringchar/-/lodash._escapestringchar-2.3.0.tgz#cce73ae60fc6da55d2bf8a0679c23ca2bab149fc"
integrity sha1-zOc65g/G2lXSv4oGecI8orqxSfw=

lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
Expand Down Expand Up @@ -8618,7 +8618,7 @@ lodash.pairs@^3.0.0:
dependencies:
lodash.keys "^3.0.0"

lodash.restparam@*, lodash.restparam@^3.0.0:
lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
Expand Down Expand Up @@ -9746,7 +9746,7 @@ npm-pick-manifest@^2.2.3:
npm-package-arg "^6.0.0"
semver "^5.4.1"

npm-profile@*, npm-profile@^4.0.1:
npm-profile@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/npm-profile/-/npm-profile-4.0.1.tgz#d350f7a5e6b60691c7168fbb8392c3603583f5aa"
integrity sha512-NQ1I/1Q7YRtHZXkcuU1/IyHeLy6pd+ScKg4+DQHdfsm769TGq6HPrkbuNJVJS4zwE+0mvvmeULzQdWn2L2EsVA==
Expand Down Expand Up @@ -11119,7 +11119,7 @@ readable-stream@~2.0.5:
string_decoder "~0.10.x"
util-deprecate "~1.0.1"

readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=
Expand Down

0 comments on commit 63cd8d3

Please sign in to comment.