@axa-fr/oidc-client 7.27.19 → 7.29.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 +122 -4
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.spec.d.ts +2 -0
- package/dist/crypto.spec.d.ts.map +1 -0
- package/dist/events.d.ts +3 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/fetch.d.ts +2 -1
- package/dist/fetch.d.ts.map +1 -1
- package/dist/fetch.spec.d.ts +2 -0
- package/dist/fetch.spec.d.ts.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +922 -556
- package/dist/index.umd.cjs +2 -2
- package/dist/jwt.spec.d.ts +2 -0
- package/dist/jwt.spec.d.ts.map +1 -0
- package/dist/login.d.ts +3 -3
- package/dist/login.d.ts.map +1 -1
- package/dist/logout.d.ts.map +1 -1
- package/dist/oidc.d.ts +12 -2
- package/dist/oidc.d.ts.map +1 -1
- package/dist/oidcClient.d.ts +5 -1
- package/dist/oidcClient.d.ts.map +1 -1
- package/dist/oidcError.d.ts +57 -0
- package/dist/oidcError.d.ts.map +1 -0
- package/dist/oidcError.spec.d.ts +2 -0
- package/dist/oidcError.spec.d.ts.map +1 -0
- package/dist/oidcStateError.d.ts +7 -4
- package/dist/oidcStateError.d.ts.map +1 -1
- package/dist/par.spec.d.ts +2 -0
- package/dist/par.spec.d.ts.map +1 -0
- package/dist/parseTokens.d.ts +1 -1
- package/dist/parseTokens.d.ts.map +1 -1
- package/dist/pushedAuthorizationRequestError.d.ts +20 -0
- package/dist/pushedAuthorizationRequestError.d.ts.map +1 -0
- package/dist/pushedAuthorizationRequestError.spec.d.ts +2 -0
- package/dist/pushedAuthorizationRequestError.spec.d.ts.map +1 -0
- package/dist/renewTokens.d.ts +11 -0
- package/dist/renewTokens.d.ts.map +1 -1
- package/dist/renewTokensError.spec.d.ts +2 -0
- package/dist/renewTokensError.spec.d.ts.map +1 -0
- package/dist/requests.d.ts +21 -14
- package/dist/requests.d.ts.map +1 -1
- package/dist/silentLogin.d.ts +6 -4
- package/dist/silentLogin.d.ts.map +1 -1
- package/dist/silentLogin.spec.d.ts +2 -0
- package/dist/silentLogin.spec.d.ts.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/user.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/package.json +2 -3
- package/src/crypto.spec.ts +40 -0
- package/src/crypto.ts +9 -10
- package/src/events.ts +3 -0
- package/src/fetch.spec.ts +105 -0
- package/src/fetch.ts +39 -3
- package/src/index.ts +14 -1
- package/src/jwt.spec.ts +33 -0
- package/src/jwt.ts +1 -47
- package/src/login.spec.ts +34 -0
- package/src/login.ts +119 -14
- package/src/logout.spec.ts +58 -0
- package/src/logout.ts +46 -7
- package/src/oidc.ts +78 -16
- package/src/oidcClient.ts +8 -1
- package/src/oidcError.spec.ts +105 -0
- package/src/oidcError.ts +197 -0
- package/src/oidcStateError.spec.ts +18 -0
- package/src/oidcStateError.ts +29 -8
- package/src/par.spec.ts +232 -0
- package/src/parseTokens.spec.ts +2 -1
- package/src/parseTokens.ts +1 -1
- package/src/pushedAuthorizationRequestError.spec.ts +43 -0
- package/src/pushedAuthorizationRequestError.ts +50 -0
- package/src/renewTokens.ts +167 -29
- package/src/renewTokensError.spec.ts +116 -0
- package/src/requests.spec.ts +293 -4
- package/src/requests.ts +174 -8
- package/src/silentLogin.spec.ts +158 -0
- package/src/silentLogin.ts +36 -9
- package/src/types.ts +6 -0
- package/src/user.ts +71 -31
- package/src/version.ts +1 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { OidcError, OidcErrorCode, serializeOidcError } from './oidcError';
|
|
4
|
+
import { isOidcStateError, OidcStateError, OidcStateErrorCode } from './oidcStateError';
|
|
5
|
+
import { _silentLoginAsync } from './silentLogin';
|
|
6
|
+
|
|
7
|
+
const installDom = () => {
|
|
8
|
+
const contentWindow = {};
|
|
9
|
+
const iframe = {
|
|
10
|
+
contentWindow,
|
|
11
|
+
height: '',
|
|
12
|
+
id: '',
|
|
13
|
+
remove: vi.fn(),
|
|
14
|
+
setAttribute: vi.fn(),
|
|
15
|
+
style: { display: '' },
|
|
16
|
+
width: '',
|
|
17
|
+
};
|
|
18
|
+
let listener: (event: MessageEvent) => void;
|
|
19
|
+
vi.stubGlobal('document', {
|
|
20
|
+
body: { appendChild: vi.fn() },
|
|
21
|
+
createElement: vi.fn(() => iframe),
|
|
22
|
+
});
|
|
23
|
+
vi.stubGlobal('window', {
|
|
24
|
+
addEventListener: vi.fn((_name: string, callback: (event: MessageEvent) => void) => {
|
|
25
|
+
listener = callback;
|
|
26
|
+
}),
|
|
27
|
+
removeEventListener: vi.fn(),
|
|
28
|
+
});
|
|
29
|
+
return { contentWindow, iframe, getListener: () => listener };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
vi.useRealTimers();
|
|
34
|
+
vi.unstubAllGlobals();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('_silentLoginAsync typed errors', () => {
|
|
38
|
+
it('rejects with SILENT_LOGIN_TIMEOUT and publishes the same error', async () => {
|
|
39
|
+
vi.useFakeTimers();
|
|
40
|
+
installDom();
|
|
41
|
+
const publishEvent = vi.fn();
|
|
42
|
+
|
|
43
|
+
const promise = _silentLoginAsync(
|
|
44
|
+
'default',
|
|
45
|
+
{
|
|
46
|
+
authority: 'https://issuer.example.com',
|
|
47
|
+
client_id: 'client',
|
|
48
|
+
redirect_uri: 'https://client.example.com/callback',
|
|
49
|
+
scope: 'openid',
|
|
50
|
+
silent_login_uri: 'https://issuer.example.com/silent-login',
|
|
51
|
+
silent_redirect_uri: 'https://client.example.com/silent-callback',
|
|
52
|
+
silent_login_timeout: 25,
|
|
53
|
+
},
|
|
54
|
+
publishEvent,
|
|
55
|
+
'refresh',
|
|
56
|
+
)();
|
|
57
|
+
const rejection = expect(promise).rejects.toMatchObject({
|
|
58
|
+
code: OidcErrorCode.SILENT_LOGIN_TIMEOUT,
|
|
59
|
+
message: 'timeout',
|
|
60
|
+
phase: 'refresh',
|
|
61
|
+
retryable: true,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
await vi.advanceTimersByTimeAsync(25);
|
|
65
|
+
await rejection;
|
|
66
|
+
const publishedError = publishEvent.mock.calls.find(
|
|
67
|
+
([name]) => name === 'silentLoginAsync_error',
|
|
68
|
+
)?.[1];
|
|
69
|
+
expect(publishedError).toBeInstanceOf(OidcError);
|
|
70
|
+
expect(publishedError.code).toBe(OidcErrorCode.SILENT_LOGIN_TIMEOUT);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('reconstructs a serialized OidcError received from the callback iframe', async () => {
|
|
74
|
+
const { contentWindow, getListener } = installDom();
|
|
75
|
+
const publishEvent = vi.fn();
|
|
76
|
+
const original = new OidcError(OidcErrorCode.LOGIN_REQUIRED, 'login required', {
|
|
77
|
+
phase: 'refresh',
|
|
78
|
+
retryable: false,
|
|
79
|
+
oauthError: 'login_required',
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const promise = _silentLoginAsync(
|
|
83
|
+
'default',
|
|
84
|
+
{
|
|
85
|
+
authority: 'https://issuer.example.com',
|
|
86
|
+
client_id: 'client',
|
|
87
|
+
redirect_uri: 'https://client.example.com/callback',
|
|
88
|
+
scope: 'openid',
|
|
89
|
+
silent_login_uri: 'https://issuer.example.com/silent-login',
|
|
90
|
+
silent_redirect_uri: 'https://client.example.com/silent-callback',
|
|
91
|
+
silent_login_timeout: 1000,
|
|
92
|
+
},
|
|
93
|
+
publishEvent,
|
|
94
|
+
'refresh',
|
|
95
|
+
)();
|
|
96
|
+
getListener()({
|
|
97
|
+
origin: 'https://issuer.example.com',
|
|
98
|
+
source: contentWindow,
|
|
99
|
+
data: `default_oidc_exception:${JSON.stringify({
|
|
100
|
+
error: original.toString(),
|
|
101
|
+
oidcError: serializeOidcError(original),
|
|
102
|
+
})}`,
|
|
103
|
+
} as unknown as MessageEvent);
|
|
104
|
+
|
|
105
|
+
await expect(promise).rejects.toMatchObject({
|
|
106
|
+
code: OidcErrorCode.LOGIN_REQUIRED,
|
|
107
|
+
phase: 'refresh',
|
|
108
|
+
oauthError: 'login_required',
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('keeps serialized OidcStateError instances detectable after the iframe round-trip', async () => {
|
|
113
|
+
const { contentWindow, getListener } = installDom();
|
|
114
|
+
const publishEvent = vi.fn();
|
|
115
|
+
const original = new OidcStateError(
|
|
116
|
+
OidcStateErrorCode.NONCE_MISSING,
|
|
117
|
+
'refresh token: nonce missing from storage',
|
|
118
|
+
'refresh',
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const promise = _silentLoginAsync(
|
|
122
|
+
'default',
|
|
123
|
+
{
|
|
124
|
+
authority: 'https://issuer.example.com',
|
|
125
|
+
client_id: 'client',
|
|
126
|
+
redirect_uri: 'https://client.example.com/callback',
|
|
127
|
+
scope: 'openid',
|
|
128
|
+
silent_login_uri: 'https://issuer.example.com/silent-login',
|
|
129
|
+
silent_redirect_uri: 'https://client.example.com/silent-callback',
|
|
130
|
+
silent_login_timeout: 1000,
|
|
131
|
+
},
|
|
132
|
+
publishEvent,
|
|
133
|
+
'refresh',
|
|
134
|
+
)();
|
|
135
|
+
getListener()({
|
|
136
|
+
origin: 'https://issuer.example.com',
|
|
137
|
+
source: contentWindow,
|
|
138
|
+
data: `default_oidc_exception:${JSON.stringify({
|
|
139
|
+
error: original.toString(),
|
|
140
|
+
oidcError: serializeOidcError(original),
|
|
141
|
+
})}`,
|
|
142
|
+
} as unknown as MessageEvent);
|
|
143
|
+
|
|
144
|
+
let thrownError: unknown;
|
|
145
|
+
try {
|
|
146
|
+
await promise;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
thrownError = error;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
expect(isOidcStateError(thrownError)).toBe(true);
|
|
152
|
+
expect(thrownError).toMatchObject({
|
|
153
|
+
code: OidcStateErrorCode.NONCE_MISSING,
|
|
154
|
+
phase: 'refresh',
|
|
155
|
+
name: 'OidcStateError',
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
package/src/silentLogin.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
import { eventNames } from './events.js';
|
|
2
|
+
import {
|
|
3
|
+
createOAuthError,
|
|
4
|
+
deserializeOidcError,
|
|
5
|
+
OidcError,
|
|
6
|
+
OidcErrorCode,
|
|
7
|
+
OidcErrorPhase,
|
|
8
|
+
} from './oidcError.js';
|
|
2
9
|
import { Tokens } from './parseTokens.js';
|
|
3
10
|
import { autoRenewTokens } from './renewTokens.js';
|
|
4
11
|
import timer from './timer.js';
|
|
5
12
|
import { OidcConfiguration, StringMap } from './types.js';
|
|
6
13
|
export type SilentLoginResponse = {
|
|
7
|
-
tokens: Tokens;
|
|
8
|
-
sessionState: string;
|
|
9
|
-
error
|
|
14
|
+
tokens: Tokens | null;
|
|
15
|
+
sessionState: string | null;
|
|
16
|
+
error?: string;
|
|
17
|
+
oidcError?: OidcError;
|
|
10
18
|
};
|
|
11
19
|
|
|
12
20
|
export type PublishEventFunction = (eventName: string, eventData: any) => void;
|
|
@@ -16,6 +24,7 @@ export const _silentLoginAsync =
|
|
|
16
24
|
configurationName: string,
|
|
17
25
|
configuration: OidcConfiguration,
|
|
18
26
|
publishEvent: PublishEventFunction,
|
|
27
|
+
phase: OidcErrorPhase = 'login',
|
|
19
28
|
) =>
|
|
20
29
|
(
|
|
21
30
|
extras: StringMap = null,
|
|
@@ -91,13 +100,27 @@ export const _silentLoginAsync =
|
|
|
91
100
|
clear();
|
|
92
101
|
} else if (data.startsWith(key_error)) {
|
|
93
102
|
const result = JSON.parse(e.data.replace(key_error, ''));
|
|
94
|
-
|
|
95
|
-
|
|
103
|
+
const error =
|
|
104
|
+
deserializeOidcError(result.oidcError) ??
|
|
105
|
+
createOAuthError(
|
|
106
|
+
result.error,
|
|
107
|
+
result.error_description,
|
|
108
|
+
`Error from OIDC server: ${result.error} - ${result.error_description}`,
|
|
109
|
+
phase,
|
|
110
|
+
);
|
|
111
|
+
publishEvent(eventNames.silentLoginAsync_error, error);
|
|
112
|
+
resolve({
|
|
113
|
+
error: 'oidc_' + result.error,
|
|
114
|
+
tokens: null,
|
|
115
|
+
sessionState: null,
|
|
116
|
+
oidcError: error,
|
|
117
|
+
});
|
|
96
118
|
clear();
|
|
97
119
|
} else if (data.startsWith(key_exception)) {
|
|
98
120
|
const result = JSON.parse(e.data.replace(key_exception, ''));
|
|
99
|
-
|
|
100
|
-
|
|
121
|
+
const error = deserializeOidcError(result.oidcError) ?? new Error(result.error);
|
|
122
|
+
publishEvent(eventNames.silentLoginAsync_error, error);
|
|
123
|
+
reject(error);
|
|
101
124
|
clear();
|
|
102
125
|
}
|
|
103
126
|
}
|
|
@@ -112,8 +135,12 @@ export const _silentLoginAsync =
|
|
|
112
135
|
setTimeout(() => {
|
|
113
136
|
if (!isResolved) {
|
|
114
137
|
clear();
|
|
115
|
-
|
|
116
|
-
|
|
138
|
+
const error = new OidcError(OidcErrorCode.SILENT_LOGIN_TIMEOUT, 'timeout', {
|
|
139
|
+
phase,
|
|
140
|
+
retryable: true,
|
|
141
|
+
});
|
|
142
|
+
publishEvent(eventNames.silentLoginAsync_error, error);
|
|
143
|
+
reject(error);
|
|
117
144
|
}
|
|
118
145
|
}, silentSigninTimeout);
|
|
119
146
|
} catch (e) {
|
package/src/types.ts
CHANGED
|
@@ -2,6 +2,8 @@ export type Fetch = typeof window.fetch;
|
|
|
2
2
|
|
|
3
3
|
export type LogoutToken = 'access_token' | 'refresh_token';
|
|
4
4
|
|
|
5
|
+
export type PushedAuthorizationRequestMode = 'disabled' | 'auto' | 'required';
|
|
6
|
+
|
|
5
7
|
export type ServiceWorkerUpdateRequireCallback = (
|
|
6
8
|
registration: any,
|
|
7
9
|
stopKeepAlive: () => void,
|
|
@@ -30,6 +32,8 @@ export type OidcConfiguration = {
|
|
|
30
32
|
refresh_time_before_tokens_expiration_in_second?: number;
|
|
31
33
|
token_automatic_renew_mode?: TokenAutomaticRenewMode;
|
|
32
34
|
token_request_timeout?: number;
|
|
35
|
+
par?: PushedAuthorizationRequestMode;
|
|
36
|
+
par_request_timeout?: number;
|
|
33
37
|
service_worker_relative_url?: string;
|
|
34
38
|
service_worker_register?: ServiceWorkerRegister;
|
|
35
39
|
service_worker_keep_alive_path?: string;
|
|
@@ -70,6 +74,8 @@ export interface AuthorityConfiguration {
|
|
|
70
74
|
authorization_endpoint: string;
|
|
71
75
|
token_endpoint: string;
|
|
72
76
|
revocation_endpoint: string;
|
|
77
|
+
pushed_authorization_request_endpoint?: string;
|
|
78
|
+
require_pushed_authorization_requests?: boolean;
|
|
73
79
|
end_session_endpoint?: string;
|
|
74
80
|
userinfo_endpoint?: string;
|
|
75
81
|
check_session_iframe?: string;
|
package/src/user.ts
CHANGED
|
@@ -1,41 +1,81 @@
|
|
|
1
|
+
import { eventNames } from './events';
|
|
1
2
|
import { fetchWithTokens } from './fetch';
|
|
2
3
|
import Oidc from './oidc';
|
|
4
|
+
import {
|
|
5
|
+
createNetworkError,
|
|
6
|
+
isNetworkErrorCause,
|
|
7
|
+
isOidcError,
|
|
8
|
+
isRetryableHttpStatus,
|
|
9
|
+
OidcError,
|
|
10
|
+
OidcErrorCode,
|
|
11
|
+
} from './oidcError';
|
|
3
12
|
|
|
4
13
|
export const userInfoAsync =
|
|
5
14
|
(oidc: Oidc) =>
|
|
6
15
|
async (noCache = false, demonstrating_proof_of_possession = false) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// Check storage cache
|
|
11
|
-
const stored =
|
|
12
|
-
!noCache && oidc.configuration.storage?.getItem(`oidc.${oidc.configurationName}.userInfo`);
|
|
13
|
-
if (stored) {
|
|
14
|
-
oidc.userInfo = JSON.parse(stored);
|
|
15
|
-
return oidc.userInfo;
|
|
16
|
-
}
|
|
17
|
-
const configuration = oidc.configuration;
|
|
18
|
-
const oidcServerConfiguration = await oidc.initAsync(
|
|
19
|
-
configuration.authority,
|
|
20
|
-
configuration.authority_configuration,
|
|
21
|
-
);
|
|
22
|
-
const url = oidcServerConfiguration.userInfoEndpoint;
|
|
23
|
-
const fetchUserInfo = async () => {
|
|
24
|
-
const oidcFetch = fetchWithTokens(fetch, oidc, demonstrating_proof_of_possession);
|
|
25
|
-
const response = await oidcFetch(url);
|
|
26
|
-
if (response.status !== 200) {
|
|
27
|
-
return null;
|
|
16
|
+
try {
|
|
17
|
+
if (oidc.userInfo != null && !noCache) {
|
|
18
|
+
return oidc.userInfo;
|
|
28
19
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
// Check storage cache
|
|
21
|
+
const stored =
|
|
22
|
+
!noCache && oidc.configuration.storage?.getItem(`oidc.${oidc.configurationName}.userInfo`);
|
|
23
|
+
if (stored) {
|
|
24
|
+
oidc.userInfo = JSON.parse(stored);
|
|
25
|
+
return oidc.userInfo;
|
|
26
|
+
}
|
|
27
|
+
const configuration = oidc.configuration;
|
|
28
|
+
const oidcServerConfiguration = await oidc.initAsync(
|
|
29
|
+
configuration.authority,
|
|
30
|
+
configuration.authority_configuration,
|
|
38
31
|
);
|
|
32
|
+
const url = oidcServerConfiguration.userInfoEndpoint;
|
|
33
|
+
const fetchUserInfo = async () => {
|
|
34
|
+
const oidcFetch = fetchWithTokens(
|
|
35
|
+
fetch,
|
|
36
|
+
oidc,
|
|
37
|
+
demonstrating_proof_of_possession,
|
|
38
|
+
'userinfo',
|
|
39
|
+
);
|
|
40
|
+
const response = await oidcFetch(url);
|
|
41
|
+
if (response.status !== 200) {
|
|
42
|
+
const isDpopNonceRequired =
|
|
43
|
+
response.headers?.has('DPoP-Nonce') &&
|
|
44
|
+
(response.status === 400 ||
|
|
45
|
+
response.status === 401 ||
|
|
46
|
+
response.headers.get('WWW-Authenticate')?.includes('use_dpop_nonce'));
|
|
47
|
+
const error = new OidcError(
|
|
48
|
+
isDpopNonceRequired ? OidcErrorCode.DPOP_NONCE_REQUIRED : OidcErrorCode.REQUEST_FAILED,
|
|
49
|
+
'UserInfo request failed',
|
|
50
|
+
{
|
|
51
|
+
phase: 'userinfo',
|
|
52
|
+
retryable: isDpopNonceRequired || isRetryableHttpStatus(response.status),
|
|
53
|
+
status: response.status,
|
|
54
|
+
oauthError: isDpopNonceRequired ? 'use_dpop_nonce' : undefined,
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
oidc.publishEvent(eventNames.userInfoAsync_error, error);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return response.json();
|
|
61
|
+
};
|
|
62
|
+
const userInfo = await fetchUserInfo();
|
|
63
|
+
oidc.userInfo = userInfo;
|
|
64
|
+
// Store in cache
|
|
65
|
+
if (userInfo) {
|
|
66
|
+
oidc.configuration.storage?.setItem(
|
|
67
|
+
`oidc.${oidc.configurationName}.userInfo`,
|
|
68
|
+
JSON.stringify(userInfo),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return userInfo;
|
|
72
|
+
} catch (cause) {
|
|
73
|
+
const error = isOidcError(cause)
|
|
74
|
+
? cause
|
|
75
|
+
: isNetworkErrorCause(cause)
|
|
76
|
+
? createNetworkError(cause, 'userinfo')
|
|
77
|
+
: cause;
|
|
78
|
+
oidc.publishEvent(eventNames.userInfoAsync_error, error);
|
|
79
|
+
throw error;
|
|
39
80
|
}
|
|
40
|
-
return userInfo;
|
|
41
81
|
};
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '7.
|
|
1
|
+
export default '7.29.1';
|