@whook/oauth2 26.0.0 → 26.0.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.
Files changed (85) hide show
  1. package/dist/index.d.ts +6 -2
  2. package/dist/index.js +5 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.test.js +421 -2
  5. package/dist/index.test.js.map +1 -1
  6. package/dist/libs/authorizationRequests.d.ts +6 -0
  7. package/dist/libs/authorizationRequests.js +22 -0
  8. package/dist/libs/authorizationRequests.js.map +1 -0
  9. package/dist/libs/authorizationRequests.test.d.ts +1 -0
  10. package/dist/libs/authorizationRequests.test.js +24 -0
  11. package/dist/libs/authorizationRequests.test.js.map +1 -0
  12. package/dist/libs/errors.d.ts +31 -1
  13. package/dist/libs/errors.js +49 -0
  14. package/dist/libs/errors.js.map +1 -1
  15. package/dist/libs/redirectURI.d.ts +1 -0
  16. package/dist/libs/redirectURI.js +16 -0
  17. package/dist/libs/redirectURI.js.map +1 -1
  18. package/dist/libs/schemas.d.ts +8 -0
  19. package/dist/libs/schemas.js +8 -0
  20. package/dist/libs/schemas.js.map +1 -1
  21. package/dist/libs/verifier.d.ts +3 -0
  22. package/dist/libs/verifier.js +14 -0
  23. package/dist/libs/verifier.js.map +1 -1
  24. package/dist/routes/getOAuth2Authorize.d.ts +30 -14
  25. package/dist/routes/getOAuth2Authorize.js +92 -30
  26. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  27. package/dist/routes/getOAuth2Authorize.test.js +826 -19
  28. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  29. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +10 -3
  30. package/dist/routes/getOAuth2WellKnownMetadata.js +30 -5
  31. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  32. package/dist/routes/getOAuth2WellKnownMetadata.test.js +20 -18
  33. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  34. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  35. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  36. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  37. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  38. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  39. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  40. package/dist/routes/postOAuth2Token.d.ts +12 -0
  41. package/dist/routes/postOAuth2Token.js +12 -0
  42. package/dist/routes/postOAuth2Token.js.map +1 -1
  43. package/dist/services/authCookies.js +8 -4
  44. package/dist/services/authCookies.js.map +1 -1
  45. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +2 -7
  46. package/dist/services/oAuth2AuthorizationCodeGranter.js +5 -22
  47. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
  48. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +12 -141
  49. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -1
  50. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  51. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  52. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  53. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  54. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  55. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  56. package/dist/services/oAuth2Granters.d.ts +10 -12
  57. package/dist/services/oAuth2Granters.js.map +1 -1
  58. package/dist/services/oAuth2ImplicitGranter.js +3 -14
  59. package/dist/services/oAuth2ImplicitGranter.js.map +1 -1
  60. package/dist/services/oAuth2ImplicitGranter.test.js +25 -163
  61. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -1
  62. package/package.json +7 -6
  63. package/src/index.test.ts +459 -3
  64. package/src/index.ts +17 -0
  65. package/src/libs/authorizationRequests.test.ts +40 -0
  66. package/src/libs/authorizationRequests.ts +42 -0
  67. package/src/libs/errors.ts +87 -1
  68. package/src/libs/redirectURI.ts +22 -0
  69. package/src/libs/schemas.ts +10 -0
  70. package/src/libs/verifier.ts +18 -0
  71. package/src/routes/getOAuth2Authorize.test.ts +940 -23
  72. package/src/routes/getOAuth2Authorize.ts +168 -49
  73. package/src/routes/getOAuth2WellKnownMetadata.test.ts +22 -20
  74. package/src/routes/getOAuth2WellKnownMetadata.ts +44 -8
  75. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  76. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  77. package/src/routes/postOAuth2Token.ts +12 -0
  78. package/src/services/authCookies.ts +8 -4
  79. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +12 -169
  80. package/src/services/oAuth2AuthorizationCodeGranter.ts +8 -31
  81. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  82. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  83. package/src/services/oAuth2Granters.ts +12 -14
  84. package/src/services/oAuth2ImplicitGranter.test.ts +29 -190
  85. package/src/services/oAuth2ImplicitGranter.ts +3 -19
package/src/index.ts CHANGED
@@ -5,10 +5,12 @@ import initGetOAuth2Authorize, {
5
5
  redirectURIParameter as getOAuth2AuthorizeRedirectURIParameter,
6
6
  scopeParameter as getOAuth2AuthorizeScopeParameter,
7
7
  stateParameter as getOAuth2AuthorizeStateParameter,
8
+ requestURIParameter as getOAuth2AuthorizeRequestURIParameter,
8
9
  codeChallengeSchema as getOAuth2AuthorizeCodeChallengeSchema,
9
10
  codeChallengeParameter as getOAuth2AuthorizeCodeChallengeParameter,
10
11
  codeChallengeMethodSchema as getOAuth2AuthorizeCodeChallengeMethodSchema,
11
12
  codeChallengeMethodParameter as getOAuth2AuthorizeCodeChallengeMethodParameter,
13
+ requestURISchema as getOAuth2AuthorizeRequestURISchema,
12
14
  scopeSchema as getOAuth2AuthorizeScopeSchema,
13
15
  } from './routes/getOAuth2Authorize.js';
14
16
  import initPostOAuth2Acknowledge, {
@@ -23,6 +25,11 @@ import initPostOAuth2Token, {
23
25
  refreshTokenRequestBodySchema as postOAuth2TokenRefreshTokenRequestBodySchema,
24
26
  tokenBodySchema as postOAuth2TokenTokenBodySchema,
25
27
  } from './routes/postOAuth2Token.js';
28
+ import initPostOAuth2PushedAuthorizationRequest, {
29
+ definition as postOAuth2PushedAuthorizationRequestDefinition,
30
+ pushedAuthorizationRequestBodySchema as postOAuth2PushedAuthorizationRequestBodySchema,
31
+ requestURISchema as postOAuth2PushedAuthorizationRequestRequestURISchema,
32
+ } from './routes/postOAuth2PushedAuthorizationRequest.js';
26
33
  import initGetOAuth2WellKnown, {
27
34
  definition as getOAuth2WellKnownDefinition,
28
35
  endpointAuthenticationMethodsSchema as getOAuth2WellKnownEndpointAuthenticationMethodsSchema,
@@ -72,6 +79,9 @@ import initAuthCookiesService from './services/authCookies.js';
72
79
  import { type OAuth2YErrorRegistry } from './libs/errors.js';
73
80
  export * from './services/authCookies.js';
74
81
  export type * from './services/authCookies.js';
82
+ import initOAuth2AuthorizationRequests from './services/oAuth2AuthorizationRequests.js';
83
+ export * from './services/oAuth2AuthorizationRequests.js';
84
+ export type * from './services/oAuth2AuthorizationRequests.js';
75
85
 
76
86
  export * from './libs/schemas.js';
77
87
  export type * from './libs/schemas.js';
@@ -100,11 +110,13 @@ export {
100
110
  getOAuth2AuthorizeRedirectURIParameter,
101
111
  getOAuth2AuthorizeScopeParameter,
102
112
  getOAuth2AuthorizeStateParameter,
113
+ getOAuth2AuthorizeRequestURIParameter,
103
114
  getOAuth2AuthorizeScopeSchema,
104
115
  getOAuth2AuthorizeCodeChallengeSchema,
105
116
  getOAuth2AuthorizeCodeChallengeMethodSchema,
106
117
  getOAuth2AuthorizeCodeChallengeParameter,
107
118
  getOAuth2AuthorizeCodeChallengeMethodParameter,
119
+ getOAuth2AuthorizeRequestURISchema,
108
120
  initGetOAuth2WellKnown,
109
121
  getOAuth2WellKnownDefinition,
110
122
  getOAuth2WellKnownEndpointAuthenticationMethodsSchema,
@@ -132,6 +144,10 @@ export {
132
144
  postOAuth2TokenClientCredentialsTokenRequestBodySchema,
133
145
  postOAuth2TokenRefreshTokenRequestBodySchema,
134
146
  postOAuth2TokenTokenBodySchema,
147
+ initPostOAuth2PushedAuthorizationRequest,
148
+ postOAuth2PushedAuthorizationRequestDefinition,
149
+ postOAuth2PushedAuthorizationRequestBodySchema,
150
+ postOAuth2PushedAuthorizationRequestRequestURISchema,
135
151
  initOAuth2Granters,
136
152
  initOAuth2ClientCredentialsGranter,
137
153
  initOAuth2AuthorizationCodeGranter,
@@ -146,4 +162,5 @@ export {
146
162
  initPostAuthRefresh,
147
163
  postAuthRefreshDefinition,
148
164
  initAuthCookiesService,
165
+ initOAuth2AuthorizationRequests,
149
166
  };
@@ -0,0 +1,40 @@
1
+ import { describe, test, expect } from '@jest/globals';
2
+ import {
3
+ buildRequestURI,
4
+ isRequestURI,
5
+ readRequestURI,
6
+ } from './authorizationRequests.js';
7
+
8
+ describe('buildRequestURI', () => {
9
+ test('should build URIs', () => {
10
+ expect(buildRequestURI('a_id')).toMatchInlineSnapshot(
11
+ `"urn:ietf:params:oauth:request_uri:a_id"`,
12
+ );
13
+ });
14
+ });
15
+
16
+ describe('isRequestURI', () => {
17
+ test('should cast URIs', () => {
18
+ expect(
19
+ isRequestURI('urn:ietf:params:oauth:request_uri:a_id'),
20
+ ).toMatchInlineSnapshot(`true`);
21
+ });
22
+
23
+ test('should fail with bad URIs', () => {
24
+ expect(() => isRequestURI('a_id')).toThrow('E_OAUTH2_BAD_REQUEST_URI');
25
+ });
26
+
27
+ test('should fail with empty id', () => {
28
+ expect(() => isRequestURI('urn:ietf:params:oauth:request_uri:')).toThrow(
29
+ 'E_OAUTH2_BAD_REQUEST_URI',
30
+ );
31
+ });
32
+ });
33
+
34
+ describe('read', () => {
35
+ test('should read URIs', () => {
36
+ expect(
37
+ readRequestURI('urn:ietf:params:oauth:request_uri:a_id'),
38
+ ).toMatchInlineSnapshot(`"a_id"`);
39
+ });
40
+ });
@@ -0,0 +1,42 @@
1
+ import { YError } from 'yerror';
2
+
3
+ export const PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX =
4
+ 'urn:ietf:params:oauth:request_uri:';
5
+
6
+ export type WhookOAuth2AuthorizationRequestURI =
7
+ `${typeof PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX}${string}`;
8
+ export type WhookOAuth2AuthorizationRequestId = string;
9
+
10
+ export function buildRequestURI(
11
+ requestId: WhookOAuth2AuthorizationRequestId,
12
+ ): WhookOAuth2AuthorizationRequestURI {
13
+ return `${PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX}${requestId}`;
14
+ }
15
+
16
+ export function isRequestURI(
17
+ requestURI: string | undefined,
18
+ ): requestURI is WhookOAuth2AuthorizationRequestURI {
19
+ if (
20
+ !requestURI ||
21
+ !requestURI.startsWith(PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX) ||
22
+ requestURI === PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX
23
+ ) {
24
+ throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
25
+ requestURI,
26
+ undefined,
27
+ undefined,
28
+ ]);
29
+ }
30
+
31
+ return true;
32
+ }
33
+
34
+ export function readRequestURI(
35
+ requestURI: WhookOAuth2AuthorizationRequestURI,
36
+ ): WhookOAuth2AuthorizationRequestId {
37
+ const requestId = requestURI.slice(
38
+ PUSHED_AUTHORIZATION_REQUEST_URI_PREFIX.length,
39
+ );
40
+
41
+ return requestId;
42
+ }
@@ -9,7 +9,9 @@ import { type WhookOAuth2ClientId } from '../services/oAuth2Granters.js';
9
9
  * OAuth2 errors registry
10
10
  */
11
11
  export interface OAuth2YErrorRegistry {
12
- E_OAUTH2_UNKNOWN_RESPONSE_TYPE: [responseType: string];
12
+ E_OAUTH2_MISCONFIGURED: [];
13
+
14
+ E_OAUTH2_UNKNOWN_RESPONSE_TYPE: [responseType: string | undefined];
13
15
 
14
16
  E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: [responseType: string];
15
17
 
@@ -50,6 +52,36 @@ export interface OAuth2YErrorRegistry {
50
52
  */
51
53
  E_OAUTH2_PKCE_NOT_SUPPORTED: [responseType: string];
52
54
 
55
+ /**
56
+ * Thrown when Pushed Authorization Requests are required
57
+ */
58
+ E_OAUTH2_PAR_REQUIRED: [];
59
+
60
+ /**
61
+ * Thrown when Pushed Authorization Requests are used but disabled
62
+ */
63
+ E_OAUTH2_PAR_NOT_SUPPORTED: [];
64
+
65
+ /**
66
+ * Thrown when a request URI has unexpected additional
67
+ * parameters
68
+ */
69
+ E_OAUTH2_BAD_REQUEST_URI_PARAMETERS: string[];
70
+
71
+ /**
72
+ * Thrown when a request URI is invalid, unknown or expired
73
+ */
74
+ E_OAUTH2_BAD_REQUEST_URI: [
75
+ requestURI: string | undefined,
76
+ expiresAt: number | undefined,
77
+ currentTime: number | undefined,
78
+ ];
79
+
80
+ /**
81
+ * Thrown when a request URI is provided where forbidden
82
+ */
83
+ E_OAUTH2_REQUEST_URI_NOT_ALLOWED: [];
84
+
53
85
  /**
54
86
  * Thrown when a not supported scope is asked
55
87
  */
@@ -117,6 +149,11 @@ export interface OAuth2YErrorRegistry {
117
149
  */
118
150
  E_OAUTH2_AUTHORIZATION_CODE_VERIFIER_REQUIRED: [];
119
151
 
152
+ /**
153
+ * Thrown when the client does not exist
154
+ */
155
+ E_OAUTH2_CLIENT_NOT_FOUND: [WhookOAuth2ClientId];
156
+
120
157
  /**
121
158
  * Thrown when the authenticated client does
122
159
  * not match the token/query/code clientId
@@ -151,6 +188,13 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
151
188
  uri: DEFAULT_ERROR_URI,
152
189
  help: DEFAULT_HELP_URI,
153
190
  },
191
+ E_OAUTH2_MISCONFIGURED: {
192
+ code: 'server_error',
193
+ status: 500,
194
+ description: `Server configuration error.`,
195
+ uri: DEFAULT_ERROR_URI,
196
+ help: DEFAULT_HELP_URI,
197
+ },
154
198
  E_OAUTH2_UNKNOWN_ACKNOWLEDGER_TYPE: {
155
199
  code: 'unsupported_response_type',
156
200
  status: 400,
@@ -220,6 +264,41 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
220
264
  uri: DEFAULT_ERROR_URI,
221
265
  help: DEFAULT_HELP_URI,
222
266
  },
267
+ E_OAUTH2_PAR_REQUIRED: {
268
+ code: 'invalid_request',
269
+ status: 400,
270
+ description: 'Pushed authorization requests are required for this server.',
271
+ uri: DEFAULT_ERROR_URI,
272
+ help: DEFAULT_HELP_URI,
273
+ },
274
+ E_OAUTH2_PAR_NOT_SUPPORTED: {
275
+ code: 'invalid_request',
276
+ status: 400,
277
+ description: 'Pushed authorization requests are not enabled for this server.',
278
+ uri: DEFAULT_ERROR_URI,
279
+ help: DEFAULT_HELP_URI,
280
+ },
281
+ E_OAUTH2_BAD_REQUEST_URI: {
282
+ code: 'invalid_request_uri',
283
+ status: 400,
284
+ description: 'The request URI is invalid, expired or unknown ($0).',
285
+ uri: DEFAULT_ERROR_URI,
286
+ help: DEFAULT_HELP_URI,
287
+ },
288
+ E_OAUTH2_BAD_REQUEST_URI_PARAMETERS: {
289
+ code: 'invalid_request',
290
+ status: 400,
291
+ description: 'The request URI should not have additional parameters ($0).',
292
+ uri: DEFAULT_ERROR_URI,
293
+ help: DEFAULT_HELP_URI,
294
+ },
295
+ E_OAUTH2_REQUEST_URI_NOT_ALLOWED: {
296
+ code: 'invalid_request',
297
+ status: 400,
298
+ description: 'The request_uri parameter is not allowed on this endpoint.',
299
+ uri: DEFAULT_ERROR_URI,
300
+ help: DEFAULT_HELP_URI,
301
+ },
223
302
  E_OAUTH2_BAD_SCOPE: {
224
303
  code: 'invalid_scope',
225
304
  status: 400,
@@ -301,6 +380,13 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
301
380
  uri: DEFAULT_ERROR_URI,
302
381
  help: DEFAULT_HELP_URI,
303
382
  },
383
+ E_OAUTH2_CLIENT_NOT_FOUND: {
384
+ code: 'invalid_client',
385
+ status: 400,
386
+ description: 'The client provided does not exist ($0).',
387
+ uri: DEFAULT_ERROR_URI,
388
+ help: DEFAULT_HELP_URI,
389
+ },
304
390
  E_OAUTH2_CLIENT_MISMATCH: {
305
391
  code: 'invalid_request',
306
392
  status: 400,
@@ -1,6 +1,28 @@
1
1
  import { type WhookErrorDescriptor } from '@whook/whook';
2
2
  import { YError } from 'yerror';
3
3
 
4
+ export function getUsableRedirectURI(
5
+ allowedURIS: string[],
6
+ requestedURI?: string,
7
+ httpsOnly = false,
8
+ ) {
9
+ if (requestedURI) {
10
+ checkRedirectURI(allowedURIS, requestedURI, httpsOnly);
11
+ }
12
+ requestedURI ||= allowedURIS[0];
13
+
14
+ try {
15
+ new URL(requestedURI);
16
+ } catch (err) {
17
+ throw YError.wrap(err as Error, 'E_OAUTH2_BAD_REDIRECT_URI', [
18
+ requestedURI,
19
+ allowedURIS,
20
+ ]);
21
+ }
22
+
23
+ return requestedURI;
24
+ }
25
+
4
26
  export function checkRedirectURI(
5
27
  allowedURIS: string[],
6
28
  requestedURI: string,
@@ -25,3 +25,13 @@ export const scopeSchema = {
25
25
  pattern: '^[!#-[\\]-~]+(?: [!#-[\\]-~]+)*$',
26
26
  },
27
27
  } as const satisfies WhookAPISchemaDefinition;
28
+
29
+ export const requestURISchema = {
30
+ name: 'RequestURI',
31
+ schema: {
32
+ description:
33
+ 'OAuth2 PAR request_uri, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.2.',
34
+ type: 'string',
35
+ pattern: '^urn:ietf:params:oauth:request_uri:',
36
+ },
37
+ } as const satisfies WhookAPISchemaDefinition;
@@ -1,6 +1,8 @@
1
1
  import { createHash } from 'crypto';
2
2
  import { YError } from 'yerror';
3
3
 
4
+ export const AUTHORIZATION_CODE_RESPONSE_TYPE = 'code';
5
+ export const AUTHORIZATION_CODE_GRANT_TYPE = 'authorization_code';
4
6
  export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
5
7
  export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
6
8
  export const S256_CODE_CHALLENGE_METHOD = 'S256';
@@ -19,6 +21,22 @@ export function base64UrlEncode(buf: Buffer): string {
19
21
  return s;
20
22
  }
21
23
 
24
+ export function checkCodeChallengeParameters(
25
+ responseType: string,
26
+ codeChallenge?: string,
27
+ forcePKCE?: boolean,
28
+ ) {
29
+ if (responseType === AUTHORIZATION_CODE_RESPONSE_TYPE) {
30
+ if (!codeChallenge) {
31
+ if (forcePKCE) {
32
+ throw new YError('E_OAUTH2_PKCE_REQUIRED', [responseType]);
33
+ }
34
+ }
35
+ } else if (codeChallenge) {
36
+ throw new YError('E_OAUTH2_PKCE_NOT_SUPPORTED', [responseType]);
37
+ }
38
+ }
39
+
22
40
  export function computeCodeChallenge(
23
41
  codeChallengeMethod: CodeChallengeMethod,
24
42
  codeVerifier: string,