@bity/oauth2-auth-code-pkce
Advanced tools
Comparing version 2.9.0 to 2.10.1
85
index.js
@@ -279,3 +279,3 @@ "use strict"; | ||
}) | ||
.reduce(function (a, c) { return (__assign(__assign({}, a), c)); }); | ||
.reduce(function (a, c) { return (__assign(__assign({}, a), c)); }, {}); | ||
return { realm: obj.realm, error: obj.error }; | ||
@@ -399,6 +399,10 @@ } | ||
* doesn't return because of the redirect behavior (uses `location.replace`). | ||
* | ||
* @param oneTimeParams A way to specify "one time" used query string | ||
* parameters during the authorization code fetching process, usually for | ||
* values which need to change at run-time. | ||
*/ | ||
OAuth2AuthCodePKCE.prototype.fetchAuthorizationCode = function () { | ||
OAuth2AuthCodePKCE.prototype.fetchAuthorizationCode = function (oneTimeParams) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, clientId, redirectUrl, scopes, _b, codeChallenge, codeVerifier, stateQueryParam, url; | ||
var _a, clientId, extraAuthorizationParams, redirectUrl, scopes, _b, codeChallenge, codeVerifier, stateQueryParam, url, extraParameters; | ||
return __generator(this, function (_c) { | ||
@@ -408,3 +412,3 @@ switch (_c.label) { | ||
this.assertStateAndConfigArePresent(); | ||
_a = this.config, clientId = _a.clientId, redirectUrl = _a.redirectUrl, scopes = _a.scopes; | ||
_a = this.config, clientId = _a.clientId, extraAuthorizationParams = _a.extraAuthorizationParams, redirectUrl = _a.redirectUrl, scopes = _a.scopes; | ||
return [4 /*yield*/, OAuth2AuthCodePKCE | ||
@@ -428,2 +432,6 @@ .generatePKCECodes()]; | ||
+ "code_challenge_method=S256"; | ||
if (extraAuthorizationParams || oneTimeParams) { | ||
extraParameters = __assign(__assign({}, extraAuthorizationParams), oneTimeParams); | ||
url = url + "&" + OAuth2AuthCodePKCE.objectToQueryString(extraParameters); | ||
} | ||
location.replace(url); | ||
@@ -446,3 +454,3 @@ return [2 /*return*/]; | ||
var onAccessTokenExpiry = this.config.onAccessTokenExpiry; | ||
var _a = this.state, accessToken = _a.accessToken, authorizationCode = _a.authorizationCode, hasAuthCodeBeenExchangedForAccessToken = _a.hasAuthCodeBeenExchangedForAccessToken, refreshToken = _a.refreshToken, scopes = _a.scopes; | ||
var _a = this.state, accessToken = _a.accessToken, authorizationCode = _a.authorizationCode, explicitlyExposedTokens = _a.explicitlyExposedTokens, hasAuthCodeBeenExchangedForAccessToken = _a.hasAuthCodeBeenExchangedForAccessToken, refreshToken = _a.refreshToken, scopes = _a.scopes; | ||
if (!authorizationCode) { | ||
@@ -462,3 +470,3 @@ return Promise.reject(new ErrorNoAuthCode()); | ||
} | ||
return Promise.resolve({ token: accessToken, scopes: scopes }); | ||
return Promise.resolve({ token: accessToken, explicitlyExposedTokens: explicitlyExposedTokens, scopes: scopes }); | ||
}; | ||
@@ -472,3 +480,3 @@ /** | ||
this.assertStateAndConfigArePresent(); | ||
var _b = this.config, onInvalidGrant = _b.onInvalidGrant, tokenUrl = _b.tokenUrl; | ||
var _b = this.config, extraRefreshParams = _b.extraRefreshParams, clientId = _b.clientId, tokenUrl = _b.tokenUrl; | ||
var refreshToken = this.state.refreshToken; | ||
@@ -480,3 +488,7 @@ if (!refreshToken) { | ||
var body = "grant_type=refresh_token&" | ||
+ ("refresh_token=" + ((_a = refreshToken) === null || _a === void 0 ? void 0 : _a.value)); | ||
+ ("refresh_token=" + ((_a = refreshToken) === null || _a === void 0 ? void 0 : _a.value) + "&") | ||
+ ("client_id=" + clientId); | ||
if (extraRefreshParams) { | ||
body = url + "&" + OAuth2AuthCodePKCE.objectToQueryString(extraRefreshParams); | ||
} | ||
return fetch(url, { | ||
@@ -489,6 +501,8 @@ method: 'POST', | ||
}) | ||
.then(function (res) { return res.status === 400 ? Promise.reject(res.json()) : res.json(); }) | ||
.then(function (_a) { | ||
var access_token = _a.access_token, expires_in = _a.expires_in, refresh_token = _a.refresh_token, scope = _a.scope; | ||
.then(function (res) { return res.status >= 400 ? res.json().then(function (data) { return Promise.reject(data); }) : res.json(); }) | ||
.then(function (json) { | ||
var access_token = json.access_token, expires_in = json.expires_in, refresh_token = json.refresh_token, scope = json.scope; | ||
var explicitlyExposedTokens = _this.config.explicitlyExposedTokens; | ||
var scopes = []; | ||
var tokensToExpose = {}; | ||
var accessToken = { | ||
@@ -505,2 +519,11 @@ value: access_token, | ||
} | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries(explicitlyExposedTokens | ||
.map(function (tokenName) { return [tokenName, json[tokenName]]; }) | ||
.filter(function (_a) { | ||
var _ = _a[0], tokenValue = _a[1]; | ||
return tokenValue !== undefined; | ||
})); | ||
_this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -513,6 +536,10 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(exports.LOCALSTORAGE_STATE, JSON.stringify(_this.state)); | ||
return { token: accessToken, scopes: scopes }; | ||
var accessContext = { token: accessToken, scopes: scopes }; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}) | ||
.catch(function (jsonPromise) { return Promise.reject(jsonPromise); }) | ||
.catch(function (data) { | ||
var onInvalidGrant = _this.config.onInvalidGrant; | ||
var error = data.error || 'There was a network error.'; | ||
@@ -526,3 +553,3 @@ switch (error) { | ||
} | ||
return Promise.reject(error); | ||
return Promise.reject(toErrorClass(error)); | ||
}); | ||
@@ -619,5 +646,7 @@ }; | ||
} | ||
return jsonPromise.then(function (_a) { | ||
var access_token = _a.access_token, expires_in = _a.expires_in, refresh_token = _a.refresh_token, scope = _a.scope; | ||
return jsonPromise.then(function (json) { | ||
var access_token = json.access_token, expires_in = json.expires_in, refresh_token = json.refresh_token, scope = json.scope; | ||
var explicitlyExposedTokens = _this.config.explicitlyExposedTokens; | ||
var scopes = []; | ||
var tokensToExpose = {}; | ||
_this.state.hasAuthCodeBeenExchangedForAccessToken = true; | ||
@@ -636,2 +665,11 @@ _this.authCodeForAccessTokenRequest = undefined; | ||
} | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries(explicitlyExposedTokens | ||
.map(function (tokenName) { return [tokenName, json[tokenName]]; }) | ||
.filter(function (_a) { | ||
var _ = _a[0], tokenValue = _a[1]; | ||
return tokenValue !== undefined; | ||
})); | ||
_this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -644,3 +682,7 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(exports.LOCALSTORAGE_STATE, JSON.stringify(_this.state)); | ||
return { token: accessToken, scopes: scopes }; | ||
var accessContext = { token: accessToken, scopes: scopes }; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}); | ||
@@ -689,2 +731,11 @@ }); | ||
/** | ||
* Converts the keys and values of an object to a url query string | ||
*/ | ||
OAuth2AuthCodePKCE.objectToQueryString = function (dict) { | ||
return Object.entries(dict).map(function (_a) { | ||
var key = _a[0], val = _a[1]; | ||
return key + "=" + encodeURIComponent(val); | ||
}).join('&'); | ||
}; | ||
/** | ||
* Generates a code_verifier and code_challenge, as specified in rfc7636. | ||
@@ -691,0 +742,0 @@ */ |
98
index.ts
@@ -8,2 +8,3 @@ /** | ||
clientId: string; | ||
explicitlyExposedTokens?: string[]; | ||
onAccessTokenExpiry: (refreshAccessToken: () => Promise<AccessContext>) => Promise<AccessContext>; | ||
@@ -14,2 +15,4 @@ onInvalidGrant: (refreshAuthCodeOrRefreshToken: () => Promise<void>) => void; | ||
tokenUrl: URL; | ||
extraAuthorizationParams?: ObjStringDict; | ||
extraRefreshParams?: ObjStringDict; | ||
} | ||
@@ -28,2 +31,3 @@ | ||
codeVerifier?: string; | ||
explicitlyExposedTokens?: ObjStringDict; | ||
hasAuthCodeBeenExchangedForAccessToken?: boolean; | ||
@@ -48,5 +52,7 @@ refreshToken?: RefreshToken; | ||
token?: AccessToken; | ||
explicitlyExposedTokens?: ObjStringDict; | ||
scopes?: Scopes; | ||
}; | ||
export type ObjStringDict = { [_: string]: string }; | ||
export type HttpClient = ((...args: any[]) => Promise<any>); | ||
@@ -136,3 +142,3 @@ export type URL = string; | ||
.map(tokens => { const [k,v] = tokens.split('='); return {[k]:v}; }) | ||
.reduce((a, c) => ({ ...a, ...c})); | ||
.reduce((a, c) => ({ ...a, ...c}), {}); | ||
@@ -277,7 +283,11 @@ return { realm: obj.realm, error: obj.error }; | ||
* doesn't return because of the redirect behavior (uses `location.replace`). | ||
* | ||
* @param oneTimeParams A way to specify "one time" used query string | ||
* parameters during the authorization code fetching process, usually for | ||
* values which need to change at run-time. | ||
*/ | ||
public async fetchAuthorizationCode(): Promise<void> { | ||
public async fetchAuthorizationCode(oneTimeParams?: ObjStringDict): Promise<void> { | ||
this.assertStateAndConfigArePresent(); | ||
const { clientId, redirectUrl, scopes } = this.config; | ||
const { clientId, extraAuthorizationParams, redirectUrl, scopes } = this.config; | ||
const { codeChallenge, codeVerifier } = await OAuth2AuthCodePKCE | ||
@@ -298,3 +308,3 @@ .generatePKCECodes(); | ||
const url = this.config.authorizationUrl | ||
let url = this.config.authorizationUrl | ||
+ `?response_type=code&` | ||
@@ -308,2 +318,11 @@ + `client_id=${encodeURIComponent(clientId)}&` | ||
if (extraAuthorizationParams || oneTimeParams) { | ||
const extraParameters: ObjStringDict = { | ||
...extraAuthorizationParams, | ||
...oneTimeParams | ||
}; | ||
url = `${url}&${OAuth2AuthCodePKCE.objectToQueryString(extraParameters)}` | ||
} | ||
location.replace(url); | ||
@@ -326,2 +345,3 @@ } | ||
authorizationCode, | ||
explicitlyExposedTokens, | ||
hasAuthCodeBeenExchangedForAccessToken, | ||
@@ -350,3 +370,3 @@ refreshToken, | ||
return Promise.resolve({ token: accessToken, scopes }); | ||
return Promise.resolve({ token: accessToken, explicitlyExposedTokens, scopes }); | ||
} | ||
@@ -360,3 +380,3 @@ | ||
const { onInvalidGrant, tokenUrl } = this.config; | ||
const { extraRefreshParams, clientId, tokenUrl } = this.config; | ||
const { refreshToken } = this.state; | ||
@@ -369,5 +389,10 @@ | ||
const url = tokenUrl; | ||
const body = `grant_type=refresh_token&` | ||
+ `refresh_token=${refreshToken?.value}`; | ||
let body = `grant_type=refresh_token&` | ||
+ `refresh_token=${refreshToken?.value}&` | ||
+ `client_id=${clientId}`; | ||
if (extraRefreshParams) { | ||
body = `${url}&${OAuth2AuthCodePKCE.objectToQueryString(extraRefreshParams)}` | ||
} | ||
return fetch(url, { | ||
@@ -380,5 +405,8 @@ method: 'POST', | ||
}) | ||
.then(res => res.status === 400 ? Promise.reject(res.json()) : res.json()) | ||
.then(({ access_token, expires_in, refresh_token, scope }) => { | ||
.then(res => res.status >= 400 ? res.json().then(data => Promise.reject(data)) : res.json()) | ||
.then((json) => { | ||
const { access_token, expires_in, refresh_token, scope } = json; | ||
const { explicitlyExposedTokens } = this.config; | ||
let scopes = []; | ||
let tokensToExpose = {}; | ||
@@ -398,2 +426,11 @@ const accessToken: AccessToken = { | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries( | ||
explicitlyExposedTokens | ||
.map((tokenName: string): [string, string|undefined] => [tokenName, json[tokenName]]) | ||
.filter(([_, tokenValue]: [string, string|undefined]) => tokenValue !== undefined) | ||
); | ||
this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -407,6 +444,11 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state)); | ||
return { token: accessToken, scopes }; | ||
let accessContext: AccessContext = {token: accessToken, scopes}; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}) | ||
.catch(jsonPromise => Promise.reject(jsonPromise)) | ||
.catch(data => { | ||
const { onInvalidGrant } = this.config; | ||
const error = data.error || 'There was a network error.'; | ||
@@ -420,3 +462,3 @@ switch (error) { | ||
} | ||
return Promise.reject(error); | ||
return Promise.reject(toErrorClass(error)); | ||
}); | ||
@@ -528,4 +570,7 @@ } | ||
return jsonPromise.then(({ access_token, expires_in, refresh_token, scope }) => { | ||
return jsonPromise.then((json) => { | ||
const { access_token, expires_in, refresh_token, scope } = json; | ||
const { explicitlyExposedTokens } = this.config; | ||
let scopes = []; | ||
let tokensToExpose = {}; | ||
this.state.hasAuthCodeBeenExchangedForAccessToken = true; | ||
@@ -547,2 +592,11 @@ this.authCodeForAccessTokenRequest = undefined; | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries( | ||
explicitlyExposedTokens | ||
.map((tokenName: string): [string, string|undefined] => [tokenName, json[tokenName]]) | ||
.filter(([_, tokenValue]: [string, string|undefined]) => tokenValue !== undefined) | ||
); | ||
this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -556,3 +610,8 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state)); | ||
return { token: accessToken, scopes }; | ||
let accessContext: AccessContext = {token: accessToken, scopes}; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}); | ||
@@ -611,2 +670,11 @@ }); | ||
/** | ||
* Converts the keys and values of an object to a url query string | ||
*/ | ||
static objectToQueryString(dict: ObjStringDict): string { | ||
return Object.entries(dict).map( | ||
([key, val]: [string, string]) => `${key}=${encodeURIComponent(val)}` | ||
).join('&'); | ||
} | ||
/** | ||
* Generates a code_verifier and code_challenge, as specified in rfc7636. | ||
@@ -613,0 +681,0 @@ */ |
@@ -280,3 +280,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.OAuth2AuthCodePKCE = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
}) | ||
.reduce(function (a, c) { return (__assign(__assign({}, a), c)); }); | ||
.reduce(function (a, c) { return (__assign(__assign({}, a), c)); }, {}); | ||
return { realm: obj.realm, error: obj.error }; | ||
@@ -400,6 +400,10 @@ } | ||
* doesn't return because of the redirect behavior (uses `location.replace`). | ||
* | ||
* @param oneTimeParams A way to specify "one time" used query string | ||
* parameters during the authorization code fetching process, usually for | ||
* values which need to change at run-time. | ||
*/ | ||
OAuth2AuthCodePKCE.prototype.fetchAuthorizationCode = function () { | ||
OAuth2AuthCodePKCE.prototype.fetchAuthorizationCode = function (oneTimeParams) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, clientId, redirectUrl, scopes, _b, codeChallenge, codeVerifier, stateQueryParam, url; | ||
var _a, clientId, extraAuthorizationParams, redirectUrl, scopes, _b, codeChallenge, codeVerifier, stateQueryParam, url, extraParameters; | ||
return __generator(this, function (_c) { | ||
@@ -409,3 +413,3 @@ switch (_c.label) { | ||
this.assertStateAndConfigArePresent(); | ||
_a = this.config, clientId = _a.clientId, redirectUrl = _a.redirectUrl, scopes = _a.scopes; | ||
_a = this.config, clientId = _a.clientId, extraAuthorizationParams = _a.extraAuthorizationParams, redirectUrl = _a.redirectUrl, scopes = _a.scopes; | ||
return [4 /*yield*/, OAuth2AuthCodePKCE | ||
@@ -429,2 +433,6 @@ .generatePKCECodes()]; | ||
+ "code_challenge_method=S256"; | ||
if (extraAuthorizationParams || oneTimeParams) { | ||
extraParameters = __assign(__assign({}, extraAuthorizationParams), oneTimeParams); | ||
url = url + "&" + OAuth2AuthCodePKCE.objectToQueryString(extraParameters); | ||
} | ||
location.replace(url); | ||
@@ -447,3 +455,3 @@ return [2 /*return*/]; | ||
var onAccessTokenExpiry = this.config.onAccessTokenExpiry; | ||
var _a = this.state, accessToken = _a.accessToken, authorizationCode = _a.authorizationCode, hasAuthCodeBeenExchangedForAccessToken = _a.hasAuthCodeBeenExchangedForAccessToken, refreshToken = _a.refreshToken, scopes = _a.scopes; | ||
var _a = this.state, accessToken = _a.accessToken, authorizationCode = _a.authorizationCode, explicitlyExposedTokens = _a.explicitlyExposedTokens, hasAuthCodeBeenExchangedForAccessToken = _a.hasAuthCodeBeenExchangedForAccessToken, refreshToken = _a.refreshToken, scopes = _a.scopes; | ||
if (!authorizationCode) { | ||
@@ -463,3 +471,3 @@ return Promise.reject(new ErrorNoAuthCode()); | ||
} | ||
return Promise.resolve({ token: accessToken, scopes: scopes }); | ||
return Promise.resolve({ token: accessToken, explicitlyExposedTokens: explicitlyExposedTokens, scopes: scopes }); | ||
}; | ||
@@ -473,3 +481,3 @@ /** | ||
this.assertStateAndConfigArePresent(); | ||
var _b = this.config, onInvalidGrant = _b.onInvalidGrant, tokenUrl = _b.tokenUrl; | ||
var _b = this.config, extraRefreshParams = _b.extraRefreshParams, clientId = _b.clientId, tokenUrl = _b.tokenUrl; | ||
var refreshToken = this.state.refreshToken; | ||
@@ -481,3 +489,7 @@ if (!refreshToken) { | ||
var body = "grant_type=refresh_token&" | ||
+ ("refresh_token=" + ((_a = refreshToken) === null || _a === void 0 ? void 0 : _a.value)); | ||
+ ("refresh_token=" + ((_a = refreshToken) === null || _a === void 0 ? void 0 : _a.value) + "&") | ||
+ ("client_id=" + clientId); | ||
if (extraRefreshParams) { | ||
body = url + "&" + OAuth2AuthCodePKCE.objectToQueryString(extraRefreshParams); | ||
} | ||
return fetch(url, { | ||
@@ -490,6 +502,8 @@ method: 'POST', | ||
}) | ||
.then(function (res) { return res.status === 400 ? Promise.reject(res.json()) : res.json(); }) | ||
.then(function (_a) { | ||
var access_token = _a.access_token, expires_in = _a.expires_in, refresh_token = _a.refresh_token, scope = _a.scope; | ||
.then(function (res) { return res.status >= 400 ? res.json().then(function (data) { return Promise.reject(data); }) : res.json(); }) | ||
.then(function (json) { | ||
var access_token = json.access_token, expires_in = json.expires_in, refresh_token = json.refresh_token, scope = json.scope; | ||
var explicitlyExposedTokens = _this.config.explicitlyExposedTokens; | ||
var scopes = []; | ||
var tokensToExpose = {}; | ||
var accessToken = { | ||
@@ -506,2 +520,11 @@ value: access_token, | ||
} | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries(explicitlyExposedTokens | ||
.map(function (tokenName) { return [tokenName, json[tokenName]]; }) | ||
.filter(function (_a) { | ||
var _ = _a[0], tokenValue = _a[1]; | ||
return tokenValue !== undefined; | ||
})); | ||
_this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -514,6 +537,10 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(exports.LOCALSTORAGE_STATE, JSON.stringify(_this.state)); | ||
return { token: accessToken, scopes: scopes }; | ||
var accessContext = { token: accessToken, scopes: scopes }; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}) | ||
.catch(function (jsonPromise) { return Promise.reject(jsonPromise); }) | ||
.catch(function (data) { | ||
var onInvalidGrant = _this.config.onInvalidGrant; | ||
var error = data.error || 'There was a network error.'; | ||
@@ -527,3 +554,3 @@ switch (error) { | ||
} | ||
return Promise.reject(error); | ||
return Promise.reject(toErrorClass(error)); | ||
}); | ||
@@ -620,5 +647,7 @@ }; | ||
} | ||
return jsonPromise.then(function (_a) { | ||
var access_token = _a.access_token, expires_in = _a.expires_in, refresh_token = _a.refresh_token, scope = _a.scope; | ||
return jsonPromise.then(function (json) { | ||
var access_token = json.access_token, expires_in = json.expires_in, refresh_token = json.refresh_token, scope = json.scope; | ||
var explicitlyExposedTokens = _this.config.explicitlyExposedTokens; | ||
var scopes = []; | ||
var tokensToExpose = {}; | ||
_this.state.hasAuthCodeBeenExchangedForAccessToken = true; | ||
@@ -637,2 +666,11 @@ _this.authCodeForAccessTokenRequest = undefined; | ||
} | ||
if (explicitlyExposedTokens) { | ||
tokensToExpose = Object.fromEntries(explicitlyExposedTokens | ||
.map(function (tokenName) { return [tokenName, json[tokenName]]; }) | ||
.filter(function (_a) { | ||
var _ = _a[0], tokenValue = _a[1]; | ||
return tokenValue !== undefined; | ||
})); | ||
_this.state.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
if (scope) { | ||
@@ -645,3 +683,7 @@ // Multiple scopes are passed and delimited by spaces, | ||
localStorage.setItem(exports.LOCALSTORAGE_STATE, JSON.stringify(_this.state)); | ||
return { token: accessToken, scopes: scopes }; | ||
var accessContext = { token: accessToken, scopes: scopes }; | ||
if (explicitlyExposedTokens) { | ||
accessContext.explicitlyExposedTokens = tokensToExpose; | ||
} | ||
return accessContext; | ||
}); | ||
@@ -690,2 +732,11 @@ }); | ||
/** | ||
* Converts the keys and values of an object to a url query string | ||
*/ | ||
OAuth2AuthCodePKCE.objectToQueryString = function (dict) { | ||
return Object.entries(dict).map(function (_a) { | ||
var key = _a[0], val = _a[1]; | ||
return key + "=" + encodeURIComponent(val); | ||
}).join('&'); | ||
}; | ||
/** | ||
* Generates a code_verifier and code_challenge, as specified in rfc7636. | ||
@@ -692,0 +743,0 @@ */ |
{ | ||
"name": "@bity/oauth2-auth-code-pkce", | ||
"version": "2.9.0", | ||
"version": "2.10.1", | ||
"description": "An OAuth 2.0 client that ONLY supports Authorization Code flow with PKCE support.", | ||
@@ -27,5 +27,5 @@ "main": "index.js", | ||
"browserify": "^16.5.0", | ||
"http-server": "^0.11.1", | ||
"http-server": "^0.12.3", | ||
"typescript": "^3.7.5" | ||
} | ||
} |
@@ -29,2 +29,31 @@ # OAuth2AuthCodePKCE client | ||
## Exposing other query string parameters on return | ||
Some OAuth servers will return additional parameters to the requester. In order | ||
to access these they must be explicitly asked for: | ||
``` | ||
config.explicitlyExposedTokens = ['open_id']; | ||
``` | ||
Then this will be available as a property: | ||
`accessContext.explicitlyExposedTokens.open_id`. | ||
## Extra parameters which other OAuth servers require | ||
It is probable you will encounter an OAuth server which requires some additional | ||
parameters. In order to pass extra parameters, add the following to the | ||
configuration: | ||
``` | ||
config.extraAuthorizationParameters = { 'some_query_string_param': 'value', ... }; | ||
``` | ||
If you have values which need to be computed at run-time and then passed, you | ||
can pass them like so: | ||
``` | ||
oauth2.fetchAuthorizationCode({ 'another_query_string_param': computedValue }); | ||
``` | ||
## Module systems supported | ||
@@ -31,0 +60,0 @@ |
@@ -6,3 +6,3 @@ { | ||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ | ||
"lib": ["dom", "ES2015", "ES2017"], /* Specify library files to be included in the compilation. */ | ||
"lib": ["dom", "ES2015", "ES2017", "ES2019"], /* Specify library files to be included in the compilation. */ | ||
// "allowJs": true, /* Allow javascript files to be compiled. */ | ||
@@ -9,0 +9,0 @@ // "checkJs": true, /* Report errors in .js files. */ |
Sorry, the diff of this file is not supported yet
135619
2188
72