@axa-fr/react-oidc 6.8.1 → 6.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/OidcServiceWorker.js +19 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/vanilla/initWorker.d.ts +1 -1
- package/dist/vanilla/initWorker.d.ts.map +1 -1
- package/dist/vanilla/initWorker.js +2 -7
- package/dist/vanilla/initWorker.js.map +1 -1
- package/dist/vanilla/oidc.d.ts +1 -0
- package/dist/vanilla/oidc.d.ts.map +1 -1
- package/dist/vanilla/oidc.js +11 -11
- package/dist/vanilla/oidc.js.map +1 -1
- package/dist/vanilla/parseTokens.d.ts +21 -2
- package/dist/vanilla/parseTokens.d.ts.map +1 -1
- package/dist/vanilla/parseTokens.js +43 -5
- package/dist/vanilla/parseTokens.js.map +1 -1
- package/dist/vanilla/vanillaOidc.d.ts +1 -6
- package/dist/vanilla/vanillaOidc.d.ts.map +1 -1
- package/dist/vanilla/vanillaOidc.js +1 -13
- package/dist/vanilla/vanillaOidc.js.map +1 -1
- package/package.json +1 -1
- package/src/configurations.ts +3 -0
- package/src/oidc/index.ts +1 -0
- package/src/oidc/vanilla/OidcServiceWorker.js +19 -2
- package/src/oidc/vanilla/initWorker.ts +2 -8
- package/src/oidc/vanilla/oidc.ts +34 -30
- package/src/oidc/vanilla/parseTokens.spec.ts +48 -0
- package/src/oidc/vanilla/parseTokens.ts +53 -7
- package/src/oidc/vanilla/vanillaOidc.ts +2 -18
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isTokensOidcValid = exports.isTokensValid = exports.computeTimeLeft = exports.parseOriginalTokens = exports.setTokens = void 0;
|
|
12
|
+
exports.isTokensOidcValid = exports.getValidTokenAsync = exports.isTokensValid = exports.computeTimeLeft = exports.parseOriginalTokens = exports.setTokens = exports.TokenRenewMode = void 0;
|
|
13
|
+
const initWorker_1 = require("./initWorker");
|
|
4
14
|
const b64DecodeUnicode = (str) => decodeURIComponent(Array.prototype.map.call(atob(str), (c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
|
|
5
15
|
const parseJwt = (token) => JSON.parse(b64DecodeUnicode(token.split('.')[1].replace('-', '+').replace('_', '/')));
|
|
6
16
|
const extractTokenPayload = (token) => {
|
|
@@ -23,7 +33,12 @@ const extractTokenPayload = (token) => {
|
|
|
23
33
|
const countLetter = (str, find) => {
|
|
24
34
|
return (str.split(find)).length - 1;
|
|
25
35
|
};
|
|
26
|
-
|
|
36
|
+
exports.TokenRenewMode = {
|
|
37
|
+
access_token_or_id_token_invalid: "access_token_or_id_token_invalid",
|
|
38
|
+
access_token_invalid: "access_token_invalid",
|
|
39
|
+
id_token_invalid: "id_token_invalid"
|
|
40
|
+
};
|
|
41
|
+
const setTokens = (tokens, oldTokens = null, tokenRenewMode) => {
|
|
27
42
|
if (!tokens) {
|
|
28
43
|
return null;
|
|
29
44
|
}
|
|
@@ -41,7 +56,16 @@ const setTokens = (tokens, oldTokens = null) => {
|
|
|
41
56
|
const _idTokenPayload = tokens.idTokenPayload ? tokens.idTokenPayload : extractTokenPayload(tokens.idToken);
|
|
42
57
|
const idTokenExpireAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : Number.MAX_VALUE;
|
|
43
58
|
const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp) ? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
|
|
44
|
-
|
|
59
|
+
let expiresAt;
|
|
60
|
+
if (tokenRenewMode === exports.TokenRenewMode.access_token_invalid) {
|
|
61
|
+
expiresAt = accessTokenExpiresAt;
|
|
62
|
+
}
|
|
63
|
+
else if (tokenRenewMode === exports.TokenRenewMode.id_token_invalid) {
|
|
64
|
+
expiresAt = idTokenExpireAt;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
expiresAt = idTokenExpireAt < accessTokenExpiresAt ? idTokenExpireAt : accessTokenExpiresAt;
|
|
68
|
+
}
|
|
45
69
|
const newTokens = Object.assign(Object.assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload, expiresAt });
|
|
46
70
|
// When refresh_token is not rotated we reuse ald refresh_token
|
|
47
71
|
if (oldTokens != null && "refreshToken" in oldTokens && !("refreshToken" in tokens)) {
|
|
@@ -51,7 +75,7 @@ const setTokens = (tokens, oldTokens = null) => {
|
|
|
51
75
|
return newTokens;
|
|
52
76
|
};
|
|
53
77
|
exports.setTokens = setTokens;
|
|
54
|
-
const parseOriginalTokens = (tokens, oldTokens) => {
|
|
78
|
+
const parseOriginalTokens = (tokens, oldTokens, tokenRenewMode) => {
|
|
55
79
|
if (!tokens) {
|
|
56
80
|
return null;
|
|
57
81
|
}
|
|
@@ -79,7 +103,7 @@ const parseOriginalTokens = (tokens, oldTokens) => {
|
|
|
79
103
|
// @ts-ignore
|
|
80
104
|
data.idTokenPayload = tokens.idTokenPayload;
|
|
81
105
|
}
|
|
82
|
-
return (0, exports.setTokens)(data, oldTokens);
|
|
106
|
+
return (0, exports.setTokens)(data, oldTokens, tokenRenewMode);
|
|
83
107
|
};
|
|
84
108
|
exports.parseOriginalTokens = parseOriginalTokens;
|
|
85
109
|
const computeTimeLeft = (refreshTimeBeforeTokensExpirationInSecond, expiresAt) => {
|
|
@@ -94,6 +118,20 @@ const isTokensValid = (tokens) => {
|
|
|
94
118
|
return (0, exports.computeTimeLeft)(0, tokens.expiresAt) > 0;
|
|
95
119
|
};
|
|
96
120
|
exports.isTokensValid = isTokensValid;
|
|
121
|
+
const getValidTokenAsync = (oidc, waitMs = 200, numberWait = 50) => __awaiter(void 0, void 0, void 0, function* () {
|
|
122
|
+
let numberWaitTemp = numberWait;
|
|
123
|
+
while (!(0, exports.isTokensValid)(oidc.tokens) && numberWaitTemp > 0) {
|
|
124
|
+
yield (0, initWorker_1.sleepAsync)(200);
|
|
125
|
+
numberWaitTemp = numberWaitTemp - 1;
|
|
126
|
+
}
|
|
127
|
+
const isValid = (0, exports.isTokensValid)(oidc.tokens);
|
|
128
|
+
return {
|
|
129
|
+
isTokensValid: isValid,
|
|
130
|
+
tokens: oidc.tokens,
|
|
131
|
+
numberWaited: numberWaitTemp - numberWait
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
exports.getValidTokenAsync = getValidTokenAsync;
|
|
97
135
|
// https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation (excluding rules #1, #4, #5, #7, #8, #12, and #13 which did not apply).
|
|
98
136
|
// https://github.com/openid/AppAuth-JS/issues/65
|
|
99
137
|
const isTokensOidcValid = (tokens, nonce, oidcServerConfiguration) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseTokens.js","sourceRoot":"","sources":["../../src/oidc/vanilla/parseTokens.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseTokens.js","sourceRoot":"","sources":["../../src/oidc/vanilla/parseTokens.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAAwC;AAGxC,MAAM,gBAAgB,GAAG,CAAC,GAAG,EAAE,EAAE,CAC7B,kBAAkB,CAAC,KAAK,CAAC,S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C,MAAM;QACnB,YAAY,EAAE,cAAc,GAAG,UAAU;KAC5C,CAAC;AACN,CAAC,CAAA,CAAA;AAZY,QAAA,kBAAkB,sBAY9B;AAED,kJAAkJ;AAClJ,iDAAiD;AAC1C,MAAM,iBAAiB,GAAE,CAAC,MAAM,EAAE,KAAK,EAAE,uBAAuB,EAAE,EAAE;IACvE,IAAG,MAAM,CAAC,cAAc,EAAE;QACtB,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;QAC7C,0JAA0J;QAC1J,IAAG,uBAAuB,CAAC,MAAM,KAAM,cAAc,CAAC,GAAG,EAAC;YACtD,OAAO,KAAK,CAAC;SAChB;QACD,+YAA+Y;QAE/Y,gbAAgb;QAEhb,4EAA4E;QAC5E,MAAM,qBAAqB,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAE,IAAI,CAAC;QACzD,IAAG,cAAc,CAAC,GAAG,IAAI,cAAc,CAAC,GAAG,GAAG,qBAAqB,EAAE;YACjE,OAAO,KAAK,CAAC;SAChB;QACD,6NAA6N;QAC7N,MAAM,eAAe,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;QACzC,IAAG,cAAc,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,GAAG,eAAe,CAAC,GAAG,qBAAqB,EAAE;YACrF,OAAO,KAAK,CAAC;SAChB;QACD,+UAA+U;QAC/U,IAAI,cAAc,CAAC,KAAK,IAAI,cAAc,CAAC,KAAK,KAAK,KAAK,EAAE;YACxD,OAAO,KAAK,CAAC;SAChB;KACJ;IACD,OAAO,IAAI,CAAC;AAChB,CAAC,CAAA;AA3BY,QAAA,iBAAiB,qBA2B7B"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { LoginCallback, Oidc, OidcConfiguration, StringMap } from "./oidc";
|
|
2
|
+
import { ValidToken } from "./parseTokens";
|
|
2
3
|
import { Tokens } from "./parseTokens";
|
|
3
|
-
declare type ValidToken = {
|
|
4
|
-
isTokensValid: Boolean;
|
|
5
|
-
tokens: Tokens;
|
|
6
|
-
numberWaited: Number;
|
|
7
|
-
};
|
|
8
4
|
export declare class VanillaOidc {
|
|
9
5
|
private _oidc;
|
|
10
6
|
constructor(oidc: Oidc);
|
|
@@ -52,5 +48,4 @@ export declare class VanillaOidc {
|
|
|
52
48
|
getValidTokenAsync(waitMs?: number, numberWait?: number): Promise<ValidToken>;
|
|
53
49
|
userInfoAsync(): Promise<any>;
|
|
54
50
|
}
|
|
55
|
-
export {};
|
|
56
51
|
//# sourceMappingURL=vanillaOidc.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vanillaOidc.d.ts","sourceRoot":"","sources":["../../src/oidc/vanilla/vanillaOidc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,IAAI,EAAE,iBAAiB,EAAE,SAAS,EAAC,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"vanillaOidc.d.ts","sourceRoot":"","sources":["../../src/oidc/vanilla/vanillaOidc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,IAAI,EAAE,iBAAiB,EAAE,SAAS,EAAC,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAoC,UAAU,EAAC,MAAM,eAAe,CAAC;AAE5E,OAAO,EAAC,MAAM,EAAC,MAAM,eAAe,CAAC;AAGrC,qBAAa,WAAW;IACpB,OAAO,CAAC,KAAK,CAAO;gBACR,IAAI,EAAE,IAAI;IAGtB,eAAe,CAAC,IAAI,EAAC,QAAQ,GAAE,MAAM;IAGrC,uBAAuB,CAAC,EAAE,EAAC,MAAM,GAAE,IAAI;IAGvC,YAAY,CAAC,SAAS,EAAC,MAAM,EAAE,IAAI,EAAC,GAAG,GAAI,IAAI;IAG/C,MAAM,CAAC,WAAW,CAAC,aAAa,EAAC,iBAAiB,EAAE,IAAI,GAAC,MAAgB,GAAE,WAAW;IAGtF,MAAM,CAAC,GAAG,CAAC,IAAI,GAAC,MAAgB,GAAE,WAAW;IAG7C,MAAM,CAAC,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;MAAmB;IACpC,2BAA2B,IAAG,OAAO,CAAC,OAAO,CAAC;IAG9C,UAAU,CAAC,YAAY,GAAC,MAAgB,EAAE,MAAM,GAAC,SAAc,EAAE,cAAc,GAAC,OAAa,EAAE,KAAK,GAAC,MAAgB,EAAE,eAAe,UAAQ,GAAE,OAAO,CAAC,IAAI,CAAC;IAG7J,WAAW,CAAC,iBAAiB,GAAE,MAAM,GAAG,IAAI,GAAG,SAAqB,EAAE,MAAM,GAAE,SAAgB,GAAE,OAAO,CAAC,IAAI,CAAC;IAG7G,wBAAwB,IAAG,OAAO,CAAC,GAAG,CAAC;IAGvC,gBAAgB,CAAC,MAAM,GAAC,SAAc,GAAE,OAAO,CAAC,IAAI,CAAC;IAGrD,kBAAkB,IAAG,OAAO,CAAC,aAAa,CAAC;IAG3C,IAAI,MAAM,IAAG,MAAM,CAElB;IACD,IAAI,aAAa,IAAG,iBAAiB,CAEpC;IACK,kBAAkB,CAAC,MAAM,SAAI,EAAE,UAAU,SAAG,GAAI,OAAO,CAAC,UAAU,CAAC;IAGnE,aAAa,IAAG,OAAO,CAAC,GAAG,CAAC;CAGrC"}
|
|
@@ -12,7 +12,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.VanillaOidc = void 0;
|
|
13
13
|
const oidc_1 = require("./oidc");
|
|
14
14
|
const parseTokens_1 = require("./parseTokens");
|
|
15
|
-
const initWorker_1 = require("./initWorker");
|
|
16
15
|
class VanillaOidc {
|
|
17
16
|
constructor(oidc) {
|
|
18
17
|
this._oidc = oidc;
|
|
@@ -59,18 +58,7 @@ class VanillaOidc {
|
|
|
59
58
|
}
|
|
60
59
|
getValidTokenAsync(waitMs = 200, numberWait = 50) {
|
|
61
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
|
|
63
|
-
let numberWaitTemp = numberWait;
|
|
64
|
-
while (oidc.tokens && !(0, parseTokens_1.isTokensValid)(oidc.tokens) && numberWaitTemp > 0) {
|
|
65
|
-
yield (0, initWorker_1.sleepAsync)(200);
|
|
66
|
-
numberWaitTemp = numberWaitTemp - 1;
|
|
67
|
-
}
|
|
68
|
-
const isValid = !(0, parseTokens_1.isTokensValid)(oidc.tokens);
|
|
69
|
-
return {
|
|
70
|
-
isTokensValid: isValid,
|
|
71
|
-
tokens: oidc.tokens,
|
|
72
|
-
numberWaited: numberWaitTemp - numberWait
|
|
73
|
-
};
|
|
61
|
+
return (0, parseTokens_1.getValidTokenAsync)(this._oidc, waitMs, numberWait);
|
|
74
62
|
});
|
|
75
63
|
}
|
|
76
64
|
userInfoAsync() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vanillaOidc.js","sourceRoot":"","sources":["../../src/oidc/vanilla/vanillaOidc.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAyE;AACzE,+
|
|
1
|
+
{"version":3,"file":"vanillaOidc.js","sourceRoot":"","sources":["../../src/oidc/vanilla/vanillaOidc.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAyE;AACzE,+CAA4E;AAK5E,MAAa,WAAW;IAEpB,YAAY,IAAU;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACtB,CAAC;IACD,eAAe,CAAC,IAAa;QACzB,OAAO,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IACD,uBAAuB,CAAC,EAAS;QAC7B,IAAI,CAAC,KAAK,CAAC,uBAAuB,CAAC,EAAE,CAAC,CAAC;IAC3C,CAAC;IACD,YAAY,CAAC,SAAgB,EAAE,IAAQ;QACnC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IACD,MAAM,CAAC,WAAW,CAAC,aAA+B,EAAE,OAAY,SAAS;QACrE,O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AC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;QAC9D,CAAC;KAAA;IACK,aAAa;;YACf,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC;QACtC,CAAC;KAAA;;AAlDL,kCAmDC;AA/BU,sBAAU,GAAG,WAAI,CAAC,UAAU,CAAC"}
|
package/package.json
CHANGED
package/src/configurations.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {TokenRenewMode} from "./oidc";
|
|
2
|
+
|
|
1
3
|
export const configurationIdentityServer = {
|
|
2
4
|
client_id: 'interactive.public.short',
|
|
3
5
|
redirect_uri: window.location.origin + '/authentication/callback',
|
|
@@ -12,6 +14,7 @@ export const configurationIdentityServer = {
|
|
|
12
14
|
//storage: sessionStorage,
|
|
13
15
|
//silent_login_timeout: 3333000
|
|
14
16
|
//monitor_session: true,
|
|
17
|
+
token_renew_mode : TokenRenewMode.access_token_invalid
|
|
15
18
|
};
|
|
16
19
|
|
|
17
20
|
export const configurationIdentityServerWithHash = {
|
package/src/oidc/index.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { useOidcUser, OidcUserStatus} from "./User";
|
|
|
3
3
|
export { useOidc, useOidcAccessToken, useOidcIdToken } from "./ReactOidc";
|
|
4
4
|
export { withOidcFetch, useOidcFetch } from "./FetchToken";
|
|
5
5
|
export { OidcProvider } from "./OidcProvider";
|
|
6
|
+
export {TokenRenewMode } from "./vanilla/parseTokens";
|
|
@@ -100,6 +100,12 @@ const isTokensOidcValid =(tokens, nonce, oidcServerConfiguration) =>{
|
|
|
100
100
|
return true;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
const TokenRenewMode = {
|
|
104
|
+
access_token_or_id_token_invalid: "access_token_or_id_token_invalid",
|
|
105
|
+
access_token_invalid:"access_token_invalid",
|
|
106
|
+
id_token_invalid: "id_token_invalid"
|
|
107
|
+
}
|
|
108
|
+
|
|
103
109
|
function hideTokens(currentDatabaseElement) {
|
|
104
110
|
const configurationName = currentDatabaseElement.configurationName;
|
|
105
111
|
return (response) => {
|
|
@@ -136,7 +142,16 @@ function hideTokens(currentDatabaseElement) {
|
|
|
136
142
|
|
|
137
143
|
const idTokenExpiresAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
|
|
138
144
|
const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp)? accessTokenPayload.exp : tokens.issued_at + tokens.expires_in;
|
|
139
|
-
|
|
145
|
+
|
|
146
|
+
let expiresAt;
|
|
147
|
+
const tokenRenewMode = currentDatabaseElement.oidcConfiguration.token_renew_mode;
|
|
148
|
+
if (tokenRenewMode === TokenRenewMode.access_token_invalid) {
|
|
149
|
+
expiresAt = accessTokenExpiresAt;
|
|
150
|
+
} else if (tokenRenewMode === TokenRenewMode.id_token_invalid) {
|
|
151
|
+
expiresAt = idTokenExpiresAt;
|
|
152
|
+
} else {
|
|
153
|
+
expiresAt = idTokenExpiresAt < accessTokenExpiresAt ? idTokenExpiresAt : accessTokenExpiresAt;
|
|
154
|
+
}
|
|
140
155
|
secureTokens.expiresAt = expiresAt;
|
|
141
156
|
|
|
142
157
|
tokens.expiresAt = expiresAt;
|
|
@@ -390,6 +405,7 @@ addEventListener('message', event => {
|
|
|
390
405
|
tokens: null,
|
|
391
406
|
items:[],
|
|
392
407
|
oidcServerConfiguration: null,
|
|
408
|
+
oidcConfiguration:null,
|
|
393
409
|
status:null,
|
|
394
410
|
configurationName: configurationName,
|
|
395
411
|
};
|
|
@@ -411,7 +427,7 @@ addEventListener('message', event => {
|
|
|
411
427
|
return;
|
|
412
428
|
case "init":
|
|
413
429
|
const oidcServerConfiguration = data.data.oidcServerConfiguration;
|
|
414
|
-
|
|
430
|
+
const domains = trustedDomains[configurationName];
|
|
415
431
|
if (!domains.find(f => f === acceptAnyDomainToken)) {
|
|
416
432
|
checkDomain(domains, oidcServerConfiguration.tokenEndpoint);
|
|
417
433
|
checkDomain(domains, oidcServerConfiguration.revocationEndpoint);
|
|
@@ -419,6 +435,7 @@ addEventListener('message', event => {
|
|
|
419
435
|
checkDomain(domains, oidcServerConfiguration.issuer);
|
|
420
436
|
}
|
|
421
437
|
currentDatabase.oidcServerConfiguration = oidcServerConfiguration;
|
|
438
|
+
currentDatabase.oidcConfiguration = data.data.oidcConfiguration;
|
|
422
439
|
const where = data.data.where;
|
|
423
440
|
if(where === "loginCallbackAsync" || where === "tryKeepExistingSessionAsync") {
|
|
424
441
|
currentLoginCallbackConfigurationName = configurationName;
|
|
@@ -45,11 +45,6 @@ const keepAlive = () => {
|
|
|
45
45
|
} catch (error){console.log(error)}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
/*window.addEventListener('error', (event) => {
|
|
49
|
-
var textContent = `${event.type}: ${event.message}\n`;
|
|
50
|
-
console.log(textContent)
|
|
51
|
-
});*/
|
|
52
|
-
|
|
53
48
|
const isServiceWorkerProxyActiveAsync = () => {
|
|
54
49
|
try {
|
|
55
50
|
return fetch('/OidcKeepAliveServiceWorker.json', {
|
|
@@ -121,10 +116,10 @@ export const initWorkerAsync = async(serviceWorkerRelativeUrl, configurationName
|
|
|
121
116
|
const clearAsync=(status) =>{
|
|
122
117
|
return sendMessageAsync(registration)({type: "clear", data: {status}, configurationName});
|
|
123
118
|
}
|
|
124
|
-
const initAsync= async (oidcServerConfiguration, where) => {
|
|
119
|
+
const initAsync= async (oidcServerConfiguration, where, oidcConfiguration) => {
|
|
125
120
|
const result = await sendMessageAsync(registration)({
|
|
126
121
|
type: "init",
|
|
127
|
-
data: {oidcServerConfiguration, where},
|
|
122
|
+
data: {oidcServerConfiguration, where, oidcConfiguration},
|
|
128
123
|
configurationName
|
|
129
124
|
});
|
|
130
125
|
// @ts-ignore
|
|
@@ -163,7 +158,6 @@ export const initWorkerAsync = async(serviceWorkerRelativeUrl, configurationName
|
|
|
163
158
|
loadItemsAsync,
|
|
164
159
|
clearAsync,
|
|
165
160
|
initAsync,
|
|
166
|
-
// getAccessTokenPayloadAsync,
|
|
167
161
|
startKeepAliveServiceWorker,
|
|
168
162
|
isServiceWorkerProxyActiveAsync,
|
|
169
163
|
setSessionStateAsync,
|
package/src/oidc/vanilla/oidc.ts
CHANGED
|
@@ -19,7 +19,15 @@ import timer from './timer';
|
|
|
19
19
|
import {CheckSessionIFrame} from "./checkSessionIFrame"
|
|
20
20
|
import {getParseQueryStringFromLocation} from "./route-utils";
|
|
21
21
|
import {AuthorizationServiceConfigurationJson} from "@openid/appauth/src/authorization_service_configuration";
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
computeTimeLeft,
|
|
24
|
+
isTokensOidcValid,
|
|
25
|
+
isTokensValid,
|
|
26
|
+
parseOriginalTokens,
|
|
27
|
+
setTokens, TokenRenewMode,
|
|
28
|
+
TokenRenewModeType,
|
|
29
|
+
Tokens
|
|
30
|
+
} from "./parseTokens";
|
|
23
31
|
|
|
24
32
|
const TOKEN_TYPE ={
|
|
25
33
|
refresh_token:"refresh_token",
|
|
@@ -56,7 +64,7 @@ const performRevocationRequestAsync= async (url, token, token_type=TOKEN_TYPE.re
|
|
|
56
64
|
};
|
|
57
65
|
}
|
|
58
66
|
|
|
59
|
-
const performTokenRequestAsync= async (url, details, extras, oldTokens) => {
|
|
67
|
+
const performTokenRequestAsync= async (url, details, extras, oldTokens, tokenRenewMode: string) => {
|
|
60
68
|
for (let [key, value] of Object.entries(extras)) {
|
|
61
69
|
if (details[key] === undefined) {
|
|
62
70
|
details[key] = value;
|
|
@@ -84,7 +92,7 @@ const performTokenRequestAsync= async (url, details, extras, oldTokens) => {
|
|
|
84
92
|
const tokens = await response.json();
|
|
85
93
|
return {
|
|
86
94
|
success : true,
|
|
87
|
-
data: parseOriginalTokens(tokens, oldTokens)
|
|
95
|
+
data: parseOriginalTokens(tokens, oldTokens,tokenRenewMode)
|
|
88
96
|
};
|
|
89
97
|
}
|
|
90
98
|
|
|
@@ -175,6 +183,7 @@ export interface AuthorityConfiguration {
|
|
|
175
183
|
token_request_extras?:StringMap,
|
|
176
184
|
storage?: Storage
|
|
177
185
|
monitor_session?: boolean
|
|
186
|
+
token_renew_mode?: string
|
|
178
187
|
};
|
|
179
188
|
|
|
180
189
|
const oidcDatabase = {};
|
|
@@ -363,19 +372,20 @@ export class Oidc {
|
|
|
363
372
|
private configurationName: string;
|
|
364
373
|
private checkSessionIFrame: CheckSessionIFrame;
|
|
365
374
|
constructor(configuration:OidcConfiguration, configurationName="default") {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
this.
|
|
375
|
+
let silent_login_uri = configuration.silent_login_uri;
|
|
376
|
+
if(configuration.silent_redirect_uri && !configuration.silent_login_uri){
|
|
377
|
+
silent_login_uri = `${configuration.silent_redirect_uri.replace("-callback", "").replace("callback", "")}-login`;
|
|
378
|
+
}
|
|
379
|
+
this.configuration = {
|
|
380
|
+
...configuration,
|
|
381
|
+
silent_login_uri,
|
|
382
|
+
monitor_session: configuration.monitor_session ?? false,
|
|
383
|
+
refresh_time_before_tokens_expiration_in_second : configuration.refresh_time_before_tokens_expiration_in_second ?? 60,
|
|
384
|
+
silent_login_timeout: configuration.silent_login_timeout ?? 12000,
|
|
385
|
+
token_renew_mode : configuration.token_renew_mode ?? TokenRenewMode.access_token_or_id_token_invalid
|
|
386
|
+
};
|
|
387
|
+
this.configurationName= configurationName;
|
|
388
|
+
this.tokens = null;
|
|
379
389
|
this.userInfo = null;
|
|
380
390
|
this.events = [];
|
|
381
391
|
this.timeoutId = null;
|
|
@@ -390,21 +400,17 @@ export class Oidc {
|
|
|
390
400
|
this.destroyAsync.bind(this);
|
|
391
401
|
this.logoutAsync.bind(this);
|
|
392
402
|
this.renewTokensAsync.bind(this);
|
|
393
|
-
|
|
394
403
|
this.initAsync(this.configuration.authority, this.configuration.authority_configuration);
|
|
395
404
|
}
|
|
396
|
-
|
|
397
405
|
subscriveEvents(func):string{
|
|
398
406
|
const id = getRandomInt(9999999999999).toString();
|
|
399
407
|
this.events.push({id, func});
|
|
400
408
|
return id;
|
|
401
409
|
}
|
|
402
|
-
|
|
403
410
|
removeEventSubscription(id) :void{
|
|
404
411
|
const newEvents = this.events.filter(e => e.id !== id);
|
|
405
412
|
this.events = newEvents;
|
|
406
413
|
}
|
|
407
|
-
|
|
408
414
|
publishEvent(eventName, data){
|
|
409
415
|
this.events.forEach(event => {
|
|
410
416
|
event.func(eventName, data)
|
|
@@ -435,7 +441,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
435
441
|
window.top.postMessage(`${this.configurationName}_oidc_error:${JSON.stringify({error: queryParams.error})}`, window.location.origin);
|
|
436
442
|
}
|
|
437
443
|
}
|
|
438
|
-
|
|
439
444
|
async silentLoginCallbackAsync() {
|
|
440
445
|
try {
|
|
441
446
|
await this.loginCallbackAsync(true);
|
|
@@ -445,7 +450,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
445
450
|
this._silentLoginErrorCallbackFromIFrame();
|
|
446
451
|
}
|
|
447
452
|
}
|
|
448
|
-
|
|
449
453
|
async silentLoginAsync(extras:StringMap=null, state:string=null, scope:string=null) {
|
|
450
454
|
if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
|
|
451
455
|
return Promise.resolve(null);
|
|
@@ -585,7 +589,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
585
589
|
const oidcServerConfiguration = await this.initAsync(configuration.authority, configuration.authority_configuration);
|
|
586
590
|
serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
|
|
587
591
|
if (serviceWorker) {
|
|
588
|
-
const {tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync");
|
|
592
|
+
const {tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync", configuration);
|
|
589
593
|
if (tokens) {
|
|
590
594
|
serviceWorker.startKeepAliveServiceWorker();
|
|
591
595
|
// @ts-ignore
|
|
@@ -649,7 +653,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
649
653
|
return result;
|
|
650
654
|
});
|
|
651
655
|
}
|
|
652
|
-
|
|
653
656
|
loginPromise: Promise<void>=null;
|
|
654
657
|
async loginAsync(callbackPath:string=undefined, extras:StringMap=null, isSilentSignin:boolean=false, scope:string=undefined, silentLoginOnly = false) {
|
|
655
658
|
if(this.loginPromise !== null){
|
|
@@ -684,6 +687,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
684
687
|
}
|
|
685
688
|
}
|
|
686
689
|
this.publishEvent(eventNames.loginAsync_begin, {});
|
|
690
|
+
|
|
687
691
|
try {
|
|
688
692
|
const redirectUri = isSilentSignin ? configuration.silent_redirect_uri : configuration.redirect_uri;
|
|
689
693
|
if (!scope) {
|
|
@@ -701,7 +705,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
701
705
|
let storage;
|
|
702
706
|
if (serviceWorker) {
|
|
703
707
|
serviceWorker.startKeepAliveServiceWorker();
|
|
704
|
-
await serviceWorker.initAsync(oidcServerConfiguration, "loginAsync");
|
|
708
|
+
await serviceWorker.initAsync(oidcServerConfiguration, "loginAsync", configuration);
|
|
705
709
|
await serviceWorker.setNonceAsync(nonce);
|
|
706
710
|
storage = new MemoryStorageBackend(serviceWorker.saveItemsAsync, {});
|
|
707
711
|
await storage.setItem("dummy", {});
|
|
@@ -838,7 +842,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
838
842
|
let nonceData = null;
|
|
839
843
|
if(serviceWorker){
|
|
840
844
|
serviceWorker.startKeepAliveServiceWorker();
|
|
841
|
-
await serviceWorker.initAsync(oidcServerConfiguration, "loginCallbackAsync");
|
|
845
|
+
await serviceWorker.initAsync(oidcServerConfiguration, "loginCallbackAsync", configuration);
|
|
842
846
|
const items = await serviceWorker.loadItemsAsync();
|
|
843
847
|
storage = new MemoryStorageBackend(serviceWorker.saveItemsAsync, items);
|
|
844
848
|
const dummy =await storage.getItem("dummy");
|
|
@@ -914,10 +918,10 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
914
918
|
const loginParams = getLoginParams(this.configurationName, redirectUri);
|
|
915
919
|
let formattedTokens = null;
|
|
916
920
|
if (serviceWorker) {
|
|
917
|
-
const {tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync");
|
|
921
|
+
const {tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync", configuration);
|
|
918
922
|
formattedTokens = tokens;
|
|
919
923
|
} else{
|
|
920
|
-
formattedTokens = setTokens(tokenResponse);
|
|
924
|
+
formattedTokens = setTokens(tokenResponse, null, configuration.token_renew_mode);
|
|
921
925
|
}
|
|
922
926
|
if(!isTokensOidcValid(formattedTokens, nonceData.nonce, oidcServerConfiguration)){
|
|
923
927
|
const exception = new Error("Tokens are not OpenID valid");
|
|
@@ -1050,7 +1054,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
1050
1054
|
refresh_token: tokens.refreshToken,
|
|
1051
1055
|
};
|
|
1052
1056
|
const oidcServerConfiguration = await this.initAsync(authority, configuration.authority_configuration);
|
|
1053
|
-
const tokenResponse = await performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, finalExtras, tokens);
|
|
1057
|
+
const tokenResponse = await performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, finalExtras, tokens, configuration.token_renew_mode);
|
|
1054
1058
|
if (tokenResponse.success) {
|
|
1055
1059
|
if(!isTokensOidcValid(tokenResponse.data, nonce.nonce, oidcServerConfiguration)){
|
|
1056
1060
|
updateTokens(null);
|
|
@@ -1091,7 +1095,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
|
|
|
1091
1095
|
const oidcServerConfiguration = await this.initAsync(configuration.authority, configuration.authority_configuration);
|
|
1092
1096
|
const serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, configurationName);
|
|
1093
1097
|
if (serviceWorker) {
|
|
1094
|
-
const {status, tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync");
|
|
1098
|
+
const {status, tokens} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync", configuration);
|
|
1095
1099
|
if (status == "LOGGED_OUT") {
|
|
1096
1100
|
return {tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB", nonce: nullNonce};
|
|
1097
1101
|
}else if (status == "SESSIONS_LOST") {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {getValidTokenAsync, isTokensOidcValid} from "./parseTokens";
|
|
2
|
+
|
|
3
|
+
describe('ParseTokens test Suite', () => {
|
|
4
|
+
const currentTimeUnixSecond = new Date().getTime() / 1000;
|
|
5
|
+
describe.each([
|
|
6
|
+
[currentTimeUnixSecond + 120, currentTimeUnixSecond - 10, true],
|
|
7
|
+
[currentTimeUnixSecond - 20, currentTimeUnixSecond - 50, false],
|
|
8
|
+
])('getValidTokenAsync', (expiresAt, issuedAt, expectIsValidToken) => {
|
|
9
|
+
it('should getValidTokenAsync wait and return value', async () => {
|
|
10
|
+
const oidc = {
|
|
11
|
+
tokens: {
|
|
12
|
+
refreshToken: 'youhou',
|
|
13
|
+
idTokenPayload: null,
|
|
14
|
+
idToken: 'youhou',
|
|
15
|
+
accessTokenPayload: null,
|
|
16
|
+
accessToken: 'youhou',
|
|
17
|
+
expiresAt: expiresAt,
|
|
18
|
+
issuedAt: issuedAt,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const result = await getValidTokenAsync(oidc, 1, 1);
|
|
22
|
+
expect(result.isTokensValid).toEqual(expectIsValidToken);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const idTokenPayload = {iss: "toto", exp: currentTimeUnixSecond +900, iat: currentTimeUnixSecond -900, nonce: "nonce"};
|
|
28
|
+
const oidcServerConfiguration = {issuer:"toto"};
|
|
29
|
+
const idTokenPayloadExpired = {...idTokenPayload, exp: currentTimeUnixSecond-20};
|
|
30
|
+
const idTokenPayloadIssuedTooLongTimeAgo = {...idTokenPayload, iat: currentTimeUnixSecond-20000000};
|
|
31
|
+
|
|
32
|
+
describe.each([
|
|
33
|
+
[idTokenPayload, "nonce", oidcServerConfiguration, true, "success"],
|
|
34
|
+
[idTokenPayload, "other_nonce", oidcServerConfiguration, false, "bad nonce"],
|
|
35
|
+
[idTokenPayload, "nonce", {issuer:"tutu"}, false, "different issuer"],
|
|
36
|
+
[idTokenPayloadExpired, "nonce", oidcServerConfiguration, false, "id token expired issuer"],
|
|
37
|
+
[idTokenPayloadIssuedTooLongTimeAgo, "nonce", oidcServerConfiguration, false, "id token expired issuer"],
|
|
38
|
+
])('isTokensOidcValid', (idTokenPayload, nonce, oidcServerConfiguration, expectIsValidToken, status) => {
|
|
39
|
+
it('should isTokensOidcValid return ' + status, async () => {
|
|
40
|
+
const oidc = {
|
|
41
|
+
idTokenPayload
|
|
42
|
+
}
|
|
43
|
+
const isValid = await isTokensOidcValid(oidc, nonce, oidcServerConfiguration);
|
|
44
|
+
expect(isValid).toEqual(expectIsValidToken);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import {sleepAsync} from "./initWorker";
|
|
2
|
+
|
|
2
3
|
|
|
3
4
|
const b64DecodeUnicode = (str) =>
|
|
4
5
|
decodeURIComponent(Array.prototype.map.call(atob(str), (c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
|
|
@@ -34,7 +35,19 @@ export type Tokens = {
|
|
|
34
35
|
issuedAt: number
|
|
35
36
|
};
|
|
36
37
|
|
|
37
|
-
export
|
|
38
|
+
export type TokenRenewModeType = {
|
|
39
|
+
access_token_or_id_token_invalid: string,
|
|
40
|
+
access_token_invalid:string,
|
|
41
|
+
id_token_invalid: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const TokenRenewMode = {
|
|
45
|
+
access_token_or_id_token_invalid: "access_token_or_id_token_invalid",
|
|
46
|
+
access_token_invalid:"access_token_invalid",
|
|
47
|
+
id_token_invalid: "id_token_invalid"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const setTokens = (tokens, oldTokens=null, tokenRenewMode: string):Tokens =>{
|
|
38
51
|
|
|
39
52
|
if(!tokens){
|
|
40
53
|
return null;
|
|
@@ -56,7 +69,16 @@ export const setTokens = (tokens, oldTokens=null):Tokens =>{
|
|
|
56
69
|
|
|
57
70
|
const idTokenExpireAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
|
|
58
71
|
const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp)? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
|
|
59
|
-
|
|
72
|
+
|
|
73
|
+
let expiresAt;
|
|
74
|
+
|
|
75
|
+
if (tokenRenewMode === TokenRenewMode.access_token_invalid) {
|
|
76
|
+
expiresAt = accessTokenExpiresAt;
|
|
77
|
+
} else if (tokenRenewMode === TokenRenewMode.id_token_invalid) {
|
|
78
|
+
expiresAt = idTokenExpireAt;
|
|
79
|
+
} else {
|
|
80
|
+
expiresAt = idTokenExpireAt < accessTokenExpiresAt ? idTokenExpireAt : accessTokenExpiresAt;
|
|
81
|
+
}
|
|
60
82
|
|
|
61
83
|
const newTokens = {...tokens, idTokenPayload: _idTokenPayload, accessTokenPayload, expiresAt};
|
|
62
84
|
// When refresh_token is not rotated we reuse ald refresh_token
|
|
@@ -70,7 +92,7 @@ export const setTokens = (tokens, oldTokens=null):Tokens =>{
|
|
|
70
92
|
|
|
71
93
|
|
|
72
94
|
|
|
73
|
-
export const parseOriginalTokens= (tokens, oldTokens) =>{
|
|
95
|
+
export const parseOriginalTokens= (tokens, oldTokens, tokenRenewMode: string ) =>{
|
|
74
96
|
if(!tokens){
|
|
75
97
|
return null;
|
|
76
98
|
}
|
|
@@ -92,8 +114,7 @@ export const parseOriginalTokens= (tokens, oldTokens) =>{
|
|
|
92
114
|
// @ts-ignore
|
|
93
115
|
data.refreshToken= tokens.refresh_token;
|
|
94
116
|
}
|
|
95
|
-
|
|
96
|
-
|
|
117
|
+
|
|
97
118
|
if(tokens.accessTokenPayload !== undefined){
|
|
98
119
|
// @ts-ignore
|
|
99
120
|
data.accessTokenPayload = tokens.accessTokenPayload;
|
|
@@ -104,7 +125,7 @@ export const parseOriginalTokens= (tokens, oldTokens) =>{
|
|
|
104
125
|
data.idTokenPayload = tokens.idTokenPayload;
|
|
105
126
|
}
|
|
106
127
|
|
|
107
|
-
return setTokens(data, oldTokens);
|
|
128
|
+
return setTokens(data, oldTokens, tokenRenewMode);
|
|
108
129
|
}
|
|
109
130
|
|
|
110
131
|
export const computeTimeLeft = (refreshTimeBeforeTokensExpirationInSecond, expiresAt)=>{
|
|
@@ -119,6 +140,31 @@ export const isTokensValid= (tokens) =>{
|
|
|
119
140
|
return computeTimeLeft(0, tokens.expiresAt) > 0;
|
|
120
141
|
}
|
|
121
142
|
|
|
143
|
+
|
|
144
|
+
export type ValidToken = {
|
|
145
|
+
isTokensValid: Boolean,
|
|
146
|
+
tokens: Tokens,
|
|
147
|
+
numberWaited: Number
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface OidcToken{
|
|
151
|
+
tokens?: Tokens;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export const getValidTokenAsync = async (oidc: OidcToken, waitMs = 200, numberWait = 50): Promise<ValidToken> => {
|
|
155
|
+
let numberWaitTemp = numberWait;
|
|
156
|
+
while (!isTokensValid(oidc.tokens) && numberWaitTemp > 0) {
|
|
157
|
+
await sleepAsync(200);
|
|
158
|
+
numberWaitTemp = numberWaitTemp - 1;
|
|
159
|
+
}
|
|
160
|
+
const isValid = isTokensValid(oidc.tokens);
|
|
161
|
+
return {
|
|
162
|
+
isTokensValid: isValid,
|
|
163
|
+
tokens: oidc.tokens,
|
|
164
|
+
numberWaited: numberWaitTemp - numberWait
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
122
168
|
// https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation (excluding rules #1, #4, #5, #7, #8, #12, and #13 which did not apply).
|
|
123
169
|
// https://github.com/openid/AppAuth-JS/issues/65
|
|
124
170
|
export const isTokensOidcValid =(tokens, nonce, oidcServerConfiguration) =>{
|