oidc-spa 6.15.1 → 7.0.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 +12 -13
- package/core/Oidc.d.ts +24 -12
- package/core/createOidc.d.ts +15 -30
- package/core/createOidc.js +131 -123
- 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 +1 -7
- package/react/react.js +8 -59
- package/react/react.js.map +1 -1
- package/src/core/Oidc.ts +27 -14
- package/src/core/createOidc.ts +121 -119
- 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 +11 -72
- package/src/tools/readExpirationTimeInJwt.ts +4 -5
- package/tools/readExpirationTimeInJwt.js +4 -4
- package/tools/readExpirationTimeInJwt.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/core/loginSilent.ts
CHANGED
|
@@ -27,7 +27,7 @@ export async function loginSilent(params: {
|
|
|
27
27
|
stateQueryParamValue_instance: string;
|
|
28
28
|
configId: string;
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
transformUrlBeforeRedirect:
|
|
31
31
|
| ((params: { authorizationUrl: string; isSilent: true }) => string)
|
|
32
32
|
| undefined;
|
|
33
33
|
|
|
@@ -42,7 +42,7 @@ export async function loginSilent(params: {
|
|
|
42
42
|
oidcClientTsUserManager,
|
|
43
43
|
stateQueryParamValue_instance,
|
|
44
44
|
configId,
|
|
45
|
-
|
|
45
|
+
transformUrlBeforeRedirect,
|
|
46
46
|
getExtraQueryParams,
|
|
47
47
|
getExtraTokenParams,
|
|
48
48
|
autoLogin
|
|
@@ -126,10 +126,10 @@ export async function loginSilent(params: {
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
apply_transform_url: {
|
|
129
|
-
if (
|
|
129
|
+
if (transformUrlBeforeRedirect === undefined) {
|
|
130
130
|
break apply_transform_url;
|
|
131
131
|
}
|
|
132
|
-
url =
|
|
132
|
+
url = transformUrlBeforeRedirect({ authorizationUrl: url, isSilent: true });
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
return url;
|
|
@@ -6,7 +6,9 @@ import type { Oidc } from "./Oidc";
|
|
|
6
6
|
|
|
7
7
|
export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, unknown>>(params: {
|
|
8
8
|
oidcClientTsUser: OidcClientTsUser;
|
|
9
|
-
decodedIdTokenSchema?: {
|
|
9
|
+
decodedIdTokenSchema?: {
|
|
10
|
+
parse: (decodedIdToken_original: Oidc.Tokens.DecodedIdToken_base) => DecodedIdToken;
|
|
11
|
+
};
|
|
10
12
|
__unsafe_useIdTokenAsAccessToken: boolean;
|
|
11
13
|
decodedIdToken_previous: DecodedIdToken | undefined;
|
|
12
14
|
log: typeof console.log | undefined;
|
|
@@ -23,69 +25,29 @@ export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, u
|
|
|
23
25
|
|
|
24
26
|
const accessToken = oidcClientTsUser.access_token;
|
|
25
27
|
|
|
26
|
-
const accessTokenExpirationTime = (() => {
|
|
27
|
-
read_from_token_response: {
|
|
28
|
-
const { expires_at } = oidcClientTsUser;
|
|
29
|
-
|
|
30
|
-
if (expires_at === undefined) {
|
|
31
|
-
break read_from_token_response;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return expires_at * 1000;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
read_from_jwt: {
|
|
38
|
-
const expirationTime = readExpirationTimeInJwt(accessToken);
|
|
39
|
-
|
|
40
|
-
if (expirationTime === undefined) {
|
|
41
|
-
break read_from_jwt;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return expirationTime;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
assert(false, "Failed to get access token expiration time");
|
|
48
|
-
})();
|
|
49
|
-
|
|
50
28
|
const refreshToken = oidcClientTsUser.refresh_token;
|
|
51
29
|
|
|
52
|
-
const refreshTokenExpirationTime = (() => {
|
|
53
|
-
if (refreshToken === undefined) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
read_from_jwt: {
|
|
58
|
-
const expirationTime = readExpirationTimeInJwt(refreshToken);
|
|
59
|
-
|
|
60
|
-
if (expirationTime === undefined) {
|
|
61
|
-
break read_from_jwt;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return expirationTime;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return undefined;
|
|
68
|
-
})();
|
|
69
|
-
|
|
70
30
|
const idToken = oidcClientTsUser.id_token;
|
|
71
31
|
|
|
72
32
|
assert(idToken !== undefined, "No id token provided by the oidc server");
|
|
73
33
|
|
|
34
|
+
const decodedIdToken_original = decodeJwt<Oidc.Tokens.DecodedIdToken_base>(idToken);
|
|
35
|
+
|
|
36
|
+
if (isFirstInit) {
|
|
37
|
+
log?.(
|
|
38
|
+
[
|
|
39
|
+
`Decoded ID token`,
|
|
40
|
+
decodedIdTokenSchema === undefined ? "" : " before `decodedIdTokenSchema.parse()`\n",
|
|
41
|
+
JSON.stringify(decodedIdToken_original, null, 2)
|
|
42
|
+
].join("")
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
74
46
|
const decodedIdToken = (() => {
|
|
75
|
-
let decodedIdToken
|
|
76
|
-
|
|
77
|
-
if (isFirstInit) {
|
|
78
|
-
log?.(
|
|
79
|
-
[
|
|
80
|
-
`Decoded ID token`,
|
|
81
|
-
decodedIdTokenSchema === undefined ? "" : " before `decodedIdTokenSchema.parse()`\n",
|
|
82
|
-
JSON.stringify(decodedIdToken, null, 2)
|
|
83
|
-
].join("")
|
|
84
|
-
);
|
|
85
|
-
}
|
|
47
|
+
let decodedIdToken: DecodedIdToken;
|
|
86
48
|
|
|
87
49
|
if (decodedIdTokenSchema !== undefined) {
|
|
88
|
-
decodedIdToken = decodedIdTokenSchema.parse(
|
|
50
|
+
decodedIdToken = decodedIdTokenSchema.parse(decodedIdToken_original);
|
|
89
51
|
|
|
90
52
|
if (isFirstInit) {
|
|
91
53
|
log?.(
|
|
@@ -95,18 +57,50 @@ export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, u
|
|
|
95
57
|
].join("")
|
|
96
58
|
);
|
|
97
59
|
}
|
|
60
|
+
} else {
|
|
61
|
+
// @ts-expect-error
|
|
62
|
+
decodedIdToken = decodedIdToken_original;
|
|
98
63
|
}
|
|
99
64
|
|
|
100
65
|
if (
|
|
101
66
|
decodedIdToken_previous !== undefined &&
|
|
102
67
|
JSON.stringify(decodedIdToken) === JSON.stringify(decodedIdToken_previous)
|
|
103
68
|
) {
|
|
69
|
+
// NOTE: For stable ref, prevent re-render for component that would memoize
|
|
104
70
|
return decodedIdToken_previous;
|
|
105
71
|
}
|
|
106
72
|
|
|
107
73
|
return decodedIdToken;
|
|
108
74
|
})();
|
|
109
75
|
|
|
76
|
+
const issuedAtTime = (() => {
|
|
77
|
+
// NOTE: The id_token is always a JWT as per the protocol.
|
|
78
|
+
// We don't use Date.now() due to network latency.
|
|
79
|
+
const id_token_iat = (() => {
|
|
80
|
+
let iat: number | undefined;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const iat_claimValue = decodedIdToken_original.iat;
|
|
84
|
+
assert(iat_claimValue === undefined || typeof iat_claimValue === "number");
|
|
85
|
+
iat = iat_claimValue;
|
|
86
|
+
} catch {
|
|
87
|
+
iat = undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (iat === undefined) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return iat;
|
|
95
|
+
})();
|
|
96
|
+
|
|
97
|
+
if (id_token_iat === undefined) {
|
|
98
|
+
return Date.now();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return id_token_iat * 1000;
|
|
102
|
+
})();
|
|
103
|
+
|
|
110
104
|
const tokens_common: Oidc.Tokens.Common<DecodedIdToken> = {
|
|
111
105
|
...(__unsafe_useIdTokenAsAccessToken
|
|
112
106
|
? {
|
|
@@ -122,9 +116,50 @@ export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, u
|
|
|
122
116
|
return expirationTime;
|
|
123
117
|
})()
|
|
124
118
|
}
|
|
125
|
-
: {
|
|
119
|
+
: {
|
|
120
|
+
accessToken,
|
|
121
|
+
accessTokenExpirationTime: (() => {
|
|
122
|
+
read_from_jwt: {
|
|
123
|
+
const expirationTime = readExpirationTimeInJwt(accessToken);
|
|
124
|
+
|
|
125
|
+
if (expirationTime === undefined) {
|
|
126
|
+
break read_from_jwt;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return expirationTime;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
read_from_token_response_expires_at: {
|
|
133
|
+
const { expires_at } = oidcClientTsUser.__oidc_spa_tokenResponse;
|
|
134
|
+
|
|
135
|
+
if (expires_at === undefined) {
|
|
136
|
+
break read_from_token_response_expires_at;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
assert(typeof expires_at === "number", "2033392");
|
|
140
|
+
|
|
141
|
+
return expires_at * 1000;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
read_from_token_response_expires_in: {
|
|
145
|
+
const { expires_in } = oidcClientTsUser.__oidc_spa_tokenResponse;
|
|
146
|
+
|
|
147
|
+
if (expires_in === undefined) {
|
|
148
|
+
break read_from_token_response_expires_in;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
assert(typeof expires_in === "number", "203333425");
|
|
152
|
+
|
|
153
|
+
return issuedAtTime + expires_in * 1_000;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
assert(false, "Failed to get access token expiration time");
|
|
157
|
+
})()
|
|
158
|
+
}),
|
|
126
159
|
idToken,
|
|
127
|
-
decodedIdToken
|
|
160
|
+
decodedIdToken,
|
|
161
|
+
decodedIdToken_original,
|
|
162
|
+
issuedAtTime
|
|
128
163
|
};
|
|
129
164
|
|
|
130
165
|
const tokens: Oidc.Tokens<DecodedIdToken> =
|
|
@@ -137,7 +172,43 @@ export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, u
|
|
|
137
172
|
...tokens_common,
|
|
138
173
|
hasRefreshToken: true,
|
|
139
174
|
refreshToken,
|
|
140
|
-
refreshTokenExpirationTime
|
|
175
|
+
refreshTokenExpirationTime: (() => {
|
|
176
|
+
read_from_token_response_expires_at: {
|
|
177
|
+
const { refresh_expires_at } = oidcClientTsUser.__oidc_spa_tokenResponse;
|
|
178
|
+
|
|
179
|
+
if (refresh_expires_at === undefined) {
|
|
180
|
+
break read_from_token_response_expires_at;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
assert(typeof refresh_expires_at === "number", "2033392");
|
|
184
|
+
|
|
185
|
+
return refresh_expires_at * 1000;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
read_from_token_response_expires_in: {
|
|
189
|
+
const { refresh_expires_in } = oidcClientTsUser.__oidc_spa_tokenResponse;
|
|
190
|
+
|
|
191
|
+
if (refresh_expires_in === undefined) {
|
|
192
|
+
break read_from_token_response_expires_in;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
assert(typeof refresh_expires_in === "number", "2033425330");
|
|
196
|
+
|
|
197
|
+
return issuedAtTime + refresh_expires_in * 1000;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
read_from_jwt: {
|
|
201
|
+
const expirationTime = readExpirationTimeInJwt(refreshToken);
|
|
202
|
+
|
|
203
|
+
if (expirationTime === undefined) {
|
|
204
|
+
break read_from_jwt;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return expirationTime;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return undefined;
|
|
211
|
+
})()
|
|
141
212
|
});
|
|
142
213
|
|
|
143
214
|
if (
|
|
@@ -156,27 +227,3 @@ export function oidcClientTsUserToTokens<DecodedIdToken extends Record<string, u
|
|
|
156
227
|
|
|
157
228
|
return tokens;
|
|
158
229
|
}
|
|
159
|
-
|
|
160
|
-
export function getMsBeforeExpiration(tokens: Oidc.Tokens): number {
|
|
161
|
-
// NOTE: In general the access token is supposed to have a shorter
|
|
162
|
-
// lifespan than the refresh token but we don't want to make any
|
|
163
|
-
// assumption here.
|
|
164
|
-
const tokenExpirationTime = Math.min(
|
|
165
|
-
tokens.accessTokenExpirationTime,
|
|
166
|
-
tokens.refreshTokenExpirationTime ?? Number.POSITIVE_INFINITY
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
const msBeforeExpiration = Math.min(
|
|
170
|
-
tokenExpirationTime - Date.now(),
|
|
171
|
-
// NOTE: We want to make sure we do not overflow the setTimeout
|
|
172
|
-
// that must be a 32 bit unsigned integer.
|
|
173
|
-
// This can happen if the tokenExpirationTime is more than 24.8 days in the future.
|
|
174
|
-
Math.pow(2, 31) - 1
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
if (msBeforeExpiration < 0) {
|
|
178
|
-
return 0;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return msBeforeExpiration;
|
|
182
|
-
}
|
package/src/mock/oidc.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type ParamsOfCreateMockOidc<
|
|
|
29
29
|
const URL_SEARCH_PARAM_NAME = "isUserLoggedIn";
|
|
30
30
|
|
|
31
31
|
export async function createMockOidc<
|
|
32
|
-
DecodedIdToken extends Record<string, unknown> =
|
|
32
|
+
DecodedIdToken extends Record<string, unknown> = Oidc.Tokens.DecodedIdToken_base,
|
|
33
33
|
AutoLogin extends boolean = false
|
|
34
34
|
>(
|
|
35
35
|
params: ParamsOfCreateMockOidc<DecodedIdToken, AutoLogin>
|
|
@@ -130,7 +130,7 @@ export async function createMockOidc<
|
|
|
130
130
|
...common,
|
|
131
131
|
isUserLoggedIn: true,
|
|
132
132
|
renewTokens: async () => {},
|
|
133
|
-
|
|
133
|
+
...(() => {
|
|
134
134
|
const tokens_common: Oidc.Tokens.Common<DecodedIdToken> = {
|
|
135
135
|
accessToken: mockedTokens.accessToken ?? "mocked-access-token",
|
|
136
136
|
accessTokenExpirationTime: mockedTokens.accessTokenExpirationTime ?? Infinity,
|
|
@@ -142,7 +142,16 @@ export async function createMockOidc<
|
|
|
142
142
|
"You haven't provided a mocked decodedIdToken",
|
|
143
143
|
"See https://docs.oidc-spa.dev/v/v6/mock"
|
|
144
144
|
].join("\n")
|
|
145
|
-
})
|
|
145
|
+
}),
|
|
146
|
+
decodedIdToken_original:
|
|
147
|
+
mockedTokens.decodedIdToken_original ??
|
|
148
|
+
createObjectThatThrowsIfAccessed<Oidc.Tokens.DecodedIdToken_base>({
|
|
149
|
+
debugMessage: [
|
|
150
|
+
"You haven't provided a mocked decodedIdToken_original",
|
|
151
|
+
"See https://docs.oidc-spa.dev/v/v6/mock"
|
|
152
|
+
].join("\n")
|
|
153
|
+
}),
|
|
154
|
+
issuedAtTime: Date.now()
|
|
146
155
|
};
|
|
147
156
|
|
|
148
157
|
const tokens: Oidc.Tokens<DecodedIdToken> =
|
|
@@ -158,10 +167,11 @@ export async function createMockOidc<
|
|
|
158
167
|
hasRefreshToken: false
|
|
159
168
|
});
|
|
160
169
|
|
|
161
|
-
return
|
|
170
|
+
return {
|
|
171
|
+
getTokens: () => Promise.resolve(tokens),
|
|
172
|
+
getDecodedIdToken: () => tokens_common.decodedIdToken
|
|
173
|
+
};
|
|
162
174
|
})(),
|
|
163
|
-
getTokens_next: () => Promise.resolve(oidc.getTokens()),
|
|
164
|
-
getDecodedIdToken: () => oidc.getTokens().decodedIdToken,
|
|
165
175
|
subscribeToTokensChange: () => ({
|
|
166
176
|
unsubscribe: () => {}
|
|
167
177
|
}),
|
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,
|
|
@@ -33,10 +31,7 @@ export namespace OidcReact {
|
|
|
33
31
|
}) => Promise<never>;
|
|
34
32
|
initializationError: OidcInitializationError | undefined;
|
|
35
33
|
|
|
36
|
-
/** @deprecated: Use `const { decodedIdToken, tokens} = useOidc();` */
|
|
37
|
-
oidcTokens?: never;
|
|
38
34
|
decodedIdToken?: never;
|
|
39
|
-
tokens?: never;
|
|
40
35
|
logout?: never;
|
|
41
36
|
subscribeToAutoLogoutCountdown?: never;
|
|
42
37
|
goToAuthServer?: never;
|
|
@@ -46,10 +41,7 @@ export namespace OidcReact {
|
|
|
46
41
|
|
|
47
42
|
export type LoggedIn<DecodedIdToken extends Record<string, unknown>> = Common & {
|
|
48
43
|
isUserLoggedIn: true;
|
|
49
|
-
/** @deprecated: Use `const { decodedIdToken, tokens} = useOidc();` */
|
|
50
|
-
oidcTokens: Oidc.Tokens<DecodedIdToken>;
|
|
51
44
|
decodedIdToken: DecodedIdToken;
|
|
52
|
-
tokens: Oidc.Tokens<DecodedIdToken> | undefined;
|
|
53
45
|
logout: Oidc.LoggedIn["logout"];
|
|
54
46
|
renewTokens: Oidc.LoggedIn["renewTokens"];
|
|
55
47
|
subscribeToAutoLogoutCountdown: (
|
|
@@ -259,62 +251,22 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
259
251
|
}
|
|
260
252
|
}
|
|
261
253
|
|
|
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
|
-
});
|
|
254
|
+
const [, reRenderIfDecodedIdTokenChanged] = useState(
|
|
255
|
+
!oidc.isUserLoggedIn ? undefined : oidc.getDecodedIdToken()
|
|
256
|
+
);
|
|
281
257
|
|
|
282
258
|
useEffect(() => {
|
|
283
259
|
if (!oidc.isUserLoggedIn) {
|
|
284
260
|
return;
|
|
285
261
|
}
|
|
286
262
|
|
|
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
|
-
};
|
|
263
|
+
const { unsubscribe } = oidc.subscribeToTokensChange(() =>
|
|
264
|
+
reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken())
|
|
265
|
+
);
|
|
300
266
|
|
|
301
|
-
|
|
267
|
+
reRenderIfDecodedIdTokenChanged(oidc.getDecodedIdToken());
|
|
302
268
|
|
|
303
|
-
|
|
304
|
-
if (!isActive) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
updateTokens(tokens);
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
const { unsubscribe } = oidc.subscribeToTokensChange(tokens => {
|
|
311
|
-
updateTokens(tokens);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
return () => {
|
|
315
|
-
isActive = false;
|
|
316
|
-
unsubscribe();
|
|
317
|
-
};
|
|
269
|
+
return unsubscribe;
|
|
318
270
|
}, []);
|
|
319
271
|
|
|
320
272
|
const common: OidcReact.Common = {
|
|
@@ -334,13 +286,7 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
334
286
|
const oidcReact: OidcReact.LoggedIn<DecodedIdToken> = {
|
|
335
287
|
...common,
|
|
336
288
|
isUserLoggedIn: true,
|
|
337
|
-
oidcTokens: oidc.getTokens(),
|
|
338
289
|
decodedIdToken: oidc.getDecodedIdToken(),
|
|
339
|
-
get tokens() {
|
|
340
|
-
const tokensState = tokensState_ref.current;
|
|
341
|
-
tokensState.isConsumerReadingTokens = true;
|
|
342
|
-
return tokensState.tokens;
|
|
343
|
-
},
|
|
344
290
|
logout: oidc.logout,
|
|
345
291
|
renewTokens: oidc.renewTokens,
|
|
346
292
|
subscribeToAutoLogoutCountdown: oidc.subscribeToAutoLogoutCountdown,
|
|
@@ -422,17 +368,10 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
422
368
|
return oidc;
|
|
423
369
|
});
|
|
424
370
|
|
|
425
|
-
|
|
371
|
+
function getOidc(): Promise<Oidc<DecodedIdToken>> {
|
|
426
372
|
dReadyToCreate.resolve();
|
|
427
373
|
|
|
428
|
-
|
|
429
|
-
const oidc = await prOidc;
|
|
430
|
-
|
|
431
|
-
if (oidc.isUserLoggedIn) {
|
|
432
|
-
await oidc.getTokens_next();
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return oidc;
|
|
374
|
+
return prOidc;
|
|
436
375
|
}
|
|
437
376
|
|
|
438
377
|
const oidcReact: OidcReactApi<DecodedIdToken, false> = {
|
|
@@ -449,7 +388,7 @@ export function createOidcReactApi_dependencyInjection<
|
|
|
449
388
|
|
|
450
389
|
/** @see: https://docs.oidc-spa.dev/v/v6/usage#react-api */
|
|
451
390
|
export function createReactOidc<
|
|
452
|
-
DecodedIdToken extends Record<string, unknown> =
|
|
391
|
+
DecodedIdToken extends Record<string, unknown> = Oidc.Tokens.DecodedIdToken_base,
|
|
453
392
|
AutoLogin extends boolean = false
|
|
454
393
|
>(params: ValueOrAsyncGetter<ParamsOfCreateOidc<DecodedIdToken, AutoLogin>>) {
|
|
455
394
|
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
|
}
|
|
@@ -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"}
|