@axa-fr/oidc-client 7.27.18 → 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.
Files changed (91) hide show
  1. package/README.md +122 -4
  2. package/dist/crypto.d.ts.map +1 -1
  3. package/dist/crypto.spec.d.ts +2 -0
  4. package/dist/crypto.spec.d.ts.map +1 -0
  5. package/dist/events.d.ts +3 -0
  6. package/dist/events.d.ts.map +1 -1
  7. package/dist/fetch.d.ts +2 -1
  8. package/dist/fetch.d.ts.map +1 -1
  9. package/dist/fetch.spec.d.ts +2 -0
  10. package/dist/fetch.spec.d.ts.map +1 -0
  11. package/dist/index.d.ts +4 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +918 -545
  14. package/dist/index.umd.cjs +2 -2
  15. package/dist/initWorker.d.ts +1 -0
  16. package/dist/initWorker.d.ts.map +1 -1
  17. package/dist/initWorkerAbortError.spec.d.ts +2 -0
  18. package/dist/initWorkerAbortError.spec.d.ts.map +1 -0
  19. package/dist/jwt.spec.d.ts +2 -0
  20. package/dist/jwt.spec.d.ts.map +1 -0
  21. package/dist/login.d.ts +3 -3
  22. package/dist/login.d.ts.map +1 -1
  23. package/dist/logout.d.ts.map +1 -1
  24. package/dist/oidc.d.ts +12 -2
  25. package/dist/oidc.d.ts.map +1 -1
  26. package/dist/oidcClient.d.ts +5 -1
  27. package/dist/oidcClient.d.ts.map +1 -1
  28. package/dist/oidcError.d.ts +57 -0
  29. package/dist/oidcError.d.ts.map +1 -0
  30. package/dist/oidcError.spec.d.ts +2 -0
  31. package/dist/oidcError.spec.d.ts.map +1 -0
  32. package/dist/oidcStateError.d.ts +7 -4
  33. package/dist/oidcStateError.d.ts.map +1 -1
  34. package/dist/par.spec.d.ts +2 -0
  35. package/dist/par.spec.d.ts.map +1 -0
  36. package/dist/parseTokens.d.ts +1 -1
  37. package/dist/parseTokens.d.ts.map +1 -1
  38. package/dist/pushedAuthorizationRequestError.d.ts +20 -0
  39. package/dist/pushedAuthorizationRequestError.d.ts.map +1 -0
  40. package/dist/pushedAuthorizationRequestError.spec.d.ts +2 -0
  41. package/dist/pushedAuthorizationRequestError.spec.d.ts.map +1 -0
  42. package/dist/renewTokens.d.ts +11 -0
  43. package/dist/renewTokens.d.ts.map +1 -1
  44. package/dist/renewTokensError.spec.d.ts +2 -0
  45. package/dist/renewTokensError.spec.d.ts.map +1 -0
  46. package/dist/requests.d.ts +21 -14
  47. package/dist/requests.d.ts.map +1 -1
  48. package/dist/silentLogin.d.ts +6 -4
  49. package/dist/silentLogin.d.ts.map +1 -1
  50. package/dist/silentLogin.spec.d.ts +2 -0
  51. package/dist/silentLogin.spec.d.ts.map +1 -0
  52. package/dist/types.d.ts +5 -0
  53. package/dist/types.d.ts.map +1 -1
  54. package/dist/user.d.ts.map +1 -1
  55. package/dist/version.d.ts +1 -1
  56. package/dist/version.d.ts.map +1 -1
  57. package/package.json +2 -3
  58. package/src/crypto.spec.ts +40 -0
  59. package/src/crypto.ts +9 -10
  60. package/src/events.ts +3 -0
  61. package/src/fetch.spec.ts +105 -0
  62. package/src/fetch.ts +39 -3
  63. package/src/index.ts +14 -1
  64. package/src/initWorker.ts +37 -17
  65. package/src/initWorkerAbortError.spec.ts +147 -0
  66. package/src/jwt.spec.ts +33 -0
  67. package/src/jwt.ts +1 -47
  68. package/src/login.spec.ts +34 -0
  69. package/src/login.ts +119 -14
  70. package/src/logout.spec.ts +58 -0
  71. package/src/logout.ts +46 -7
  72. package/src/oidc.ts +78 -16
  73. package/src/oidcClient.ts +8 -1
  74. package/src/oidcError.spec.ts +105 -0
  75. package/src/oidcError.ts +197 -0
  76. package/src/oidcStateError.spec.ts +18 -0
  77. package/src/oidcStateError.ts +29 -8
  78. package/src/par.spec.ts +232 -0
  79. package/src/parseTokens.spec.ts +2 -1
  80. package/src/parseTokens.ts +1 -1
  81. package/src/pushedAuthorizationRequestError.spec.ts +43 -0
  82. package/src/pushedAuthorizationRequestError.ts +50 -0
  83. package/src/renewTokens.ts +167 -29
  84. package/src/renewTokensError.spec.ts +116 -0
  85. package/src/requests.spec.ts +293 -4
  86. package/src/requests.ts +174 -8
  87. package/src/silentLogin.spec.ts +158 -0
  88. package/src/silentLogin.ts +36 -9
  89. package/src/types.ts +6 -0
  90. package/src/user.ts +71 -31
  91. package/src/version.ts +1 -1
@@ -0,0 +1,232 @@
1
+ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { ILOidcLocation } from './location';
4
+ import { defaultLoginAsync } from './login';
5
+ import { OidcAuthorizationServiceConfiguration } from './oidc';
6
+ import { PushedAuthorizationRequestErrorCode } from './pushedAuthorizationRequestError';
7
+ import { Fetch, OidcConfiguration } from './types';
8
+
9
+ const makeStorage = (): Storage => {
10
+ const store: Record<string, string> = {};
11
+ return {
12
+ getItem: (key: string) => store[key] ?? null,
13
+ setItem: (key: string, value: string) => {
14
+ store[key] = value;
15
+ },
16
+ removeItem: (key: string) => {
17
+ delete store[key];
18
+ },
19
+ clear: () => {
20
+ for (const key of Object.keys(store)) {
21
+ delete store[key];
22
+ }
23
+ },
24
+ get length() {
25
+ return Object.keys(store).length;
26
+ },
27
+ key: (index: number) => Object.keys(store)[index] ?? null,
28
+ [Symbol.iterator]: function* () {
29
+ yield* Object.entries(store);
30
+ },
31
+ } as unknown as Storage;
32
+ };
33
+
34
+ class FakeLocation implements ILOidcLocation {
35
+ openedUrl: string = null;
36
+
37
+ open(url: string): void {
38
+ this.openedUrl = url;
39
+ }
40
+
41
+ reload(): void {}
42
+
43
+ getCurrentHref(): string {
44
+ return '';
45
+ }
46
+
47
+ getPath(): string {
48
+ return '/current';
49
+ }
50
+
51
+ getOrigin(): string {
52
+ return 'https://client.example.com';
53
+ }
54
+ }
55
+
56
+ const createConfiguration = (overrides: Partial<OidcConfiguration> = {}): OidcConfiguration => {
57
+ const storage = makeStorage();
58
+ return {
59
+ client_id: 'client',
60
+ redirect_uri: 'https://client.example.com/callback',
61
+ scope: 'openid profile',
62
+ authority: 'https://issuer.example.com',
63
+ storage,
64
+ login_state_storage: storage,
65
+ ...overrides,
66
+ };
67
+ };
68
+
69
+ const createMetadata = (overrides: Record<string, unknown> = {}) => ({
70
+ authorizationEndpoint: 'https://issuer.example.com/authorize',
71
+ issuer: 'https://issuer.example.com',
72
+ pushedAuthorizationRequestEndpoint: 'https://issuer.example.com/par',
73
+ requirePushedAuthorizationRequests: false,
74
+ ...overrides,
75
+ });
76
+
77
+ const asFetch = (mock: ReturnType<typeof vi.fn>): Fetch => mock as unknown as Fetch;
78
+
79
+ const runLogin = async ({
80
+ configuration,
81
+ metadata,
82
+ fetchMock = vi.fn(),
83
+ }: {
84
+ configuration: OidcConfiguration;
85
+ metadata: Record<string, unknown>;
86
+ fetchMock?: ReturnType<typeof vi.fn>;
87
+ }) => {
88
+ const location = new FakeLocation();
89
+ const publishEvent = vi.fn();
90
+ const initAsync = vi.fn(async () => metadata);
91
+
92
+ const promise = defaultLoginAsync(
93
+ 'default',
94
+ configuration,
95
+ publishEvent,
96
+ initAsync,
97
+ location,
98
+ () => asFetch(fetchMock),
99
+ )('/return');
100
+
101
+ return { promise, location, publishEvent, fetchMock };
102
+ };
103
+
104
+ describe('PAR mode selection', () => {
105
+ const originalWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
106
+
107
+ beforeAll(() => {
108
+ Object.defineProperty(globalThis, 'window', {
109
+ configurable: true,
110
+ value: { crypto: globalThis.crypto },
111
+ });
112
+ });
113
+
114
+ afterAll(() => {
115
+ if (originalWindow) {
116
+ Object.defineProperty(globalThis, 'window', originalWindow);
117
+ } else {
118
+ delete (globalThis as { window?: unknown }).window;
119
+ }
120
+ });
121
+
122
+ it('maps PAR discovery metadata to the authorization service configuration', () => {
123
+ const metadata = new OidcAuthorizationServiceConfiguration({
124
+ authorization_endpoint: 'https://issuer.example.com/authorize',
125
+ pushed_authorization_request_endpoint: 'https://issuer.example.com/par',
126
+ require_pushed_authorization_requests: true,
127
+ issuer: 'https://issuer.example.com',
128
+ }) as any;
129
+
130
+ expect(metadata.pushedAuthorizationRequestEndpoint).toBe('https://issuer.example.com/par');
131
+ expect(metadata.requirePushedAuthorizationRequests).toBe(true);
132
+ });
133
+
134
+ it('keeps PAR disabled by default even when the server advertises it', async () => {
135
+ const { promise, location, fetchMock } = await runLogin({
136
+ configuration: createConfiguration(),
137
+ metadata: createMetadata(),
138
+ });
139
+
140
+ await promise;
141
+
142
+ expect(fetchMock).not.toHaveBeenCalled();
143
+ const redirect = new URL(location.openedUrl);
144
+ expect(redirect.searchParams.get('request_uri')).toBeNull();
145
+ expect(redirect.searchParams.get('redirect_uri')).toBe('https://client.example.com/callback');
146
+ });
147
+
148
+ it('uses PAR in auto mode when the endpoint is advertised', async () => {
149
+ const fetchMock = vi.fn(async () => ({
150
+ status: 201,
151
+ json: async () => ({
152
+ request_uri: 'urn:ietf:params:oauth:request_uri:auto',
153
+ expires_in: 60,
154
+ }),
155
+ }));
156
+ const { promise, location } = await runLogin({
157
+ configuration: createConfiguration({ par: 'auto' }),
158
+ metadata: createMetadata(),
159
+ fetchMock,
160
+ });
161
+
162
+ await promise;
163
+
164
+ expect(fetchMock).toHaveBeenCalledOnce();
165
+ expect(Object.fromEntries(new URL(location.openedUrl).searchParams)).toEqual({
166
+ client_id: 'client',
167
+ request_uri: 'urn:ietf:params:oauth:request_uri:auto',
168
+ });
169
+ });
170
+
171
+ it('uses the existing front-channel flow in auto mode when PAR is not advertised', async () => {
172
+ const { promise, location, fetchMock } = await runLogin({
173
+ configuration: createConfiguration({ par: 'auto' }),
174
+ metadata: createMetadata({ pushedAuthorizationRequestEndpoint: undefined }),
175
+ });
176
+
177
+ await promise;
178
+
179
+ expect(fetchMock).not.toHaveBeenCalled();
180
+ expect(new URL(location.openedUrl).searchParams.get('redirect_uri')).toBe(
181
+ 'https://client.example.com/callback',
182
+ );
183
+ });
184
+
185
+ it.each([
186
+ ['required', false],
187
+ ['auto', true],
188
+ ] as const)(
189
+ 'fails before navigation in %s mode when no PAR endpoint is available',
190
+ async (par, requirePushedAuthorizationRequests) => {
191
+ const { promise, location, publishEvent } = await runLogin({
192
+ configuration: createConfiguration({ par }),
193
+ metadata: createMetadata({
194
+ pushedAuthorizationRequestEndpoint: undefined,
195
+ requirePushedAuthorizationRequests,
196
+ }),
197
+ });
198
+
199
+ await expect(promise).rejects.toMatchObject({
200
+ code: PushedAuthorizationRequestErrorCode.ENDPOINT_UNAVAILABLE,
201
+ });
202
+ expect(location.openedUrl).toBeNull();
203
+ expect(publishEvent).toHaveBeenLastCalledWith(
204
+ 'loginAsync_error',
205
+ expect.objectContaining({
206
+ code: PushedAuthorizationRequestErrorCode.ENDPOINT_UNAVAILABLE,
207
+ }),
208
+ );
209
+ },
210
+ );
211
+
212
+ it('does not silently downgrade to a front-channel request after a PAR error', async () => {
213
+ const fetchMock = vi.fn(async () => ({
214
+ status: 400,
215
+ json: async () => ({
216
+ error: 'invalid_request',
217
+ error_description: 'Invalid authorization request',
218
+ }),
219
+ }));
220
+ const { promise, location } = await runLogin({
221
+ configuration: createConfiguration({ par: 'auto' }),
222
+ metadata: createMetadata(),
223
+ fetchMock,
224
+ });
225
+
226
+ await expect(promise).rejects.toMatchObject({
227
+ code: PushedAuthorizationRequestErrorCode.REQUEST_FAILED,
228
+ status: 400,
229
+ });
230
+ expect(location.openedUrl).toBeNull();
231
+ });
232
+ });
@@ -36,8 +36,9 @@ describe('ParseTokens test Suite', () => {
36
36
  refresh_time_before_tokens_expiration_in_second: 0,
37
37
  },
38
38
  syncTokensInfoAsync: async () => synchroniseTokensStatus.TOKENS_VALID,
39
- renewTokensAsync: async (_extras: StringMap) => {
39
+ renewTokensAsync: async (_extras: StringMap): Promise<undefined> => {
40
40
  await sleepAsync({ milliseconds: 10 });
41
+ return undefined;
41
42
  },
42
43
  };
43
44
  const result = await getValidTokenAsync(oidc, 1, 1);
@@ -200,7 +200,7 @@ export interface OidcToken {
200
200
  token_automatic_renew_mode?: TokenAutomaticRenewMode;
201
201
  refresh_time_before_tokens_expiration_in_second?: number;
202
202
  };
203
- renewTokensAsync: (extras: StringMap) => Promise<void>;
203
+ renewTokensAsync: (extras: StringMap) => Promise<Tokens | null | undefined>;
204
204
  }
205
205
 
206
206
  export const getValidTokenAsync = async (
@@ -0,0 +1,43 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { OidcError } from './oidcError';
4
+ import {
5
+ isPushedAuthorizationRequestError,
6
+ PushedAuthorizationRequestError,
7
+ PushedAuthorizationRequestErrorCode,
8
+ } from './pushedAuthorizationRequestError';
9
+
10
+ describe('PushedAuthorizationRequestError', () => {
11
+ it('exposes stable error codes', () => {
12
+ expect(PushedAuthorizationRequestErrorCode.ENDPOINT_UNAVAILABLE).toBe('ENDPOINT_UNAVAILABLE');
13
+ expect(PushedAuthorizationRequestErrorCode.REQUEST_FAILED).toBe('REQUEST_FAILED');
14
+ expect(PushedAuthorizationRequestErrorCode.INVALID_RESPONSE).toBe('INVALID_RESPONSE');
15
+ });
16
+
17
+ it('preserves PAR and OAuth error details', () => {
18
+ const cause = new Error('network failure');
19
+ const error = new PushedAuthorizationRequestError(
20
+ PushedAuthorizationRequestErrorCode.REQUEST_FAILED,
21
+ 'PAR failed',
22
+ {
23
+ status: 400,
24
+ oauthError: 'invalid_request',
25
+ oauthErrorDescription: 'Invalid redirect URI',
26
+ cause,
27
+ },
28
+ );
29
+
30
+ expect(error).toBeInstanceOf(Error);
31
+ expect(error).toBeInstanceOf(OidcError);
32
+ expect(error.name).toBe('PushedAuthorizationRequestError');
33
+ expect(error.code).toBe('REQUEST_FAILED');
34
+ expect(error.status).toBe(400);
35
+ expect(error.oauthError).toBe('invalid_request');
36
+ expect(error.oauthErrorDescription).toBe('Invalid redirect URI');
37
+ expect(error.cause).toBe(cause);
38
+ expect(error.phase).toBe('login');
39
+ expect(error.retryable).toBe(true);
40
+ expect(isPushedAuthorizationRequestError(error)).toBe(true);
41
+ expect(isPushedAuthorizationRequestError(new Error('other'))).toBe(false);
42
+ });
43
+ });
@@ -0,0 +1,50 @@
1
+ import {
2
+ isRetryableHttpStatus,
3
+ OidcError,
4
+ OidcErrorCode as BaseOidcErrorCode,
5
+ } from './oidcError.js';
6
+
7
+ export const PushedAuthorizationRequestErrorCode = {
8
+ ENDPOINT_UNAVAILABLE: 'ENDPOINT_UNAVAILABLE',
9
+ REQUEST_FAILED: 'REQUEST_FAILED',
10
+ INVALID_RESPONSE: 'INVALID_RESPONSE',
11
+ } as const;
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
14
+ export type PushedAuthorizationRequestErrorCode =
15
+ (typeof PushedAuthorizationRequestErrorCode)[keyof typeof PushedAuthorizationRequestErrorCode];
16
+
17
+ type PushedAuthorizationRequestErrorOptions = {
18
+ status?: number;
19
+ oauthError?: string;
20
+ oauthErrorDescription?: string;
21
+ cause?: unknown;
22
+ };
23
+
24
+ export class PushedAuthorizationRequestError extends OidcError {
25
+ declare readonly code: PushedAuthorizationRequestErrorCode;
26
+
27
+ constructor(
28
+ code: PushedAuthorizationRequestErrorCode,
29
+ message: string,
30
+ options: PushedAuthorizationRequestErrorOptions = {},
31
+ ) {
32
+ super(code as BaseOidcErrorCode, message, {
33
+ phase: 'login',
34
+ retryable: options.cause !== undefined || isRetryableHttpStatus(options.status),
35
+ status: options.status,
36
+ oauthError: options.oauthError,
37
+ oauthErrorDescription: options.oauthErrorDescription,
38
+ cause: options.cause,
39
+ });
40
+ this.name = 'PushedAuthorizationRequestError';
41
+
42
+ Object.setPrototypeOf(this, PushedAuthorizationRequestError.prototype);
43
+ }
44
+ }
45
+
46
+ export const isPushedAuthorizationRequestError = (
47
+ value: unknown,
48
+ ): value is PushedAuthorizationRequestError => {
49
+ return value instanceof PushedAuthorizationRequestError;
50
+ };