rest-client-sdk
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -5,2 +5,9 @@ # Changelog | ||
## 2.3.0 | ||
### Changed | ||
* `TokenStorage` add possibility to pass access_token async storage key as constructor param | ||
* `AbstractClient._manageAccessDenied` checks headers instead of json to ensure refreshing the token is required | ||
## 2.2.1 | ||
@@ -7,0 +14,0 @@ |
@@ -1059,34 +1059,38 @@ import diff from 'deep-diff'; | ||
}, { | ||
key: '_manageAccessDenied', | ||
value: function _manageAccessDenied(response, input, init) { | ||
key: '_refreshTokenAndRefetch', | ||
value: function _refreshTokenAndRefetch(response, input, init) { | ||
var _this4 = this; | ||
return response.json().then(function (body) { | ||
if (body.error === 'invalid_grant') { | ||
switch (body.error_description) { | ||
case 'The access token provided has expired.': | ||
if (_this4._tokenStorage) { | ||
return _this4._tokenStorage.refreshToken().then(function () { | ||
var params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers) | ||
}); | ||
delete params.headers.Authorization; | ||
return this._tokenStorage.refreshToken().then(function () { | ||
var params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers) | ||
}); | ||
delete params.headers.Authorization; | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to renew access_token', response); | ||
}); | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to renew access_token', response); | ||
}); | ||
} | ||
}, { | ||
key: '_manageAccessDenied', | ||
value: function _manageAccessDenied(response, input, init) { | ||
// https://tools.ietf.org/html/rfc2617#section-1.2 | ||
var authorizationHeader = response.headers.get('www-authenticate'); | ||
if (authorizationHeader) { | ||
var invalidGrant = authorizationHeader.indexOf('error = "invalid_grant"'); | ||
if (invalidGrant) { | ||
var expired = authorizationHeader.indexOf('error_description="The access token provided has expired."'); | ||
switch (true) { | ||
case !!expired: | ||
if (this._tokenStorage) { | ||
return this._refreshTokenAndRefetch(response, input, init); | ||
} | ||
break; | ||
default: | ||
throw new AccessDeniedError(body.error_description, response); | ||
break; | ||
} | ||
} | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
}); | ||
} | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
} | ||
@@ -1162,6 +1166,5 @@ }, { | ||
var ACCESS_TOKEN_KEY = 'rest_client_sdk.api.access_token'; | ||
var TokenStorage = function () { | ||
function TokenStorage(tokenGenerator, asyncStorage) { | ||
var accessTokenKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'rest_client_sdk.api.access_token'; | ||
classCallCheck(this, TokenStorage); | ||
@@ -1172,2 +1175,3 @@ | ||
this.setAsyncStorage(asyncStorage); | ||
this.accessTokenKey = accessTokenKey; | ||
} | ||
@@ -1183,3 +1187,3 @@ | ||
value: function hasAccessToken() { | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (accessToken) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (accessToken) { | ||
return !!accessToken; | ||
@@ -1200,3 +1204,3 @@ }); | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (token) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (token) { | ||
if (token) { | ||
@@ -1226,3 +1230,3 @@ var tokenObject = JSON.parse(token); | ||
value: function logout() { | ||
return this._asyncStorage.removeItem(ACCESS_TOKEN_KEY); | ||
return this._asyncStorage.removeItem(this.accessTokenKey); | ||
} | ||
@@ -1246,3 +1250,3 @@ }, { | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (token) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (token) { | ||
return _this3._tokenGenerator.refreshToken(JSON.parse(token), parameters).then(function (responseData) { | ||
@@ -1258,3 +1262,3 @@ return _this3._storeAccessToken(responseData).then(function () { | ||
value: function _storeAccessToken(responseData) { | ||
return this._asyncStorage.setItem(ACCESS_TOKEN_KEY, JSON.stringify(responseData)); | ||
return this._asyncStorage.setItem(this.accessTokenKey, JSON.stringify(responseData)); | ||
} | ||
@@ -1261,0 +1265,0 @@ }]); |
@@ -1065,34 +1065,38 @@ (function (global, factory) { | ||
}, { | ||
key: '_manageAccessDenied', | ||
value: function _manageAccessDenied(response, input, init) { | ||
key: '_refreshTokenAndRefetch', | ||
value: function _refreshTokenAndRefetch(response, input, init) { | ||
var _this4 = this; | ||
return response.json().then(function (body) { | ||
if (body.error === 'invalid_grant') { | ||
switch (body.error_description) { | ||
case 'The access token provided has expired.': | ||
if (_this4._tokenStorage) { | ||
return _this4._tokenStorage.refreshToken().then(function () { | ||
var params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers) | ||
}); | ||
delete params.headers.Authorization; | ||
return this._tokenStorage.refreshToken().then(function () { | ||
var params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers) | ||
}); | ||
delete params.headers.Authorization; | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to renew access_token', response); | ||
}); | ||
return _this4._fetchWithToken(input, params); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to renew access_token', response); | ||
}); | ||
} | ||
}, { | ||
key: '_manageAccessDenied', | ||
value: function _manageAccessDenied(response, input, init) { | ||
// https://tools.ietf.org/html/rfc2617#section-1.2 | ||
var authorizationHeader = response.headers.get('www-authenticate'); | ||
if (authorizationHeader) { | ||
var invalidGrant = authorizationHeader.indexOf('error = "invalid_grant"'); | ||
if (invalidGrant) { | ||
var expired = authorizationHeader.indexOf('error_description="The access token provided has expired."'); | ||
switch (true) { | ||
case !!expired: | ||
if (this._tokenStorage) { | ||
return this._refreshTokenAndRefetch(response, input, init); | ||
} | ||
break; | ||
default: | ||
throw new AccessDeniedError(body.error_description, response); | ||
break; | ||
} | ||
} | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
}).catch(function () { | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
}); | ||
} | ||
throw new AccessDeniedError('Unable to access ressource: 401 found !', response); | ||
} | ||
@@ -1168,6 +1172,5 @@ }, { | ||
var ACCESS_TOKEN_KEY = 'rest_client_sdk.api.access_token'; | ||
var TokenStorage = function () { | ||
function TokenStorage(tokenGenerator, asyncStorage) { | ||
var accessTokenKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'rest_client_sdk.api.access_token'; | ||
classCallCheck(this, TokenStorage); | ||
@@ -1178,2 +1181,3 @@ | ||
this.setAsyncStorage(asyncStorage); | ||
this.accessTokenKey = accessTokenKey; | ||
} | ||
@@ -1189,3 +1193,3 @@ | ||
value: function hasAccessToken() { | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (accessToken) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (accessToken) { | ||
return !!accessToken; | ||
@@ -1206,3 +1210,3 @@ }); | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (token) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (token) { | ||
if (token) { | ||
@@ -1232,3 +1236,3 @@ var tokenObject = JSON.parse(token); | ||
value: function logout() { | ||
return this._asyncStorage.removeItem(ACCESS_TOKEN_KEY); | ||
return this._asyncStorage.removeItem(this.accessTokenKey); | ||
} | ||
@@ -1252,3 +1256,3 @@ }, { | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(function (token) { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(function (token) { | ||
return _this3._tokenGenerator.refreshToken(JSON.parse(token), parameters).then(function (responseData) { | ||
@@ -1264,3 +1268,3 @@ return _this3._storeAccessToken(responseData).then(function () { | ||
value: function _storeAccessToken(responseData) { | ||
return this._asyncStorage.setItem(ACCESS_TOKEN_KEY, JSON.stringify(responseData)); | ||
return this._asyncStorage.setItem(this.accessTokenKey, JSON.stringify(responseData)); | ||
} | ||
@@ -1267,0 +1271,0 @@ }]); |
{ | ||
"name": "rest-client-sdk", | ||
"version": "v2.2.1", | ||
"version": "v2.3.0", | ||
"description": "Rest Client SDK for API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -237,48 +237,46 @@ import URI from 'urijs'; | ||
_manageAccessDenied(response, input, init) { | ||
return response | ||
.json() | ||
.then(body => { | ||
if (body.error === 'invalid_grant') { | ||
switch (body.error_description) { | ||
case 'The access token provided has expired.': | ||
if (this._tokenStorage) { | ||
return this._tokenStorage | ||
.refreshToken() | ||
.then(() => { | ||
const params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers), | ||
}); | ||
delete params.headers.Authorization; | ||
_refreshTokenAndRefetch(response, input, init) { | ||
return this._tokenStorage | ||
.refreshToken() | ||
.then(() => { | ||
const params = Object.assign({}, init, { | ||
headers: Object.assign({}, init.headers), | ||
}); | ||
delete params.headers.Authorization; | ||
return this._fetchWithToken(input, params); | ||
}) | ||
.catch(() => { | ||
throw new AccessDeniedError( | ||
'Unable to renew access_token', | ||
response | ||
); | ||
}); | ||
} | ||
break; | ||
default: | ||
throw new AccessDeniedError(body.error_description, response); | ||
} | ||
} | ||
throw new AccessDeniedError( | ||
'Unable to access ressource: 401 found !', | ||
response | ||
); | ||
return this._fetchWithToken(input, params); | ||
}) | ||
.catch(() => { | ||
throw new AccessDeniedError( | ||
'Unable to access ressource: 401 found !', | ||
response | ||
); | ||
throw new AccessDeniedError('Unable to renew access_token', response); | ||
}); | ||
} | ||
_manageAccessDenied(response, input, init) { | ||
// https://tools.ietf.org/html/rfc2617#section-1.2 | ||
const authorizationHeader = response.headers.get('www-authenticate'); | ||
if (authorizationHeader) { | ||
const invalidGrant = authorizationHeader.indexOf( | ||
'error = "invalid_grant"' | ||
); | ||
if (invalidGrant) { | ||
const expired = authorizationHeader.indexOf( | ||
'error_description="The access token provided has expired."' | ||
); | ||
switch (true) { | ||
case !!expired: | ||
if (this._tokenStorage) { | ||
return this._refreshTokenAndRefetch(response, input, init); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
throw new AccessDeniedError( | ||
'Unable to access ressource: 401 found !', | ||
response | ||
); | ||
} | ||
_doFetch(accessToken, input, init) { | ||
@@ -285,0 +283,0 @@ let params = init; |
@@ -1,8 +0,7 @@ | ||
const ACCESS_TOKEN_KEY = 'rest_client_sdk.api.access_token'; | ||
class TokenStorage { | ||
constructor(tokenGenerator, asyncStorage) { | ||
constructor(tokenGenerator, asyncStorage, accessTokenKey = 'rest_client_sdk.api.access_token') { | ||
this._tokenGenerator = tokenGenerator; | ||
this._hasATokenBeenGenerated = false; | ||
this.setAsyncStorage(asyncStorage); | ||
this.accessTokenKey = accessTokenKey; | ||
} | ||
@@ -16,3 +15,3 @@ | ||
return this._asyncStorage | ||
.getItem(ACCESS_TOKEN_KEY) | ||
.getItem(this.accessTokenKey) | ||
.then(accessToken => !!accessToken); | ||
@@ -28,3 +27,3 @@ } | ||
getAccessTokenObject() { | ||
return this._asyncStorage.getItem(ACCESS_TOKEN_KEY).then(token => { | ||
return this._asyncStorage.getItem(this.accessTokenKey).then(token => { | ||
if (token) { | ||
@@ -53,3 +52,3 @@ const tokenObject = JSON.parse(token); | ||
logout() { | ||
return this._asyncStorage.removeItem(ACCESS_TOKEN_KEY); | ||
return this._asyncStorage.removeItem(this.accessTokenKey); | ||
} | ||
@@ -68,3 +67,3 @@ | ||
return this._asyncStorage | ||
.getItem(ACCESS_TOKEN_KEY) | ||
.getItem(this.accessTokenKey) | ||
.then(token => | ||
@@ -81,3 +80,3 @@ this._tokenGenerator | ||
return this._asyncStorage.setItem( | ||
ACCESS_TOKEN_KEY, | ||
this.accessTokenKey, | ||
JSON.stringify(responseData) | ||
@@ -84,0 +83,0 @@ ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
329329
4226