@axa-fr/oidc-client 7.29.8 → 7.30.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 (76) hide show
  1. package/README.md +187 -0
  2. package/dist/events.d.ts +3 -0
  3. package/dist/events.d.ts.map +1 -1
  4. package/dist/fetch.d.ts +2 -2
  5. package/dist/fetch.d.ts.map +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1339 -871
  9. package/dist/index.umd.cjs +2 -2
  10. package/dist/initSession.d.ts +2 -0
  11. package/dist/initSession.d.ts.map +1 -1
  12. package/dist/initWorker.d.ts +1 -25
  13. package/dist/initWorker.d.ts.map +1 -1
  14. package/dist/jwt.d.ts.map +1 -1
  15. package/dist/keepSession.d.ts.map +1 -1
  16. package/dist/keepSession.spec.d.ts +2 -0
  17. package/dist/keepSession.spec.d.ts.map +1 -0
  18. package/dist/legacyCompatibility.spec.d.ts +2 -0
  19. package/dist/legacyCompatibility.spec.d.ts.map +1 -0
  20. package/dist/login.d.ts +2 -1
  21. package/dist/login.d.ts.map +1 -1
  22. package/dist/logout.d.ts.map +1 -1
  23. package/dist/node_modules/@axa-fr/oidc-client-service-worker/dist/OidcServiceWorker.js +1049 -0
  24. package/dist/node_modules/@axa-fr/oidc-client-service-worker/dist/OidcTrustedDomains.js +31 -0
  25. package/dist/oidc.d.ts +6 -1
  26. package/dist/oidc.d.ts.map +1 -1
  27. package/dist/oidcClient.d.ts +13 -2
  28. package/dist/oidcClient.d.ts.map +1 -1
  29. package/dist/oidcError.d.ts +5 -0
  30. package/dist/oidcError.d.ts.map +1 -1
  31. package/dist/parseTokens.d.ts +10 -0
  32. package/dist/parseTokens.d.ts.map +1 -1
  33. package/dist/protocol.d.ts +7 -1
  34. package/dist/protocol.d.ts.map +1 -1
  35. package/dist/renewTokens.d.ts +0 -6
  36. package/dist/renewTokens.d.ts.map +1 -1
  37. package/dist/requests.d.ts +3 -3
  38. package/dist/requests.d.ts.map +1 -1
  39. package/dist/resourceParameters.d.ts +9 -0
  40. package/dist/resourceParameters.d.ts.map +1 -0
  41. package/dist/resourceTokens.d.ts +34 -0
  42. package/dist/resourceTokens.d.ts.map +1 -0
  43. package/dist/resourceTokens.spec.d.ts +2 -0
  44. package/dist/resourceTokens.spec.d.ts.map +1 -0
  45. package/dist/silentLogin.d.ts +2 -1
  46. package/dist/silentLogin.d.ts.map +1 -1
  47. package/dist/types.d.ts +28 -0
  48. package/dist/types.d.ts.map +1 -1
  49. package/dist/version.d.ts +1 -1
  50. package/package.json +3 -3
  51. package/src/events.ts +3 -0
  52. package/src/fetch.ts +115 -15
  53. package/src/index.ts +4 -0
  54. package/src/initSession.ts +9 -0
  55. package/src/initWorker.ts +65 -13
  56. package/src/jwt.spec.ts +17 -0
  57. package/src/jwt.ts +4 -1
  58. package/src/keepSession.spec.ts +254 -0
  59. package/src/keepSession.ts +10 -4
  60. package/src/legacyCompatibility.spec.ts +142 -0
  61. package/src/login.ts +91 -29
  62. package/src/logout.ts +36 -0
  63. package/src/oidc.ts +71 -20
  64. package/src/oidcClient.ts +48 -3
  65. package/src/oidcError.ts +7 -0
  66. package/src/parseTokens.ts +14 -1
  67. package/src/protocol.ts +16 -1
  68. package/src/renewTokens.ts +19 -8
  69. package/src/requests.ts +13 -11
  70. package/src/resourceParameters.ts +85 -0
  71. package/src/resourceTokens.spec.ts +1271 -0
  72. package/src/resourceTokens.ts +708 -0
  73. package/src/silentLogin.spec.ts +87 -1
  74. package/src/silentLogin.ts +24 -22
  75. package/src/types.ts +34 -0
  76. package/src/version.ts +1 -1
@@ -2,7 +2,10 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
2
2
 
3
3
  import { OidcError, OidcErrorCode, serializeOidcError } from './oidcError';
4
4
  import { isOidcStateError, OidcStateError, OidcStateErrorCode } from './oidcStateError';
5
- import { _silentLoginAsync } from './silentLogin';
5
+ import { type Tokens } from './parseTokens';
6
+ import * as renewal from './renewTokens';
7
+ import { _silentLoginAsync, defaultSilentLoginAsync } from './silentLogin';
8
+ import timer from './timer';
6
9
 
7
10
  const installDom = () => {
8
11
  const contentWindow = {};
@@ -30,10 +33,93 @@ const installDom = () => {
30
33
  };
31
34
 
32
35
  afterEach(() => {
36
+ vi.restoreAllMocks();
33
37
  vi.useRealTimers();
34
38
  vi.unstubAllGlobals();
35
39
  });
36
40
 
41
+ describe('explicit silent login', () => {
42
+ const configuration = {
43
+ authority: 'https://issuer.example.com',
44
+ client_id: 'client',
45
+ redirect_uri: 'https://client.example.com/callback',
46
+ scope: 'openid',
47
+ silent_login_uri: 'https://client.example.com/silent-login',
48
+ silent_redirect_uri: 'https://client.example.com/silent-callback',
49
+ silent_login_timeout: 25,
50
+ };
51
+ const tokens: Tokens = {
52
+ accessToken: 'access',
53
+ refreshToken: 'refresh',
54
+ idToken: 'id',
55
+ accessTokenPayload: null,
56
+ idTokenPayload: null,
57
+ expiresAt: 100,
58
+ issuedAt: 0,
59
+ };
60
+
61
+ it('returns provider errors without overwriting an existing session or stopping renewal', async () => {
62
+ const { contentWindow, getListener } = installDom();
63
+ const publishEvent = vi.fn();
64
+ const oidc = { tokens, timeoutId: 123 };
65
+ const clearTimeout = vi.spyOn(timer, 'clearTimeout').mockImplementation(() => undefined);
66
+ const renew = vi
67
+ .spyOn(renewal, 'autoRenewTokens')
68
+ .mockReturnValue(setTimeout(() => undefined, 0));
69
+ const promise = defaultSilentLoginAsync(window, 'custom', configuration, publishEvent, oidc)();
70
+ getListener()({
71
+ origin: 'https://client.example.com',
72
+ source: contentWindow,
73
+ data: 'custom_oidc_error:{"error":"login_required","error_description":"Sign in first."}',
74
+ } as unknown as MessageEvent);
75
+
76
+ await expect(promise).resolves.toMatchObject({
77
+ code: OidcErrorCode.LOGIN_REQUIRED,
78
+ oauthError: 'login_required',
79
+ });
80
+ expect(oidc.tokens).toBe(tokens);
81
+ expect(clearTimeout).not.toHaveBeenCalled();
82
+ expect(renew).not.toHaveBeenCalled();
83
+ expect(publishEvent).not.toHaveBeenCalledWith('token_acquired', expect.anything());
84
+ });
85
+
86
+ it('preserves fulfilled error values on timeout', async () => {
87
+ vi.useFakeTimers();
88
+ installDom();
89
+ const promise = defaultSilentLoginAsync(window, 'custom', configuration, vi.fn(), {
90
+ tokens: null,
91
+ timeoutId: null,
92
+ })();
93
+ const result = expect(promise).resolves.toMatchObject({
94
+ code: OidcErrorCode.SILENT_LOGIN_TIMEOUT,
95
+ });
96
+ await vi.advanceTimersByTimeAsync(25);
97
+ await result;
98
+ });
99
+
100
+ it('adopts successful iframe tokens and restarts automatic renewal', async () => {
101
+ const { contentWindow, getListener } = installDom();
102
+ const publishEvent = vi.fn();
103
+ const oidc = { tokens: null, timeoutId: 123 };
104
+ const clearTimeout = vi.spyOn(timer, 'clearTimeout').mockImplementation(() => undefined);
105
+ const renew = vi
106
+ .spyOn(renewal, 'autoRenewTokens')
107
+ .mockReturnValue(setTimeout(() => undefined, 0));
108
+ const promise = defaultSilentLoginAsync(window, 'custom', configuration, publishEvent, oidc)();
109
+ getListener()({
110
+ origin: 'https://client.example.com',
111
+ source: contentWindow,
112
+ data: `custom_oidc_tokens:${JSON.stringify({ tokens, sessionState: null })}`,
113
+ } as unknown as MessageEvent);
114
+
115
+ await expect(promise).resolves.toEqual({});
116
+ expect(oidc.tokens).toEqual(tokens);
117
+ expect(clearTimeout).toHaveBeenCalledWith(123);
118
+ expect(renew).toHaveBeenCalledWith(oidc, tokens.expiresAt, {}, undefined);
119
+ expect(publishEvent).toHaveBeenCalledWith('token_acquired', {});
120
+ });
121
+ });
122
+
37
123
  describe('_silentLoginAsync typed errors', () => {
38
124
  it('rejects with SILENT_LOGIN_TIMEOUT and publishes the same error', async () => {
39
125
  vi.useFakeTimers();
@@ -11,6 +11,7 @@ import { autoRenewTokens } from './renewTokens.js';
11
11
  import timer from './timer.js';
12
12
  import { OidcConfiguration, StringMap } from './types.js';
13
13
  export type SilentLoginResponse = {
14
+ resource?: string;
14
15
  tokens: Tokens | null;
15
16
  sessionState: string | null;
16
17
  error?: string;
@@ -175,39 +176,40 @@ export const defaultSilentLoginAsync =
175
176
  };
176
177
 
177
178
  const loginLocalAsync = async () => {
178
- if (oidc.timeoutId) {
179
- timer.clearTimeout(oidc.timeoutId);
180
- }
181
-
182
179
  let state;
183
180
  if (extras && 'state' in extras) {
184
181
  state = extras.state;
185
182
  delete extras.state;
186
183
  }
187
184
 
188
- try {
189
- const extraFinal = !configuration.extras ? extras : { ...configuration.extras, ...extras };
190
- const silentResult = await silentLoginAsync(
185
+ const extraFinal = !configuration.extras ? extras : { ...configuration.extras, ...extras };
186
+ const silentResult = await silentLoginAsync(
187
+ {
188
+ ...extraFinal,
189
+ prompt: 'none',
190
+ },
191
+ state,
192
+ scope,
193
+ );
194
+ if (!silentResult) return;
195
+ if (silentResult.oidcError) throw silentResult.oidcError;
196
+ if (!silentResult.tokens) {
197
+ throw new OidcError(
198
+ OidcErrorCode.TOKEN_REQUEST_FAILED,
199
+ 'Silent login returned no tokens.',
191
200
  {
192
- ...extraFinal,
193
- prompt: 'none',
201
+ phase: 'login',
202
+ retryable: false,
194
203
  },
195
- state,
196
- scope,
197
204
  );
198
-
199
- if (silentResult) {
200
- oidc.tokens = silentResult.tokens;
201
- publishEvent(eventNames.token_acquired, {});
202
- // @ts-ignore
203
- oidc.timeoutId = autoRenewTokens(oidc, oidc.tokens.expiresAt, extras, scope);
204
- return {};
205
- }
206
- } catch (e) {
207
- return e;
208
205
  }
206
+ if (oidc.timeoutId) timer.clearTimeout(oidc.timeoutId);
207
+ oidc.tokens = silentResult.tokens;
208
+ publishEvent(eventNames.token_acquired, {});
209
+ oidc.timeoutId = autoRenewTokens(oidc, oidc.tokens.expiresAt, extras, scope);
210
+ return {};
209
211
  };
210
- return loginLocalAsync();
212
+ return loginLocalAsync().catch(error => error);
211
213
  };
212
214
 
213
215
  export default defaultSilentLoginAsync;
package/src/types.ts CHANGED
@@ -1,5 +1,37 @@
1
+ import type { OidcResourceConfiguration } from './protocol';
2
+
3
+ export type { OidcResourceConfiguration } from './protocol';
4
+
1
5
  export type Fetch = typeof window.fetch;
2
6
 
7
+ /** Access-token metadata; accessToken is a placeholder when the worker hides it. */
8
+ export interface ResourceAccessToken {
9
+ resource: string;
10
+ issuer: string;
11
+ clientId: string;
12
+ configurationName: string;
13
+ accessToken: string;
14
+ expiresAt: number;
15
+ scope?: string;
16
+ tokenType?: string;
17
+ }
18
+
19
+ /** Options for acquiring a resource-specific token. */
20
+ export interface ResourceTokenOptions {
21
+ forceRenew?: boolean;
22
+ }
23
+
24
+ /** Options for explicitly selecting a resource in authenticated fetch. */
25
+ export interface ResourceFetchOptions {
26
+ resource?: string;
27
+ demonstratingProofOfPossession?: boolean;
28
+ }
29
+
30
+ /** OAuth parameters with RFC 8707 repeated resource values. */
31
+ export interface OAuthParameters {
32
+ [key: string]: string | readonly string[];
33
+ }
34
+
3
35
  export type LogoutToken = 'access_token' | 'refresh_token';
4
36
 
5
37
  export type PushedAuthorizationRequestMode = 'disabled' | 'auto' | 'required';
@@ -25,6 +57,8 @@ export type OidcConfiguration = {
25
57
  silent_login_uri?: string;
26
58
  silent_login_timeout?: number;
27
59
  scope: string;
60
+ resource?: string | readonly string[];
61
+ resources?: readonly OidcResourceConfiguration[];
28
62
  authority: string;
29
63
  authority_time_cache_wellknowurl_in_second?: number;
30
64
  authority_timeout_wellknowurl_in_millisecond?: number;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export default '7.29.8';
1
+ export default '7.30.1';