@ambassify/ambassify-client
Advanced tools
Comparing version 6.0.2 to 6.0.3
@@ -39,2 +39,3 @@ "use strict"; | ||
return guardian.post('/token', tokenOptions).then(res => { | ||
var _jwt$decode; | ||
if (!res.ok) { | ||
@@ -55,3 +56,3 @@ throw AmbassifyError.fromExternal({ | ||
} = res.body; | ||
const tokenData = jwt.decode(access_token); | ||
const tokenData = (_jwt$decode = jwt.decode(access_token)) === null || _jwt$decode === void 0 ? void 0 : _jwt$decode.claims; | ||
return new this({ | ||
@@ -149,2 +150,3 @@ tokenType: 'Bearer', | ||
_request(url, options) { | ||
var _jwt$decode2; | ||
if (!this.refreshTokenEnabled()) return super._request(url, options); | ||
@@ -154,3 +156,3 @@ const authorization = _get(options, 'headers.authorization', ''); | ||
if (!token) return super._request(url, options); | ||
const data = jwt.decode(token); | ||
const data = (_jwt$decode2 = jwt.decode(token)) === null || _jwt$decode2 === void 0 ? void 0 : _jwt$decode2.claims; | ||
const now = Math.floor(Date.now() / 1000); | ||
@@ -157,0 +159,0 @@ if (!data || !data.exp || data.exp > now + 60) return super._request(url, options); |
@@ -12,5 +12,11 @@ "use strict"; | ||
try { | ||
const [, encoded] = jwt.split('.'); | ||
const json = decodeBase64(encoded); | ||
return JSON.parse(json); | ||
const parts = jwt.split('.'); | ||
const header = JSON.parse(decodeBase64(parts[0])); | ||
const claims = JSON.parse(decodeBase64(parts[1])); | ||
const signature = parts[2]; | ||
return { | ||
header, | ||
claims, | ||
signature | ||
}; | ||
} catch (e) { | ||
@@ -17,0 +23,0 @@ return null; |
@@ -29,6 +29,7 @@ "use strict"; | ||
} | ||
function doExternalRefresh(fetch, options) { | ||
function doRefreshTokenRequest(fetch, options) { | ||
const { | ||
clientId, | ||
clientSecret, | ||
grantType, | ||
refreshToken, | ||
@@ -44,3 +45,3 @@ tokenEndpoint, | ||
body: JSON.stringify({ | ||
grant_type: 'refresh_token', | ||
grant_type: grantType, | ||
client_id: clientId, | ||
@@ -54,31 +55,27 @@ client_secret: clientSecret, | ||
} | ||
function doExternalRefresh(fetch, options) { | ||
return doRefreshTokenRequest(fetch, { | ||
...options, | ||
grantType: 'refresh_token' | ||
}); | ||
} | ||
function doInternalRefresh(fetch, options) { | ||
const { | ||
clientId, | ||
clientSecret, | ||
refreshToken, | ||
tokenEndpoint, | ||
tokenAuthorization | ||
} = options; | ||
const reqOptions = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
grant_type: 'ambassify:refresh_token/internal', | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
refresh_token: refreshToken | ||
}) | ||
}; | ||
if (tokenAuthorization) reqOptions.headers.authorization = tokenAuthorization; | ||
return fetch(tokenEndpoint, reqOptions); | ||
return doRefreshTokenRequest(fetch, { | ||
...options, | ||
grantType: 'ambassify:refresh_token/internal' | ||
}); | ||
} | ||
function doRefreshToken(fetch, options) { | ||
var _jwt$decode; | ||
if (!isRefreshEnabled(options)) return Promise.reject(new TokenRefreshFailed('Refresh token is not enabled')); | ||
const { | ||
tokenType = 'Bearer' | ||
tokenType = 'Bearer', | ||
refreshToken | ||
} = options; | ||
return Promise.all([doInternalRefresh(fetch, options).catch(() => null), doExternalRefresh(fetch, options).catch(() => null)]).then(results => { | ||
const { | ||
kid | ||
} = ((_jwt$decode = jwt.decode(refreshToken)) === null || _jwt$decode === void 0 ? void 0 : _jwt$decode.header) || {}; | ||
const tryInternal = !kid || kid === 'internal'; | ||
const tryExternal = !kid || kid === 'external'; | ||
return Promise.all([tryInternal && doInternalRefresh(fetch, options).catch(() => null), tryExternal && doExternalRefresh(fetch, options).catch(() => null)]).then(results => { | ||
results = results.filter(Boolean); | ||
@@ -116,2 +113,3 @@ const res = results.filter(r => r.ok).pop() || results.pop(); | ||
onCached: item => item.value.then(_ref2 => { | ||
var _jwt$decode2; | ||
let { | ||
@@ -122,3 +120,3 @@ token | ||
exp | ||
} = jwt.decode(token) || {}; | ||
} = ((_jwt$decode2 = jwt.decode(token)) === null || _jwt$decode2 === void 0 ? void 0 : _jwt$decode2.claims) || {}; | ||
if (!exp) return; | ||
@@ -125,0 +123,0 @@ let ttl = (exp - 60) * 1000 - Date.now(); |
{ | ||
"name": "@ambassify/ambassify-client", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"description": "Base API client library for all Ambassify clients", | ||
@@ -61,3 +61,3 @@ "engines": { | ||
}, | ||
"gitHead": "fbb34f1a4968bfb3f80948554da628e21349150c" | ||
"gitHead": "0b75a4fb04a82fbfcdb075c195211203e0f94e70" | ||
} |
@@ -49,3 +49,3 @@ const _get = require('lodash/get'); | ||
const { access_token, refresh_token } = res.body; | ||
const tokenData = jwt.decode(access_token); | ||
const tokenData = jwt.decode(access_token)?.claims; | ||
@@ -168,3 +168,3 @@ return new this({ | ||
const data = jwt.decode(token); | ||
const data = jwt.decode(token)?.claims; | ||
const now = Math.floor(Date.now() / 1000); | ||
@@ -171,0 +171,0 @@ if (!data || !data.exp || data.exp > now + 60) |
@@ -10,5 +10,7 @@ const decodeBase64 = require('compact-base64').decode; | ||
try { | ||
const [ , encoded ] = jwt.split('.'); | ||
const json = decodeBase64(encoded); | ||
return JSON.parse(json); | ||
const parts = jwt.split('.'); | ||
const header = JSON.parse(decodeBase64(parts[0])); | ||
const claims = JSON.parse(decodeBase64(parts[1])); | ||
const signature = parts[2]; | ||
return { header, claims, signature }; | ||
} catch (e) { | ||
@@ -15,0 +17,0 @@ return null; |
@@ -23,6 +23,7 @@ const _pick = require('lodash/pick'); | ||
function doExternalRefresh(fetch, options) { | ||
function doRefreshTokenRequest(fetch, options) { | ||
const { | ||
clientId, | ||
clientSecret, | ||
grantType, | ||
refreshToken, | ||
@@ -39,3 +40,3 @@ tokenEndpoint, | ||
body: JSON.stringify({ | ||
grant_type: 'refresh_token', | ||
grant_type: grantType, | ||
client_id: clientId, | ||
@@ -53,28 +54,14 @@ client_secret: clientSecret, | ||
function doExternalRefresh(fetch, options) { | ||
return doRefreshTokenRequest(fetch, { | ||
...options, | ||
grantType: 'refresh_token' | ||
}); | ||
} | ||
function doInternalRefresh(fetch, options) { | ||
const { | ||
clientId, | ||
clientSecret, | ||
refreshToken, | ||
tokenEndpoint, | ||
tokenAuthorization, | ||
} = options; | ||
const reqOptions = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
grant_type: 'ambassify:refresh_token/internal', | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
refresh_token: refreshToken | ||
}) | ||
}; | ||
if (tokenAuthorization) | ||
reqOptions.headers.authorization = tokenAuthorization; | ||
return fetch(tokenEndpoint, reqOptions); | ||
return doRefreshTokenRequest(fetch, { | ||
...options, | ||
grantType: 'ambassify:refresh_token/internal' | ||
}); | ||
} | ||
@@ -85,9 +72,16 @@ | ||
return Promise.reject(new TokenRefreshFailed('Refresh token is not enabled')); | ||
const { | ||
tokenType = 'Bearer' | ||
tokenType = 'Bearer', | ||
refreshToken, | ||
} = options; | ||
const { kid } = jwt.decode(refreshToken)?.header || {}; | ||
const tryInternal = !kid || kid === 'internal'; | ||
const tryExternal = !kid || kid === 'external'; | ||
return Promise.all([ | ||
doInternalRefresh(fetch, options).catch(() => null), | ||
doExternalRefresh(fetch, options).catch(() => null) | ||
tryInternal && doInternalRefresh(fetch, options).catch(() => null), | ||
tryExternal && doExternalRefresh(fetch, options).catch(() => null) | ||
]) | ||
@@ -147,3 +141,3 @@ .then(results => { | ||
onCached: item => item.value.then(({ token }) => { | ||
const { exp } = jwt.decode(token) || {}; | ||
const { exp } = jwt.decode(token)?.claims || {}; | ||
@@ -150,0 +144,0 @@ if (!exp) |
Sorry, the diff of this file is not supported yet
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
41264
1054
15