oidc-spa 6.15.1 → 7.0.1
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 +12 -13
- package/core/Oidc.d.ts +24 -12
- package/core/createOidc.d.ts +15 -30
- package/core/createOidc.js +184 -146
- package/core/createOidc.js.map +1 -1
- package/core/handleOidcCallback.js +2 -29
- package/core/handleOidcCallback.js.map +1 -1
- package/core/loginOrGoToAuthServer.d.ts +1 -2
- package/core/loginOrGoToAuthServer.js +10 -10
- package/core/loginOrGoToAuthServer.js.map +1 -1
- package/core/loginSilent.d.ts +1 -1
- package/core/loginSilent.js +4 -4
- package/core/loginSilent.js.map +1 -1
- package/core/oidcClientTsUserToTokens.d.ts +1 -2
- package/core/oidcClientTsUserToTokens.js +93 -58
- package/core/oidcClientTsUserToTokens.js.map +1 -1
- package/mock/oidc.d.ts +1 -1
- package/mock/oidc.js +29 -19
- package/mock/oidc.js.map +1 -1
- package/package.json +1 -5
- package/react/react.d.ts +9 -14
- package/react/react.js +32 -60
- package/react/react.js.map +1 -1
- package/src/core/Oidc.ts +27 -14
- package/src/core/createOidc.ts +189 -149
- package/src/core/handleOidcCallback.ts +2 -55
- package/src/core/loginOrGoToAuthServer.ts +10 -11
- package/src/core/loginSilent.ts +4 -4
- package/src/core/oidcClientTsUserToTokens.ts +129 -82
- package/src/mock/oidc.ts +16 -6
- package/src/react/react.tsx +52 -80
- package/src/tools/readExpirationTimeInJwt.ts +4 -5
- package/src/tools/startCountdown.ts +4 -5
- package/tools/readExpirationTimeInJwt.js +4 -4
- package/tools/readExpirationTimeInJwt.js.map +1 -1
- package/tools/startCountdown.d.ts +3 -2
- package/tools/startCountdown.js +4 -4
- package/tools/startCountdown.js.map +1 -1
- package/vendor/frontend/oidc-client-ts-and-jwt-decode.js +1 -1
- package/core/debug966975.d.ts +0 -7
- package/core/debug966975.js +0 -88
- package/core/debug966975.js.map +0 -1
- package/src/core/debug966975.ts +0 -85
package/src/react/react.tsx
CHANGED
|
@@ -3,8 +3,6 @@ import {
|
|
|
3
3
|
useState,
|
|
4
4
|
createContext,
|
|
5
5
|
useContext,
|
|
6
|
-
useReducer,
|
|
7
|
-
useRef,
|
|
8
6
|
type ReactNode,
|
|
9
7
|
type ComponentType,
|
|
10
8
|
type FC,
|
|
@@ -21,7 +19,11 @@ export type OidcReact<DecodedIdToken extends Record<string, unknown>> =
|
|
|
21
19
|
| OidcReact.LoggedIn<DecodedIdToken>;
|
|
22
20
|
|
|
23
21
|
export namespace OidcReact {
|
|
24
|
-
export type Common = Oidc.Common
|
|
22
|
+
export type Common = Oidc.Common & {
|
|
23
|
+
useLogoutWarningCountdown: (params: { warningDurationSeconds: number }) => {
|
|
24
|
+
secondsLeft: number | undefined;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
25
27
|
|
|
26
28
|
export type NotLoggedIn = Common & {
|
|
27
29
|
isUserLoggedIn: false;
|
|
@@ -33,12 +35,9 @@ export namespace OidcReact {
|
|
|
33
35
|
}) => Promise<never>;
|
|
34
36
|
initializationError: OidcInitializationError | undefined;
|
|
35
37
|
|
|
36
|
-
/** @deprecated: Use `const { decodedIdToken, tokens} = useOidc();` */
|
|
37
|
-
oidcTokens?: never;
|
|
38
38
|
decodedIdToken?: never;
|
|
39
|
-
tokens?: never;
|
|
40
39
|
logout?: never;
|
|
41
|
-
|
|
40
|
+
renewTokens?: never;
|
|
42
41
|
goToAuthServer?: never;
|
|
43
42
|
backFromAuthServer?: never;
|
|
44
43
|
isNewBrowserSession?: never;
|
|
@@ -46,16 +45,9 @@ export namespace OidcReact {
|
|
|
46
45
|
|
|
47
46
|
export type LoggedIn<DecodedIdToken extends Record<string, unknown>> = Common & {
|
|
48
47
|
isUserLoggedIn: true;
|
|
49
|
-
/** @deprecated: Use `const { decodedIdToken, tokens} = useOidc();` */
|
|
50
|
-
oidcTokens: Oidc.Tokens<DecodedIdToken>;
|
|
51
48
|
decodedIdToken: DecodedIdToken;
|
|
52
|
-
tokens: Oidc.Tokens<DecodedIdToken> | undefined;
|
|
53
49
|
logout: Oidc.LoggedIn["logout"];
|
|
54
50
|
renewTokens: Oidc.LoggedIn["renewTokens"];
|
|
55
|
-
subscribeToAutoLogoutCountdown: (
|
|
56
|
-
tickCallback: (params: { secondsLeft: number | undefined }) => void
|
|
57
|
-
) => { unsubscribeFromAutoLogoutCountdown: () => void };
|
|
58
|
-
|
|
59
51
|
login?: never;
|
|
60
52
|
initializationError?: never;
|
|
61
53
|
goToAuthServer: (params: {
|
|
@@ -220,6 +212,39 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
220
212
|
);
|
|
221
213
|
}
|
|
222
214
|
|
|
215
|
+
const useLogoutWarningCountdown: OidcReact.LoggedIn<DecodedIdToken>["useLogoutWarningCountdown"] = ({
|
|
216
|
+
warningDurationSeconds
|
|
217
|
+
}) => {
|
|
218
|
+
const contextValue = useContext(oidcContext);
|
|
219
|
+
|
|
220
|
+
assert(contextValue !== undefined);
|
|
221
|
+
|
|
222
|
+
const { oidc } = contextValue;
|
|
223
|
+
|
|
224
|
+
const [secondsLeft, setSecondsLeft] = useState<number | undefined>(undefined);
|
|
225
|
+
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
if (!oidc.isUserLoggedIn) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const { unsubscribeFromAutoLogoutCountdown } = oidc.subscribeToAutoLogoutCountdown(
|
|
232
|
+
({ secondsLeft }) =>
|
|
233
|
+
setSecondsLeft(
|
|
234
|
+
secondsLeft === undefined || secondsLeft > warningDurationSeconds
|
|
235
|
+
? undefined
|
|
236
|
+
: secondsLeft
|
|
237
|
+
)
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return () => {
|
|
241
|
+
unsubscribeFromAutoLogoutCountdown();
|
|
242
|
+
};
|
|
243
|
+
}, [warningDurationSeconds]);
|
|
244
|
+
|
|
245
|
+
return { secondsLeft };
|
|
246
|
+
};
|
|
247
|
+
|
|
223
248
|
function useOidc(params?: {
|
|
224
249
|
assert?: "user logged in" | "user not logged in";
|
|
225
250
|
}): OidcReact<DecodedIdToken> {
|
|
@@ -259,66 +284,27 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
259
284
|
}
|
|
260
285
|
}
|
|
261
286
|
|
|
262
|
-
const [,
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (!oidc.isUserLoggedIn) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const { unsubscribe } = oidc.subscribeToTokensChange(forceUpdate);
|
|
270
|
-
|
|
271
|
-
return unsubscribe;
|
|
272
|
-
}, [oidc]);
|
|
273
|
-
|
|
274
|
-
const tokensState_ref = useRef<{
|
|
275
|
-
isConsumerReadingTokens: boolean;
|
|
276
|
-
tokens: Oidc.Tokens<DecodedIdToken> | undefined;
|
|
277
|
-
}>({
|
|
278
|
-
isConsumerReadingTokens: false,
|
|
279
|
-
tokens: undefined
|
|
280
|
-
});
|
|
287
|
+
const [, reRenderIfDecodedIdTokenChanged] = useState(
|
|
288
|
+
!oidc.isUserLoggedIn ? undefined : oidc.getDecodedIdToken()
|
|
289
|
+
);
|
|
281
290
|
|
|
282
291
|
useEffect(() => {
|
|
283
292
|
if (!oidc.isUserLoggedIn) {
|
|
284
293
|
return;
|
|
285
294
|
}
|
|
286
295
|
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const tokenState = tokensState_ref.current;
|
|
293
|
-
|
|
294
|
-
tokenState.tokens = tokens;
|
|
295
|
-
|
|
296
|
-
if (tokenState.isConsumerReadingTokens) {
|
|
297
|
-
forceUpdate();
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
let isActive = true;
|
|
302
|
-
|
|
303
|
-
oidc.getTokens_next().then(tokens => {
|
|
304
|
-
if (!isActive) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
updateTokens(tokens);
|
|
308
|
-
});
|
|
296
|
+
const { unsubscribe } = oidc.subscribeToTokensChange(() =>
|
|
297
|
+
reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken())
|
|
298
|
+
);
|
|
309
299
|
|
|
310
|
-
|
|
311
|
-
updateTokens(tokens);
|
|
312
|
-
});
|
|
300
|
+
reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken());
|
|
313
301
|
|
|
314
|
-
return
|
|
315
|
-
isActive = false;
|
|
316
|
-
unsubscribe();
|
|
317
|
-
};
|
|
302
|
+
return unsubscribe;
|
|
318
303
|
}, []);
|
|
319
304
|
|
|
320
305
|
const common: OidcReact.Common = {
|
|
321
|
-
params: oidc.params
|
|
306
|
+
params: oidc.params,
|
|
307
|
+
useLogoutWarningCountdown
|
|
322
308
|
};
|
|
323
309
|
|
|
324
310
|
if (!oidc.isUserLoggedIn) {
|
|
@@ -334,16 +320,9 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
334
320
|
const oidcReact: OidcReact.LoggedIn<DecodedIdToken> = {
|
|
335
321
|
...common,
|
|
336
322
|
isUserLoggedIn: true,
|
|
337
|
-
oidcTokens: oidc.getTokens(),
|
|
338
323
|
decodedIdToken: oidc.getDecodedIdToken(),
|
|
339
|
-
get tokens() {
|
|
340
|
-
const tokensState = tokensState_ref.current;
|
|
341
|
-
tokensState.isConsumerReadingTokens = true;
|
|
342
|
-
return tokensState.tokens;
|
|
343
|
-
},
|
|
344
324
|
logout: oidc.logout,
|
|
345
325
|
renewTokens: oidc.renewTokens,
|
|
346
|
-
subscribeToAutoLogoutCountdown: oidc.subscribeToAutoLogoutCountdown,
|
|
347
326
|
goToAuthServer: oidc.goToAuthServer,
|
|
348
327
|
isNewBrowserSession: oidc.isNewBrowserSession,
|
|
349
328
|
backFromAuthServer: oidc.backFromAuthServer
|
|
@@ -422,17 +401,10 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
422
401
|
return oidc;
|
|
423
402
|
});
|
|
424
403
|
|
|
425
|
-
|
|
404
|
+
function getOidc(): Promise<Oidc<DecodedIdToken>> {
|
|
426
405
|
dReadyToCreate.resolve();
|
|
427
406
|
|
|
428
|
-
|
|
429
|
-
const oidc = await prOidc;
|
|
430
|
-
|
|
431
|
-
if (oidc.isUserLoggedIn) {
|
|
432
|
-
await oidc.getTokens_next();
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return oidc;
|
|
407
|
+
return prOidc;
|
|
436
408
|
}
|
|
437
409
|
|
|
438
410
|
const oidcReact: OidcReactApi<DecodedIdToken, false> = {
|
|
@@ -449,7 +421,7 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
449
421
|
|
|
450
422
|
/** @see: https://docs.oidc-spa.dev/v/v6/usage#react-api */
|
|
451
423
|
export function createReactOidc<
|
|
452
|
-
DecodedIdToken extends Record<string, unknown> =
|
|
424
|
+
DecodedIdToken extends Record<string, unknown> = Oidc.Tokens.DecodedIdToken_base,
|
|
453
425
|
AutoLogin extends boolean = false
|
|
454
426
|
>(params: ValueOrAsyncGetter<ParamsOfCreateOidc<DecodedIdToken, AutoLogin>>) {
|
|
455
427
|
return createOidcReactApi_dependencyInjection(params, createOidc);
|
|
@@ -3,15 +3,14 @@ import { assert } from "../vendor/frontend/tsafe";
|
|
|
3
3
|
|
|
4
4
|
// Return undefined if token provided wasn't a JWT or if it hasn't an exp claim number
|
|
5
5
|
export function readExpirationTimeInJwt(token: string): number | undefined {
|
|
6
|
-
let
|
|
6
|
+
let exp: number;
|
|
7
7
|
|
|
8
8
|
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
assert(typeof expirationTime === "number" && !isNaN(expirationTime));
|
|
9
|
+
exp = decodeJwt<{ exp: number }>(token).exp;
|
|
10
|
+
assert(typeof exp === "number");
|
|
12
11
|
} catch {
|
|
13
12
|
return undefined;
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
return
|
|
15
|
+
return exp * 1000;
|
|
17
16
|
}
|
|
@@ -2,17 +2,16 @@ import { setTimeout, clearTimeout } from "../tools/workerTimers";
|
|
|
2
2
|
|
|
3
3
|
export function createStartCountdown(params: {
|
|
4
4
|
tickCallback: (params: { secondsLeft: number | undefined }) => void;
|
|
5
|
-
getCountdownEndTime: () => number;
|
|
6
5
|
}) {
|
|
7
|
-
const {
|
|
6
|
+
const { tickCallback } = params;
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
function startCountdown(params: { countDownFromSeconds: number }) {
|
|
9
|
+
const { countDownFromSeconds } = params;
|
|
10
10
|
|
|
11
|
-
function startCountdown() {
|
|
12
11
|
let timer: ReturnType<typeof setTimeout>;
|
|
13
12
|
|
|
14
13
|
(async () => {
|
|
15
|
-
let secondsLeft = Math.floor(
|
|
14
|
+
let secondsLeft = Math.floor(countDownFromSeconds);
|
|
16
15
|
|
|
17
16
|
while (secondsLeft >= 0) {
|
|
18
17
|
tickCallback({ secondsLeft });
|
|
@@ -5,14 +5,14 @@ var decodeJwt_1 = require("./decodeJwt");
|
|
|
5
5
|
var tsafe_1 = require("../vendor/frontend/tsafe");
|
|
6
6
|
// Return undefined if token provided wasn't a JWT or if it hasn't an exp claim number
|
|
7
7
|
function readExpirationTimeInJwt(token) {
|
|
8
|
-
var
|
|
8
|
+
var exp;
|
|
9
9
|
try {
|
|
10
|
-
|
|
11
|
-
(0, tsafe_1.assert)(typeof
|
|
10
|
+
exp = (0, decodeJwt_1.decodeJwt)(token).exp;
|
|
11
|
+
(0, tsafe_1.assert)(typeof exp === "number");
|
|
12
12
|
}
|
|
13
13
|
catch (_a) {
|
|
14
14
|
return undefined;
|
|
15
15
|
}
|
|
16
|
-
return
|
|
16
|
+
return exp * 1000;
|
|
17
17
|
}
|
|
18
18
|
//# sourceMappingURL=readExpirationTimeInJwt.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readExpirationTimeInJwt.js","sourceRoot":"","sources":["../src/tools/readExpirationTimeInJwt.ts"],"names":[],"mappings":";;AAIA,
|
|
1
|
+
{"version":3,"file":"readExpirationTimeInJwt.js","sourceRoot":"","sources":["../src/tools/readExpirationTimeInJwt.ts"],"names":[],"mappings":";;AAIA,0DAWC;AAfD,yCAAwC;AACxC,kDAAkD;AAElD,sFAAsF;AACtF,SAAgB,uBAAuB,CAAC,KAAa;IACjD,IAAI,GAAW,CAAC;IAEhB,IAAI,CAAC;QACD,GAAG,GAAG,IAAA,qBAAS,EAAkB,KAAK,CAAC,CAAC,GAAG,CAAC;QAC5C,IAAA,cAAM,EAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC;IACpC,CAAC;IAAC,WAAM,CAAC;QACL,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,OAAO,GAAG,GAAG,IAAI,CAAC;AACtB,CAAC"}
|
|
@@ -2,9 +2,10 @@ export declare function createStartCountdown(params: {
|
|
|
2
2
|
tickCallback: (params: {
|
|
3
3
|
secondsLeft: number | undefined;
|
|
4
4
|
}) => void;
|
|
5
|
-
getCountdownEndTime: () => number;
|
|
6
5
|
}): {
|
|
7
|
-
startCountdown: (
|
|
6
|
+
startCountdown: (params: {
|
|
7
|
+
countDownFromSeconds: number;
|
|
8
|
+
}) => {
|
|
8
9
|
stopCountdown: () => void;
|
|
9
10
|
};
|
|
10
11
|
};
|
package/tools/startCountdown.js
CHANGED
|
@@ -39,17 +39,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.createStartCountdown = createStartCountdown;
|
|
40
40
|
var workerTimers_1 = require("../tools/workerTimers");
|
|
41
41
|
function createStartCountdown(params) {
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
function startCountdown() {
|
|
42
|
+
var tickCallback = params.tickCallback;
|
|
43
|
+
function startCountdown(params) {
|
|
45
44
|
var _this = this;
|
|
45
|
+
var countDownFromSeconds = params.countDownFromSeconds;
|
|
46
46
|
var timer;
|
|
47
47
|
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
48
48
|
var secondsLeft;
|
|
49
49
|
return __generator(this, function (_a) {
|
|
50
50
|
switch (_a.label) {
|
|
51
51
|
case 0:
|
|
52
|
-
secondsLeft = Math.floor(
|
|
52
|
+
secondsLeft = Math.floor(countDownFromSeconds);
|
|
53
53
|
_a.label = 1;
|
|
54
54
|
case 1:
|
|
55
55
|
if (!(secondsLeft >= 0)) return [3 /*break*/, 3];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"startCountdown.js","sourceRoot":"","sources":["../src/tools/startCountdown.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,
|
|
1
|
+
{"version":3,"file":"startCountdown.js","sourceRoot":"","sources":["../src/tools/startCountdown.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,oDAiCC;AAnCD,sDAAiE;AAEjE,SAAgB,oBAAoB,CAAC,MAEpC;IACW,IAAA,YAAY,GAAK,MAAM,aAAX,CAAY;IAEhC,SAAS,cAAc,CAAC,MAAwC;QAAhE,iBAyBC;QAxBW,IAAA,oBAAoB,GAAK,MAAM,qBAAX,CAAY;QAExC,IAAI,KAAoC,CAAC;QAEzC,CAAC;;;;;wBACO,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;;;6BAE5C,CAAA,WAAW,IAAI,CAAC,CAAA;wBACnB,YAAY,CAAC,EAAE,WAAW,aAAA,EAAE,CAAC,CAAC;wBAE9B,qBAAM,IAAI,OAAO,CAAO,UAAA,OAAO;gCAC3B,KAAK,GAAG,IAAA,yBAAU,EAAC,OAAO,EAAE,IAAK,CAAC,CAAC;4BACvC,CAAC,CAAC,EAAA;;wBAFF,SAEE,CAAC;wBAEH,WAAW,EAAE,CAAC;;;;;aAErB,CAAC,EAAE,CAAC;QAEL,IAAM,aAAa,GAAG;YAClB,IAAA,2BAAY,EAAC,KAAK,CAAC,CAAC;YACpB,YAAY,CAAC,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;QAC7C,CAAC,CAAC;QAEF,OAAO,EAAE,aAAa,eAAA,EAAE,CAAC;IAC7B,CAAC;IAED,OAAO,EAAE,cAAc,gBAAA,EAAE,CAAC;AAC9B,CAAC"}
|