@axa-fr/oidc-client 7.27.19 → 7.29.2
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,105 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createNetworkError,
|
|
5
|
+
createOAuthError,
|
|
6
|
+
deserializeOidcError,
|
|
7
|
+
isOidcError,
|
|
8
|
+
OidcError,
|
|
9
|
+
OidcErrorCode,
|
|
10
|
+
serializeOidcError,
|
|
11
|
+
} from './oidcError';
|
|
12
|
+
|
|
13
|
+
describe('OidcError', () => {
|
|
14
|
+
it('preserves all public fields and the Error prototype chain', () => {
|
|
15
|
+
const cause = new TypeError('fetch failed');
|
|
16
|
+
const error = new OidcError(OidcErrorCode.TOKEN_REQUEST_FAILED, 'Token request failed', {
|
|
17
|
+
phase: 'refresh',
|
|
18
|
+
retryable: true,
|
|
19
|
+
status: 503,
|
|
20
|
+
oauthError: 'temporarily_unavailable',
|
|
21
|
+
oauthErrorDescription: 'Try again later',
|
|
22
|
+
cause,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(error).toBeInstanceOf(Error);
|
|
26
|
+
expect(error).toBeInstanceOf(OidcError);
|
|
27
|
+
expect(error.name).toBe('OidcError');
|
|
28
|
+
expect(error.message).toBe('Token request failed');
|
|
29
|
+
expect(error).toMatchObject({
|
|
30
|
+
code: OidcErrorCode.TOKEN_REQUEST_FAILED,
|
|
31
|
+
phase: 'refresh',
|
|
32
|
+
retryable: true,
|
|
33
|
+
status: 503,
|
|
34
|
+
oauthError: 'temporarily_unavailable',
|
|
35
|
+
oauthErrorDescription: 'Try again later',
|
|
36
|
+
cause,
|
|
37
|
+
});
|
|
38
|
+
expect(isOidcError(error)).toBe(true);
|
|
39
|
+
expect(isOidcError(new Error('other'))).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it.each([
|
|
43
|
+
['login_required', OidcErrorCode.LOGIN_REQUIRED, false],
|
|
44
|
+
['consent_required', OidcErrorCode.CONSENT_REQUIRED, false],
|
|
45
|
+
['interaction_required', OidcErrorCode.INTERACTION_REQUIRED, false],
|
|
46
|
+
['server_error', OidcErrorCode.OAUTH_ERROR, true],
|
|
47
|
+
['temporarily_unavailable', OidcErrorCode.OAUTH_ERROR, true],
|
|
48
|
+
['access_denied', OidcErrorCode.OAUTH_ERROR, false],
|
|
49
|
+
] as const)('maps OAuth error %s to %s', (oauthError, code, retryable) => {
|
|
50
|
+
const error = createOAuthError(
|
|
51
|
+
oauthError,
|
|
52
|
+
'description',
|
|
53
|
+
`Error from OIDC server: ${oauthError} - description`,
|
|
54
|
+
'callback',
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(error).toMatchObject({
|
|
58
|
+
code,
|
|
59
|
+
phase: 'callback',
|
|
60
|
+
retryable,
|
|
61
|
+
oauthError,
|
|
62
|
+
oauthErrorDescription: 'description',
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('wraps a network failure without changing its message', () => {
|
|
67
|
+
const cause = new Error('Failed to fetch');
|
|
68
|
+
const error = createNetworkError(cause, 'userinfo');
|
|
69
|
+
|
|
70
|
+
expect(error).toMatchObject({
|
|
71
|
+
code: OidcErrorCode.NETWORK_ERROR,
|
|
72
|
+
message: 'Failed to fetch',
|
|
73
|
+
phase: 'userinfo',
|
|
74
|
+
retryable: true,
|
|
75
|
+
cause,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('serializes and reconstructs an OidcError without stack or cause', () => {
|
|
80
|
+
const original = new OidcError(OidcErrorCode.LOGIN_REQUIRED, 'login required', {
|
|
81
|
+
phase: 'refresh',
|
|
82
|
+
retryable: false,
|
|
83
|
+
oauthError: 'login_required',
|
|
84
|
+
oauthErrorDescription: 'Sign in again',
|
|
85
|
+
cause: new Error('not cloneable'),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const serialized = serializeOidcError(original);
|
|
89
|
+
expect(serialized).not.toHaveProperty('stack');
|
|
90
|
+
expect(serialized).not.toHaveProperty('cause');
|
|
91
|
+
|
|
92
|
+
const reconstructed = deserializeOidcError(serialized);
|
|
93
|
+
expect(reconstructed).toBeInstanceOf(OidcError);
|
|
94
|
+
expect(reconstructed).toMatchObject({
|
|
95
|
+
name: 'OidcError',
|
|
96
|
+
message: 'login required',
|
|
97
|
+
code: OidcErrorCode.LOGIN_REQUIRED,
|
|
98
|
+
phase: 'refresh',
|
|
99
|
+
retryable: false,
|
|
100
|
+
oauthError: 'login_required',
|
|
101
|
+
oauthErrorDescription: 'Sign in again',
|
|
102
|
+
});
|
|
103
|
+
expect(reconstructed?.cause).toBeUndefined();
|
|
104
|
+
});
|
|
105
|
+
});
|
package/src/oidcError.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
export const OidcErrorCode = {
|
|
2
|
+
LOGIN_REQUIRED: 'LOGIN_REQUIRED',
|
|
3
|
+
CONSENT_REQUIRED: 'CONSENT_REQUIRED',
|
|
4
|
+
INTERACTION_REQUIRED: 'INTERACTION_REQUIRED',
|
|
5
|
+
OAUTH_ERROR: 'OAUTH_ERROR',
|
|
6
|
+
TOKEN_REQUEST_FAILED: 'TOKEN_REQUEST_FAILED',
|
|
7
|
+
SILENT_LOGIN_TIMEOUT: 'SILENT_LOGIN_TIMEOUT',
|
|
8
|
+
INVALID_STATE: 'INVALID_STATE',
|
|
9
|
+
INVALID_NONCE: 'INVALID_NONCE',
|
|
10
|
+
DPOP_NONCE_REQUIRED: 'DPOP_NONCE_REQUIRED',
|
|
11
|
+
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
12
|
+
// Backwards-compatible codes exposed by the existing specialised errors.
|
|
13
|
+
STATE_MISSING: 'STATE_MISSING',
|
|
14
|
+
STATE_MISMATCH: 'STATE_MISMATCH',
|
|
15
|
+
NONCE_MISSING: 'NONCE_MISSING',
|
|
16
|
+
ENDPOINT_UNAVAILABLE: 'ENDPOINT_UNAVAILABLE',
|
|
17
|
+
REQUEST_FAILED: 'REQUEST_FAILED',
|
|
18
|
+
INVALID_RESPONSE: 'INVALID_RESPONSE',
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
22
|
+
export type OidcErrorCode = (typeof OidcErrorCode)[keyof typeof OidcErrorCode];
|
|
23
|
+
|
|
24
|
+
export type OidcErrorPhase =
|
|
25
|
+
'login' | 'callback' | 'refresh' | 'logout' | 'userinfo' | 'api_request';
|
|
26
|
+
|
|
27
|
+
export type OidcErrorOptions = {
|
|
28
|
+
phase: OidcErrorPhase;
|
|
29
|
+
retryable: boolean;
|
|
30
|
+
oauthError?: string;
|
|
31
|
+
oauthErrorDescription?: string;
|
|
32
|
+
status?: number;
|
|
33
|
+
cause?: unknown;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type SerializedOidcError = {
|
|
37
|
+
name: string;
|
|
38
|
+
message: string;
|
|
39
|
+
code: OidcErrorCode;
|
|
40
|
+
phase: OidcErrorPhase;
|
|
41
|
+
retryable: boolean;
|
|
42
|
+
oauthError?: string;
|
|
43
|
+
oauthErrorDescription?: string;
|
|
44
|
+
status?: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const oidcErrorCodes = new Set<string>(Object.values(OidcErrorCode));
|
|
48
|
+
const oidcErrorPhases = new Set<OidcErrorPhase>([
|
|
49
|
+
'login',
|
|
50
|
+
'callback',
|
|
51
|
+
'refresh',
|
|
52
|
+
'logout',
|
|
53
|
+
'userinfo',
|
|
54
|
+
'api_request',
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
export class OidcError extends Error {
|
|
58
|
+
readonly code: OidcErrorCode;
|
|
59
|
+
readonly phase: OidcErrorPhase;
|
|
60
|
+
readonly oauthError?: string;
|
|
61
|
+
readonly oauthErrorDescription?: string;
|
|
62
|
+
readonly retryable: boolean;
|
|
63
|
+
readonly status?: number;
|
|
64
|
+
readonly cause?: unknown;
|
|
65
|
+
|
|
66
|
+
constructor(code: OidcErrorCode, message: string, options: OidcErrorOptions) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.name = 'OidcError';
|
|
69
|
+
this.code = code;
|
|
70
|
+
this.phase = options.phase;
|
|
71
|
+
this.oauthError = options.oauthError;
|
|
72
|
+
this.oauthErrorDescription = options.oauthErrorDescription;
|
|
73
|
+
this.retryable = options.retryable;
|
|
74
|
+
this.status = options.status;
|
|
75
|
+
this.cause = options.cause;
|
|
76
|
+
|
|
77
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const isOidcError = (value: unknown): value is OidcError => value instanceof OidcError;
|
|
82
|
+
|
|
83
|
+
export const isRetryableHttpStatus = (status?: number): boolean =>
|
|
84
|
+
status === 408 || status === 429 || (status !== undefined && status >= 500);
|
|
85
|
+
|
|
86
|
+
export const oidcErrorCodeFromOAuthError = (oauthError?: string): OidcErrorCode => {
|
|
87
|
+
switch (oauthError) {
|
|
88
|
+
case 'login_required':
|
|
89
|
+
return OidcErrorCode.LOGIN_REQUIRED;
|
|
90
|
+
case 'consent_required':
|
|
91
|
+
return OidcErrorCode.CONSENT_REQUIRED;
|
|
92
|
+
case 'interaction_required':
|
|
93
|
+
return OidcErrorCode.INTERACTION_REQUIRED;
|
|
94
|
+
case 'use_dpop_nonce':
|
|
95
|
+
return OidcErrorCode.DPOP_NONCE_REQUIRED;
|
|
96
|
+
default:
|
|
97
|
+
return OidcErrorCode.OAUTH_ERROR;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const createOAuthError = (
|
|
102
|
+
oauthError: string | undefined,
|
|
103
|
+
oauthErrorDescription: string | undefined,
|
|
104
|
+
message: string,
|
|
105
|
+
phase: OidcErrorPhase,
|
|
106
|
+
status?: number,
|
|
107
|
+
): OidcError => {
|
|
108
|
+
const code = oidcErrorCodeFromOAuthError(oauthError);
|
|
109
|
+
const requiresInteraction =
|
|
110
|
+
code === OidcErrorCode.LOGIN_REQUIRED ||
|
|
111
|
+
code === OidcErrorCode.CONSENT_REQUIRED ||
|
|
112
|
+
code === OidcErrorCode.INTERACTION_REQUIRED;
|
|
113
|
+
const retryable =
|
|
114
|
+
!requiresInteraction &&
|
|
115
|
+
(code === OidcErrorCode.DPOP_NONCE_REQUIRED ||
|
|
116
|
+
oauthError === 'server_error' ||
|
|
117
|
+
oauthError === 'temporarily_unavailable' ||
|
|
118
|
+
isRetryableHttpStatus(status));
|
|
119
|
+
return new OidcError(code, message, {
|
|
120
|
+
phase,
|
|
121
|
+
retryable,
|
|
122
|
+
oauthError,
|
|
123
|
+
oauthErrorDescription,
|
|
124
|
+
status,
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export const createNetworkError = (
|
|
129
|
+
cause: unknown,
|
|
130
|
+
phase: OidcErrorPhase,
|
|
131
|
+
fallbackMessage = 'Network request failed',
|
|
132
|
+
): OidcError => {
|
|
133
|
+
const message =
|
|
134
|
+
cause instanceof Error && typeof cause.message === 'string' && cause.message
|
|
135
|
+
? cause.message
|
|
136
|
+
: fallbackMessage;
|
|
137
|
+
return new OidcError(OidcErrorCode.NETWORK_ERROR, message, {
|
|
138
|
+
phase,
|
|
139
|
+
retryable: true,
|
|
140
|
+
cause,
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const isNetworkErrorCause = (value: unknown): boolean => {
|
|
145
|
+
if (!(value instanceof Error)) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return (
|
|
149
|
+
value.name === 'AbortError' ||
|
|
150
|
+
value.name === 'NetworkError' ||
|
|
151
|
+
value.message === 'Network request failed' ||
|
|
152
|
+
value.message === 'Failed to fetch' ||
|
|
153
|
+
value.message === 'fetch failed'
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const serializeOidcError = (error: OidcError): SerializedOidcError => ({
|
|
158
|
+
name: error.name,
|
|
159
|
+
message: error.message,
|
|
160
|
+
code: error.code,
|
|
161
|
+
phase: error.phase,
|
|
162
|
+
retryable: error.retryable,
|
|
163
|
+
oauthError: error.oauthError,
|
|
164
|
+
oauthErrorDescription: error.oauthErrorDescription,
|
|
165
|
+
status: error.status,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
export const deserializeOidcError = (value: unknown): OidcError | null => {
|
|
169
|
+
if (!value || typeof value !== 'object') {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
const serialized = value as Partial<SerializedOidcError>;
|
|
173
|
+
if (
|
|
174
|
+
typeof serialized.message !== 'string' ||
|
|
175
|
+
typeof serialized.code !== 'string' ||
|
|
176
|
+
!oidcErrorCodes.has(serialized.code) ||
|
|
177
|
+
typeof serialized.phase !== 'string' ||
|
|
178
|
+
!oidcErrorPhases.has(serialized.phase as OidcErrorPhase) ||
|
|
179
|
+
typeof serialized.retryable !== 'boolean'
|
|
180
|
+
) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const error = new OidcError(serialized.code as OidcErrorCode, serialized.message, {
|
|
184
|
+
phase: serialized.phase as OidcErrorPhase,
|
|
185
|
+
retryable: serialized.retryable,
|
|
186
|
+
oauthError: typeof serialized.oauthError === 'string' ? serialized.oauthError : undefined,
|
|
187
|
+
oauthErrorDescription:
|
|
188
|
+
typeof serialized.oauthErrorDescription === 'string'
|
|
189
|
+
? serialized.oauthErrorDescription
|
|
190
|
+
: undefined,
|
|
191
|
+
status: typeof serialized.status === 'number' ? serialized.status : undefined,
|
|
192
|
+
});
|
|
193
|
+
if (typeof serialized.name === 'string' && serialized.name) {
|
|
194
|
+
error.name = serialized.name;
|
|
195
|
+
}
|
|
196
|
+
return error;
|
|
197
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
|
|
3
|
+
import { deserializeOidcError, OidcError, serializeOidcError } from './oidcError';
|
|
3
4
|
import { isOidcStateError, OidcStateError, OidcStateErrorCode } from './oidcStateError';
|
|
4
5
|
|
|
5
6
|
describe('OidcStateError', () => {
|
|
@@ -12,6 +13,7 @@ describe('OidcStateError', () => {
|
|
|
12
13
|
it('is an Error subclass with name "OidcStateError"', () => {
|
|
13
14
|
const err = new OidcStateError(OidcStateErrorCode.STATE_MISSING, 'missing');
|
|
14
15
|
expect(err).toBeInstanceOf(Error);
|
|
16
|
+
expect(err).toBeInstanceOf(OidcError);
|
|
15
17
|
expect(err).toBeInstanceOf(OidcStateError);
|
|
16
18
|
expect(err.name).toBe('OidcStateError');
|
|
17
19
|
});
|
|
@@ -20,6 +22,8 @@ describe('OidcStateError', () => {
|
|
|
20
22
|
const err = new OidcStateError(OidcStateErrorCode.STATE_MISMATCH, 'mismatch happened');
|
|
21
23
|
expect(err.code).toBe('STATE_MISMATCH');
|
|
22
24
|
expect(err.message).toBe('mismatch happened');
|
|
25
|
+
expect(err.phase).toBe('callback');
|
|
26
|
+
expect(err.retryable).toBe(false);
|
|
23
27
|
});
|
|
24
28
|
|
|
25
29
|
it('is detectable via isOidcStateError', () => {
|
|
@@ -30,4 +34,18 @@ describe('OidcStateError', () => {
|
|
|
30
34
|
expect(isOidcStateError(undefined)).toBe(false);
|
|
31
35
|
expect(isOidcStateError({ code: 'STATE_MISSING' })).toBe(false);
|
|
32
36
|
});
|
|
37
|
+
|
|
38
|
+
it('stays detectable after serialization and deserialization', () => {
|
|
39
|
+
const err = new OidcStateError(OidcStateErrorCode.NONCE_MISSING, 'nonce missing', 'refresh');
|
|
40
|
+
const reconstructed = deserializeOidcError(serializeOidcError(err));
|
|
41
|
+
|
|
42
|
+
expect(reconstructed).toBeInstanceOf(OidcError);
|
|
43
|
+
expect(reconstructed).not.toBeInstanceOf(OidcStateError);
|
|
44
|
+
expect(isOidcStateError(reconstructed)).toBe(true);
|
|
45
|
+
expect(reconstructed).toMatchObject({
|
|
46
|
+
code: OidcStateErrorCode.NONCE_MISSING,
|
|
47
|
+
phase: 'refresh',
|
|
48
|
+
name: 'OidcStateError',
|
|
49
|
+
});
|
|
50
|
+
});
|
|
33
51
|
});
|
package/src/oidcStateError.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isOidcError,
|
|
3
|
+
OidcError,
|
|
4
|
+
OidcErrorCode as BaseOidcErrorCode,
|
|
5
|
+
OidcErrorPhase,
|
|
6
|
+
} from './oidcError.js';
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Stable, machine-readable codes for OIDC state / nonce failures occurring
|
|
3
10
|
* between the authorization redirect and the callback handling.
|
|
@@ -21,20 +28,32 @@ export const OidcStateErrorCode = {
|
|
|
21
28
|
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
22
29
|
export type OidcStateErrorCode = (typeof OidcStateErrorCode)[keyof typeof OidcStateErrorCode];
|
|
23
30
|
|
|
31
|
+
const oidcStateErrorCodes = new Set<string>(Object.values(OidcStateErrorCode));
|
|
32
|
+
|
|
33
|
+
export const isOidcStateErrorCode = (value: unknown): value is OidcStateErrorCode =>
|
|
34
|
+
typeof value === 'string' && oidcStateErrorCodes.has(value);
|
|
35
|
+
|
|
24
36
|
/**
|
|
25
37
|
* Typed error thrown when the OIDC login state or nonce is missing,
|
|
26
38
|
* corrupted, or does not match the value returned by the authorization server.
|
|
27
39
|
*
|
|
28
|
-
* Consumers
|
|
29
|
-
* relying on the (unstable) error message text
|
|
40
|
+
* Consumers should prefer {@link isOidcStateError} and inspect `code` instead
|
|
41
|
+
* of relying on the (unstable) error message text, especially when errors may
|
|
42
|
+
* cross a postMessage boundary during silent renew.
|
|
30
43
|
*/
|
|
31
|
-
export class OidcStateError extends
|
|
32
|
-
readonly code: OidcStateErrorCode;
|
|
44
|
+
export class OidcStateError extends OidcError {
|
|
45
|
+
declare readonly code: OidcStateErrorCode;
|
|
33
46
|
|
|
34
|
-
constructor(
|
|
35
|
-
|
|
47
|
+
constructor(
|
|
48
|
+
code: OidcStateErrorCode,
|
|
49
|
+
message: string,
|
|
50
|
+
phase: Extract<OidcErrorPhase, 'callback' | 'refresh'> = 'callback',
|
|
51
|
+
) {
|
|
52
|
+
super(code as BaseOidcErrorCode, message, {
|
|
53
|
+
phase,
|
|
54
|
+
retryable: false,
|
|
55
|
+
});
|
|
36
56
|
this.name = 'OidcStateError';
|
|
37
|
-
this.code = code;
|
|
38
57
|
|
|
39
58
|
// Keep prototype chain intact when transpiled to ES5.
|
|
40
59
|
Object.setPrototypeOf(this, OidcStateError.prototype);
|
|
@@ -46,5 +65,7 @@ export class OidcStateError extends Error {
|
|
|
46
65
|
* specifically to state/nonce failures.
|
|
47
66
|
*/
|
|
48
67
|
export const isOidcStateError = (value: unknown): value is OidcStateError => {
|
|
49
|
-
return
|
|
68
|
+
return (
|
|
69
|
+
value instanceof OidcStateError || (isOidcError(value) && isOidcStateErrorCode(value.code))
|
|
70
|
+
);
|
|
50
71
|
};
|
package/src/par.spec.ts
ADDED
|
@@ -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
|
+
});
|
package/src/parseTokens.spec.ts
CHANGED
|
@@ -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);
|
package/src/parseTokens.ts
CHANGED
|
@@ -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<
|
|
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
|
+
});
|