@whook/oauth2 27.0.3 → 27.1.0

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 (121) hide show
  1. package/README.md +44 -25
  2. package/dist/index.d.ts +11 -7
  3. package/dist/index.js +9 -6
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +131 -15
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/clients.d.ts +17 -1
  8. package/dist/libs/clients.js +39 -6
  9. package/dist/libs/clients.js.map +1 -1
  10. package/dist/libs/clients.test.d.ts +1 -0
  11. package/dist/libs/clients.test.js +32 -0
  12. package/dist/libs/clients.test.js.map +1 -0
  13. package/dist/libs/errors.d.ts +24 -0
  14. package/dist/libs/errors.js +42 -0
  15. package/dist/libs/errors.js.map +1 -1
  16. package/dist/libs/verifier.d.ts +3 -3
  17. package/dist/libs/verifier.js +4 -3
  18. package/dist/libs/verifier.js.map +1 -1
  19. package/dist/routes/getOAuth2Authorize.d.ts +19 -6
  20. package/dist/routes/getOAuth2Authorize.js +29 -14
  21. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  22. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +3 -4
  23. package/dist/routes/getOAuth2WellKnownMetadata.js +19 -7
  24. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  25. package/dist/routes/getOAuth2WellKnownMetadata.test.js +81 -0
  26. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  27. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +0 -2
  28. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +1 -2
  29. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  30. package/dist/routes/postAuthLogin.d.ts +1 -1
  31. package/dist/routes/postAuthLogin.js +1 -1
  32. package/dist/routes/postAuthLogin.js.map +1 -1
  33. package/dist/routes/postAuthRefresh.d.ts +1 -1
  34. package/dist/routes/postAuthRefresh.js +1 -1
  35. package/dist/routes/postAuthRefresh.js.map +1 -1
  36. package/dist/routes/postOAuth2Acknowledge.d.ts +0 -3
  37. package/dist/routes/postOAuth2Acknowledge.js +1 -3
  38. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  39. package/dist/routes/postOAuth2DeviceAcknowledge.d.ts +64 -0
  40. package/dist/routes/postOAuth2DeviceAcknowledge.js +84 -0
  41. package/dist/routes/postOAuth2DeviceAcknowledge.js.map +1 -0
  42. package/dist/routes/postOAuth2DeviceAcknowledge.test.d.ts +1 -0
  43. package/dist/routes/postOAuth2DeviceAcknowledge.test.js +369 -0
  44. package/dist/routes/postOAuth2DeviceAcknowledge.test.js.map +1 -0
  45. package/dist/routes/postOAuth2DeviceAuthorization.d.ts +133 -0
  46. package/dist/routes/postOAuth2DeviceAuthorization.js +177 -0
  47. package/dist/routes/postOAuth2DeviceAuthorization.js.map +1 -0
  48. package/dist/routes/postOAuth2DeviceAuthorization.test.d.ts +1 -0
  49. package/dist/routes/postOAuth2DeviceAuthorization.test.js +174 -0
  50. package/dist/routes/postOAuth2DeviceAuthorization.test.js.map +1 -0
  51. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +1 -6
  52. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +11 -14
  53. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -1
  54. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +47 -0
  55. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -1
  56. package/dist/routes/postOAuth2Token.d.ts +2 -7
  57. package/dist/routes/postOAuth2Token.js +2 -6
  58. package/dist/routes/postOAuth2Token.js.map +1 -1
  59. package/dist/routes/postOAuth2Token.test.js +107 -0
  60. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  61. package/dist/routes/schemas.d.ts +178 -0
  62. package/dist/routes/schemas.js +188 -0
  63. package/dist/routes/schemas.js.map +1 -0
  64. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +0 -70
  65. package/dist/services/oAuth2AuthorizationCodeGranter.js +7 -84
  66. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
  67. package/dist/services/oAuth2AuthorizationRequests.js +4 -5
  68. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -1
  69. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +1 -16
  70. package/dist/services/oAuth2ClientCredentialsGranter.js +7 -25
  71. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  72. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.d.ts +1 -0
  73. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js +332 -0
  74. package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js.map +1 -0
  75. package/dist/services/oAuth2DeviceAuthorizationGranter.d.ts +54 -0
  76. package/dist/services/oAuth2DeviceAuthorizationGranter.js +67 -0
  77. package/dist/services/oAuth2DeviceAuthorizationGranter.js.map +1 -0
  78. package/dist/services/oAuth2Granters.js +0 -2
  79. package/dist/services/oAuth2Granters.js.map +1 -1
  80. package/dist/services/oAuth2PasswordGranter.d.ts +1 -22
  81. package/dist/services/oAuth2PasswordGranter.js +5 -30
  82. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  83. package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -19
  84. package/dist/services/oAuth2RefreshTokenGranter.js +5 -28
  85. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  86. package/package.json +2 -2
  87. package/src/index.test.ts +169 -26
  88. package/src/index.ts +21 -26
  89. package/src/libs/clients.test.ts +47 -0
  90. package/src/libs/clients.ts +60 -10
  91. package/src/libs/errors.ts +72 -0
  92. package/src/libs/verifier.ts +11 -3
  93. package/src/routes/__snapshots__/postOAuth2DeviceAcknowledge.test.ts.snap +5 -0
  94. package/src/routes/getOAuth2Authorize.ts +41 -26
  95. package/src/routes/getOAuth2WellKnownMetadata.test.ts +96 -0
  96. package/src/routes/getOAuth2WellKnownMetadata.ts +24 -10
  97. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +1 -3
  98. package/src/routes/postAuthLogin.ts +1 -1
  99. package/src/routes/postAuthRefresh.ts +1 -1
  100. package/src/routes/postOAuth2Acknowledge.ts +2 -4
  101. package/src/routes/postOAuth2DeviceAcknowledge.test.ts +408 -0
  102. package/src/routes/postOAuth2DeviceAcknowledge.ts +157 -0
  103. package/src/routes/postOAuth2DeviceAuthorization.test.ts +192 -0
  104. package/src/routes/postOAuth2DeviceAuthorization.ts +261 -0
  105. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +59 -0
  106. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +16 -22
  107. package/src/routes/postOAuth2Token.test.ts +119 -0
  108. package/src/routes/postOAuth2Token.ts +6 -16
  109. package/src/routes/schemas.ts +210 -0
  110. package/src/services/oAuth2AuthorizationCodeGranter.ts +11 -94
  111. package/src/services/oAuth2AuthorizationRequests.ts +5 -5
  112. package/src/services/oAuth2ClientCredentialsGranter.ts +8 -27
  113. package/src/services/oAuth2DeviceAuthorizationCodeGranter.test.ts +372 -0
  114. package/src/services/oAuth2DeviceAuthorizationGranter.ts +194 -0
  115. package/src/services/oAuth2Granters.ts +0 -2
  116. package/src/services/oAuth2PasswordGranter.ts +7 -33
  117. package/src/services/oAuth2RefreshTokenGranter.ts +6 -31
  118. package/dist/libs/schemas.d.ts +0 -31
  119. package/dist/libs/schemas.js +0 -33
  120. package/dist/libs/schemas.js.map +0 -1
  121. package/src/libs/schemas.ts +0 -37
package/src/index.ts CHANGED
@@ -6,30 +6,29 @@ import initGetOAuth2Authorize, {
6
6
  scopeParameter as getOAuth2AuthorizeScopeParameter,
7
7
  stateParameter as getOAuth2AuthorizeStateParameter,
8
8
  requestURIParameter as getOAuth2AuthorizeRequestURIParameter,
9
- codeChallengeSchema as getOAuth2AuthorizeCodeChallengeSchema,
10
9
  codeChallengeParameter as getOAuth2AuthorizeCodeChallengeParameter,
11
- codeChallengeMethodSchema as getOAuth2AuthorizeCodeChallengeMethodSchema,
12
10
  codeChallengeMethodParameter as getOAuth2AuthorizeCodeChallengeMethodParameter,
13
- requestURISchema as getOAuth2AuthorizeRequestURISchema,
14
- scopeSchema as getOAuth2AuthorizeScopeSchema,
15
11
  } from './routes/getOAuth2Authorize.js';
16
12
  import initPostOAuth2Acknowledge, {
17
13
  definition as postOAuth2AcknowledgeDefinition,
18
14
  } from './routes/postOAuth2Acknowledge.js';
19
15
  import initPostOAuth2Token, {
20
16
  definition as postOAuth2TokenDefinition,
21
- codeVerifierSchema as postOAuth2TokenCodeVerifierSchema,
22
- authorizationCodeTokenRequestBodySchema as postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
23
- passwordTokenRequestBodySchema as postOAuth2TokenPasswordTokenRequestBodySchema,
24
- clientCredentialsTokenRequestBodySchema as postOAuth2TokenClientCredentialsTokenRequestBodySchema,
25
- refreshTokenRequestBodySchema as postOAuth2TokenRefreshTokenRequestBodySchema,
26
17
  tokenBodySchema as postOAuth2TokenTokenBodySchema,
27
18
  } from './routes/postOAuth2Token.js';
28
19
  import initPostOAuth2PushedAuthorizationRequest, {
29
20
  definition as postOAuth2PushedAuthorizationRequestDefinition,
30
21
  pushedAuthorizationRequestBodySchema as postOAuth2PushedAuthorizationRequestBodySchema,
31
- requestURISchema as postOAuth2PushedAuthorizationRequestRequestURISchema,
32
22
  } from './routes/postOAuth2PushedAuthorizationRequest.js';
23
+ import initPostOAuth2DeviceAuthorization, {
24
+ definition as postOAuth2DeviceAuthorizationDefinition,
25
+ deviceAuthorizationRequestBodySchema as postOAuth2DeviceAuthorizationRequestBodySchema,
26
+ userCodeSchema as postOAuth2DeviceAuthorizationUserCodeSchema,
27
+ } from './routes/postOAuth2DeviceAuthorization.js';
28
+ import initPostOAuth2DeviceAcknowledge, {
29
+ definition as postOAuth2DeviceAcknowledgeDefinition,
30
+ deviceAcknowledgeRequestBodySchema as postOAuth2DeviceAcknowledgeRequestBodySchema,
31
+ } from './routes/postOAuth2DeviceAcknowledge.js';
33
32
  import initGetOAuth2WellKnown, {
34
33
  definition as getOAuth2WellKnownDefinition,
35
34
  endpointAuthenticationMethodsSchema as getOAuth2WellKnownEndpointAuthenticationMethodsSchema,
@@ -37,8 +36,6 @@ import initGetOAuth2WellKnown, {
37
36
  jsonWebEncryptionsSchema as getOAuth2WellKnownJsonWebEncryptionsSchema,
38
37
  httpsProtocolURISchema as getOAuth2WellKnownHTTPSProtocolURISchema,
39
38
  oAuth2MetadataSchema as getOAuth2WellKnownOAuth2MetadataSchema,
40
- scopeTokenSchema as getOAuth2WellKnownScopeTokenSchema,
41
- scopeTokensSchema as getOAuth2WellKnownScopeTokensSchema,
42
39
  } from './routes/getOAuth2WellKnownMetadata.js';
43
40
  import initGetOAuth2WellKnownProtectedResource, {
44
41
  definition as getOAuth2WellKnownProtectedResourceDefinition,
@@ -64,6 +61,8 @@ import initOAuth2ImplicitGranter from './services/oAuth2ImplicitGranter.js';
64
61
  export type * from './services/oAuth2ImplicitGranter.js';
65
62
  import initOAuth2AuthorizationCodeGranter from './services/oAuth2AuthorizationCodeGranter.js';
66
63
  export type * from './services/oAuth2AuthorizationCodeGranter.js';
64
+ import initOAuth2DeviceAuthorizationGranter from './services/oAuth2DeviceAuthorizationGranter.js';
65
+ export type * from './services/oAuth2DeviceAuthorizationGranter.js';
67
66
 
68
67
  import initPostAuthLogin, {
69
68
  definition as postAuthLoginDefinition,
@@ -83,8 +82,8 @@ import initOAuth2AuthorizationRequests from './services/oAuth2AuthorizationReque
83
82
  export * from './services/oAuth2AuthorizationRequests.js';
84
83
  export type * from './services/oAuth2AuthorizationRequests.js';
85
84
 
86
- export * from './libs/schemas.js';
87
- export type * from './libs/schemas.js';
85
+ export * from './routes/schemas.js';
86
+ export type * from './routes/schemas.js';
88
87
  export * from './libs/errors.js';
89
88
  export type * from './libs/errors.js';
90
89
  export * from './libs/scopes.js';
@@ -111,12 +110,8 @@ export {
111
110
  getOAuth2AuthorizeScopeParameter,
112
111
  getOAuth2AuthorizeStateParameter,
113
112
  getOAuth2AuthorizeRequestURIParameter,
114
- getOAuth2AuthorizeScopeSchema,
115
- getOAuth2AuthorizeCodeChallengeSchema,
116
- getOAuth2AuthorizeCodeChallengeMethodSchema,
117
113
  getOAuth2AuthorizeCodeChallengeParameter,
118
114
  getOAuth2AuthorizeCodeChallengeMethodParameter,
119
- getOAuth2AuthorizeRequestURISchema,
120
115
  initGetOAuth2WellKnown,
121
116
  getOAuth2WellKnownDefinition,
122
117
  getOAuth2WellKnownEndpointAuthenticationMethodsSchema,
@@ -124,8 +119,6 @@ export {
124
119
  getOAuth2WellKnownJsonWebEncryptionsSchema,
125
120
  getOAuth2WellKnownHTTPSProtocolURISchema,
126
121
  getOAuth2WellKnownOAuth2MetadataSchema,
127
- getOAuth2WellKnownScopeTokenSchema,
128
- getOAuth2WellKnownScopeTokensSchema,
129
122
  initGetOAuth2WellKnownProtectedResource,
130
123
  getOAuth2WellKnownProtectedResourceDefinition,
131
124
  getOAuth2WellKnownProtectedResourceBearerMethodSchema,
@@ -138,22 +131,24 @@ export {
138
131
  postOAuth2AcknowledgeDefinition,
139
132
  initPostOAuth2Token,
140
133
  postOAuth2TokenDefinition,
141
- postOAuth2TokenCodeVerifierSchema,
142
- postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
143
- postOAuth2TokenPasswordTokenRequestBodySchema,
144
- postOAuth2TokenClientCredentialsTokenRequestBodySchema,
145
- postOAuth2TokenRefreshTokenRequestBodySchema,
146
134
  postOAuth2TokenTokenBodySchema,
147
135
  initPostOAuth2PushedAuthorizationRequest,
148
136
  postOAuth2PushedAuthorizationRequestDefinition,
149
137
  postOAuth2PushedAuthorizationRequestBodySchema,
150
- postOAuth2PushedAuthorizationRequestRequestURISchema,
138
+ initPostOAuth2DeviceAcknowledge,
139
+ postOAuth2DeviceAcknowledgeDefinition,
140
+ postOAuth2DeviceAcknowledgeRequestBodySchema,
141
+ initPostOAuth2DeviceAuthorization,
142
+ postOAuth2DeviceAuthorizationDefinition,
143
+ postOAuth2DeviceAuthorizationRequestBodySchema,
144
+ postOAuth2DeviceAuthorizationUserCodeSchema,
151
145
  initOAuth2Granters,
152
146
  initOAuth2ClientCredentialsGranter,
153
147
  initOAuth2AuthorizationCodeGranter,
154
148
  initOAuth2PasswordGranter,
155
149
  initOAuth2RefreshTokenGranter,
156
150
  initOAuth2ImplicitGranter,
151
+ initOAuth2DeviceAuthorizationGranter,
157
152
  authCookieHeaderParameter,
158
153
  initPostAuthLogin,
159
154
  postAuthLoginDefinition,
@@ -0,0 +1,47 @@
1
+ import { describe, expect, test } from '@jest/globals';
2
+ import { checkClientsIds, toUsableClientId } from './clients.js';
3
+
4
+ describe('checkClientsIds', () => {
5
+ test('should accept exactly matching stored client IDs', () => {
6
+ expect(() => checkClientsIds('client', ['client', 'client'])).not.toThrow();
7
+ });
8
+ test('should accept undefined client IDs', () => {
9
+ expect(() =>
10
+ checkClientsIds('client', [undefined, 'client']),
11
+ ).not.toThrow();
12
+ });
13
+
14
+ test('should reject a stored client ID of "other-client"', () => {
15
+ expect(() => checkClientsIds('client', ['other-client'])).toThrow(
16
+ 'E_OAUTH2_CLIENT_MISMATCH',
17
+ );
18
+ });
19
+
20
+ test('should reject an empty stored client ID', () => {
21
+ expect(() => checkClientsIds('client', [''])).toThrow(
22
+ 'E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID',
23
+ );
24
+ });
25
+ });
26
+
27
+ describe('toUsableClientId', () => {
28
+ test.each([
29
+ [undefined, 'client'],
30
+ ['client', undefined],
31
+ ['client', 'client'],
32
+ ])('should resolve optional client IDs %p and %p', (firstId, secondId) => {
33
+ expect(toUsableClientId([firstId, secondId])).toBe('client');
34
+ });
35
+
36
+ test('should reject conflicting client IDs', () => {
37
+ expect(() =>
38
+ toUsableClientId([undefined, 'client', 'other-client']),
39
+ ).toThrow('E_OAUTH2_CLIENT_MISMATCH');
40
+ });
41
+
42
+ test('should reject missing client IDs', () => {
43
+ expect(() => toUsableClientId([undefined, undefined])).toThrow(
44
+ 'E_OAUTH2_EMPTY_CLIENT',
45
+ );
46
+ });
47
+ });
@@ -1,22 +1,72 @@
1
- import { identity } from '@whook/whook';
2
- import { type WhookOAuth2ClientId } from '../services/oAuth2Granters.js';
1
+ import {
2
+ type WhookOAuth2ClientGrants,
3
+ type WhookOAuth2ClientId,
4
+ } from '../services/oAuth2Granters.js';
3
5
  import { YError } from 'yerror';
4
6
 
7
+ /**
8
+ * Verifies that every defined client ids are the same.
9
+ * Some may be undefined since sometimes several sources
10
+ * can be used but ain't necessary defined. We just need
11
+ * to ensure every clients ids that could have been used
12
+ * are equal.
13
+ * @param usedClientId The usaed client id
14
+ * @param clientsIds All candidates ids from different sources
15
+ */
16
+ export function checkClientsIds(
17
+ usedClientId: WhookOAuth2ClientId,
18
+ clientsIds: (WhookOAuth2ClientId | undefined)[],
19
+ ) {
20
+ if (clientsIds.some((clientId) => clientId === '')) {
21
+ throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
22
+ }
23
+
24
+ if (
25
+ clientsIds.some(
26
+ (clientId) =>
27
+ typeof clientId !== 'undefined' && clientId !== usedClientId,
28
+ )
29
+ ) {
30
+ throw new YError('E_OAUTH2_CLIENT_MISMATCH', [
31
+ usedClientId,
32
+ ...clientsIds.filter((clientId) => typeof clientId !== 'undefined'),
33
+ ]);
34
+ }
35
+ }
36
+
37
+ export function checkGrants(
38
+ clientId: WhookOAuth2ClientId,
39
+ grants: WhookOAuth2ClientGrants,
40
+ ) {
41
+ if (clientId !== grants.authenticationData.clientId) {
42
+ throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
43
+ clientId,
44
+ grants.authenticationData.clientId,
45
+ ]);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Pick a usable client id from different sources
51
+ * @param clientsIds Every candidates clients ids
52
+ * @returns A usable client id
53
+ */
5
54
  export function toUsableClientId(
6
55
  clientsIds: (WhookOAuth2ClientId | undefined)[],
7
56
  ): WhookOAuth2ClientId {
8
- const usableClientId = clientsIds.find(identity);
57
+ if (clientsIds.some((clientId) => clientId === '')) {
58
+ throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
59
+ }
60
+
61
+ const usableClientId = clientsIds.find(
62
+ (clientId) => typeof clientId !== 'undefined',
63
+ );
9
64
 
10
- if (!usableClientId) {
65
+ if (typeof usableClientId === 'undefined') {
11
66
  throw new YError('E_OAUTH2_EMPTY_CLIENT', []);
12
67
  }
13
68
 
14
- if (clientsIds.some((clientId) => clientId && clientId !== usableClientId)) {
15
- throw new YError(
16
- 'E_OAUTH2_CLIENT_MISMATCH',
17
- clientsIds.filter((clientId) => typeof clientId !== 'undefined'),
18
- );
19
- }
69
+ checkClientsIds(usableClientId, clientsIds);
20
70
 
21
71
  return usableClientId;
22
72
  }
@@ -22,11 +22,31 @@ export interface OAuth2YErrorRegistry {
22
22
  */
23
23
  E_OAUTH2_BAD_REFRESH_TOKEN: [];
24
24
 
25
+ /**
26
+ * Thrown when the device code is expired/invalid
27
+ */
28
+ E_OAUTH2_BAD_DEVICE_CODE: [];
29
+
30
+ /**
31
+ * Thrown when the device code has not been authorized yet
32
+ */
33
+ E_OAUTH2_AUTHORIZATION_PENDING: [];
34
+
35
+ /**
36
+ * Thrown when the client is polling too often
37
+ */
38
+ E_OAUTH2_SLOW_DOWN: [];
39
+
25
40
  /**
26
41
  * Thrown when application has no access allowed
27
42
  */
28
43
  E_OAUTH2_ACCESS_DENIED: [clientId: WhookOAuth2ClientId];
29
44
 
45
+ /**
46
+ * Thrown when application has no device access allowed
47
+ */
48
+ E_OAUTH2_DEVICE_ACCESS_DENIED: [clientId: WhookOAuth2ClientId];
49
+
30
50
  /**
31
51
  * Thrown when application requires authentication
32
52
  */
@@ -100,6 +120,11 @@ export interface OAuth2YErrorRegistry {
100
120
  */
101
121
  E_OAUTH2_EXPIRED_CODE: [expiredAt: number, currentTime: number];
102
122
 
123
+ /**
124
+ * Thrown when the device code is expired
125
+ */
126
+ E_OAUTH2_EXPIRED_DEVICE_CODE: [expiredAt: number, currentTime: number];
127
+
103
128
  /**
104
129
  * Thrown when the authorization code is evicted
105
130
  */
@@ -154,6 +179,11 @@ export interface OAuth2YErrorRegistry {
154
179
  */
155
180
  E_OAUTH2_CLIENT_NOT_FOUND: [WhookOAuth2ClientId];
156
181
 
182
+ /**
183
+ * Thrown when a client is an empty string
184
+ */
185
+ E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID: [];
186
+
157
187
  /**
158
188
  * Thrown when the authenticated client does
159
189
  * not match the token/query/code clientId
@@ -216,6 +246,27 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
216
246
  uri: DEFAULT_ERROR_URI,
217
247
  help: DEFAULT_HELP_URI,
218
248
  },
249
+ E_OAUTH2_BAD_DEVICE_CODE: {
250
+ code: 'invalid_grant',
251
+ status: 400,
252
+ description: 'The device code is expired/invalid.',
253
+ uri: DEFAULT_ERROR_URI,
254
+ help: DEFAULT_HELP_URI,
255
+ },
256
+ E_OAUTH2_AUTHORIZATION_PENDING: {
257
+ code: 'authorization_pending',
258
+ status: 400,
259
+ description: 'The end-user has not completed the authorization yet.',
260
+ uri: DEFAULT_ERROR_URI,
261
+ help: DEFAULT_HELP_URI,
262
+ },
263
+ E_OAUTH2_SLOW_DOWN: {
264
+ code: 'slow_down',
265
+ status: 400,
266
+ description: 'The token request was sent too often.',
267
+ uri: DEFAULT_ERROR_URI,
268
+ help: DEFAULT_HELP_URI,
269
+ },
219
270
  E_OAUTH2_ACCESS_DENIED: {
220
271
  code: 'access_denied',
221
272
  status: 403,
@@ -223,6 +274,13 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
223
274
  uri: DEFAULT_ERROR_URI,
224
275
  help: DEFAULT_HELP_URI,
225
276
  },
277
+ E_OAUTH2_DEVICE_ACCESS_DENIED: {
278
+ code: 'access_denied',
279
+ status: 400,
280
+ description: 'The user denied access to your application (id: "$0").',
281
+ uri: DEFAULT_ERROR_URI,
282
+ help: DEFAULT_HELP_URI,
283
+ },
226
284
  E_OAUTH2_EMPTY_CLIENT: {
227
285
  code: 'invalid_client',
228
286
  status: 400,
@@ -321,6 +379,13 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
321
379
  uri: DEFAULT_ERROR_URI,
322
380
  help: DEFAULT_HELP_URI,
323
381
  },
382
+ E_OAUTH2_EXPIRED_DEVICE_CODE: {
383
+ code: 'expired_token',
384
+ status: 400,
385
+ description: 'The device code is expired (expired at "$0").',
386
+ uri: DEFAULT_ERROR_URI,
387
+ help: DEFAULT_HELP_URI,
388
+ },
324
389
  E_OAUTH2_EVICTED_CODE: {
325
390
  code: 'invalid_request',
326
391
  status: 400,
@@ -395,6 +460,13 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
395
460
  uri: DEFAULT_ERROR_URI,
396
461
  help: DEFAULT_HELP_URI,
397
462
  },
463
+ E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID: {
464
+ code: 'server_error',
465
+ status: 500,
466
+ description: 'Implementation error, empty client id encountered.',
467
+ uri: DEFAULT_ERROR_URI,
468
+ help: DEFAULT_HELP_URI,
469
+ },
398
470
  E_OAUTH2_CLIENT_GRANTS_MISMATCH: {
399
471
  code: 'server_error',
400
472
  status: 500,
@@ -1,11 +1,19 @@
1
1
  import { createHash } from 'crypto';
2
2
  import { YError } from 'yerror';
3
+ import {
4
+ authorizationCodeTokenRequestBodySchema,
5
+ plainCodeChallengeMethodSchema,
6
+ s256CodeChallengeMethodSchema,
7
+ } from '../routes/schemas.js';
3
8
 
4
9
  export const AUTHORIZATION_CODE_RESPONSE_TYPE = 'code';
5
- export const AUTHORIZATION_CODE_GRANT_TYPE = 'authorization_code';
10
+ export const AUTHORIZATION_CODE_GRANT_TYPE =
11
+ authorizationCodeTokenRequestBodySchema.schema.properties.grant_type.const;
6
12
  export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
7
- export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
8
- export const S256_CODE_CHALLENGE_METHOD = 'S256';
13
+ export const PLAIN_CODE_CHALLENGE_METHOD =
14
+ plainCodeChallengeMethodSchema.schema.const;
15
+ export const S256_CODE_CHALLENGE_METHOD =
16
+ s256CodeChallengeMethodSchema.schema.const;
9
17
 
10
18
  export const CODE_CHALLENGE_METHODS = [
11
19
  PLAIN_CODE_CHALLENGE_METHOD,
@@ -0,0 +1,5 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`postOAuth2DeviceAcknowledge should propagate a stored challenge client mismatch when acknowledged is false 1`] = `[YError: E_UNAUTHORIZED ([]): E_UNAUTHORIZED]`;
4
+
5
+ exports[`postOAuth2DeviceAcknowledge should propagate a stored challenge client mismatch when acknowledged is true 1`] = `[YError: E_UNAUTHORIZED ([]): E_UNAUTHORIZED]`;
@@ -19,40 +19,41 @@ import {
19
19
  stringifyScopes,
20
20
  } from '../libs/scopes.js';
21
21
  import {
22
+ AUTHORIZATION_CODE_RESPONSE_TYPE,
22
23
  checkCodeChallengeParameters,
23
24
  type CodeChallengeMethod,
24
25
  PLAIN_CODE_CHALLENGE_METHOD,
25
26
  } from '../libs/verifier.js';
26
- import {
27
- AUTHORIZATION_CODE_RESPONSE_TYPE,
28
- codeChallengeMethodSchema,
29
- codeChallengeMethodParameter,
30
- codeChallengeSchema,
31
- codeChallengeParameter,
32
- } from '../services/oAuth2AuthorizationCodeGranter.js';
33
27
  import { IMPLICIT_RESPONSE_TYPE } from '../services/oAuth2ImplicitGranter.js';
34
28
  import {
35
29
  addParamsToURL,
36
30
  buildParamsFromError,
37
31
  getUsableRedirectURI,
38
32
  } from '../libs/redirectURI.js';
39
- import { requestURISchema, scopeSchema } from '../libs/schemas.js';
33
+ import {
34
+ codeChallengeMethodSchema,
35
+ codeChallengeSchema,
36
+ requestURISchema,
37
+ scopeSchema,
38
+ } from './schemas.js';
40
39
  import {
41
40
  DEFAULT_OAUTH2_PAR,
42
41
  type WhookOAuth2AuthorizationRequestsConfig,
43
42
  type WhookOAuth2AuthorizationRequestsService,
44
43
  } from '../services/oAuth2AuthorizationRequests.js';
45
44
  import { isRequestURI } from '../libs/authorizationRequests.js';
46
- import { toUsableClientId } from '../libs/clients.js';
45
+ import { checkGrants, toUsableClientId } from '../libs/clients.js';
46
+ import { clientIdSchema } from './schemas.js';
47
47
 
48
- export {
49
- requestURISchema,
50
- scopeSchema,
51
- codeChallengeMethodSchema,
52
- codeChallengeMethodParameter,
53
- codeChallengeSchema,
54
- codeChallengeParameter,
55
- };
48
+ export const codeChallengeMethodParameter = {
49
+ name: 'code_challenge_method',
50
+ parameter: {
51
+ in: 'query',
52
+ name: 'code_challenge_method',
53
+ required: false,
54
+ schema: refersTo(codeChallengeMethodSchema),
55
+ },
56
+ } as const satisfies WhookAPIParameterDefinition;
56
57
 
57
58
  export interface WhookOAuth2AuthorizeRequestParameters {
58
59
  client_id: string;
@@ -84,17 +85,17 @@ export const responseTypeParameter = {
84
85
  },
85
86
  },
86
87
  } as const satisfies WhookAPIParameterDefinition;
88
+
87
89
  export const clientIdParameter = {
88
90
  name: 'clientId',
89
91
  parameter: {
90
92
  in: 'query',
91
93
  name: 'client_id',
92
94
  required: true,
93
- schema: {
94
- type: 'string',
95
- },
95
+ schema: refersTo(clientIdSchema),
96
96
  },
97
97
  } as const satisfies WhookAPIParameterDefinition;
98
+
98
99
  export const redirectURIParameter = {
99
100
  name: 'redirectURI',
100
101
  parameter: {
@@ -108,6 +109,7 @@ export const redirectURIParameter = {
108
109
  },
109
110
  },
110
111
  } as const satisfies WhookAPIParameterDefinition;
112
+
111
113
  export const scopeParameter = {
112
114
  name: 'scope',
113
115
  parameter: {
@@ -117,6 +119,7 @@ export const scopeParameter = {
117
119
  schema: refersTo(scopeSchema),
118
120
  },
119
121
  } as const satisfies WhookAPIParameterDefinition;
122
+
120
123
  export const stateParameter = {
121
124
  name: 'state',
122
125
  parameter: {
@@ -128,6 +131,7 @@ export const stateParameter = {
128
131
  },
129
132
  },
130
133
  } as const satisfies WhookAPIParameterDefinition;
134
+
131
135
  export const requestURIParameter = {
132
136
  name: 'requestURI',
133
137
  parameter: {
@@ -138,6 +142,16 @@ export const requestURIParameter = {
138
142
  },
139
143
  } as const satisfies WhookAPIParameterDefinition;
140
144
 
145
+ export const codeChallengeParameter = {
146
+ name: 'code_challenge',
147
+ parameter: {
148
+ in: 'query',
149
+ name: 'code_challenge',
150
+ required: false,
151
+ schema: refersTo(codeChallengeSchema),
152
+ },
153
+ } as const satisfies WhookAPIParameterDefinition;
154
+
141
155
  export const definition = {
142
156
  method: 'get',
143
157
  path: '/oauth2/authorize',
@@ -207,12 +221,7 @@ async function initGetOAuth2Authorize({
207
221
  try {
208
222
  const clientGrants = await readClientGrants(givenClientId);
209
223
 
210
- if (givenClientId !== clientGrants.authenticationData.clientId) {
211
- throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
212
- givenClientId,
213
- clientGrants.authenticationData.clientId,
214
- ]);
215
- }
224
+ checkGrants(givenClientId, clientGrants);
216
225
 
217
226
  // Default to client URI to redirect errors
218
227
  // before the request URI is decoded
@@ -314,6 +323,12 @@ async function initGetOAuth2Authorize({
314
323
  !!OAUTH2.strictScopesChecks,
315
324
  );
316
325
 
326
+ // When scopes are demanded but filtered to nothing
327
+ // let's error in order to avoid hard to debug situations
328
+ if (demandedScope && demandedScopes.length === 0) {
329
+ throw new YError('E_OAUTH2_BAD_SCOPE', [demandedScope]);
330
+ }
331
+
317
332
  const { scopes } = await granter.authorize({
318
333
  clientId: givenClientId,
319
334
  clientGrants,
@@ -10,6 +10,7 @@ import {
10
10
  type WhookOAuth2GranterService,
11
11
  } from '../index.js';
12
12
  import { type WhookDefinitions } from '@whook/whook';
13
+ import { type OpenAPI } from 'ya-open-api-types';
13
14
 
14
15
  describe('getOAuth2WellKnown', () => {
15
16
  const log = jest.fn<LogService>();
@@ -55,6 +56,89 @@ describe('getOAuth2WellKnown', () => {
55
56
  ].forEach((mock) => mock.mockReset());
56
57
  });
57
58
 
59
+ test.each<{
60
+ description: string;
61
+ security?: OpenAPI['security'];
62
+ operationSecurity?: OpenAPI['security'];
63
+ missingOperation?: boolean;
64
+ allowsNone: boolean;
65
+ }>([
66
+ {
67
+ description: 'Basic-only token operation',
68
+ operationSecurity: [{ basicAuth: ['oauth'] }],
69
+ allowsNone: false,
70
+ },
71
+ {
72
+ description: 'anonymous security alternative',
73
+ operationSecurity: [{ basicAuth: ['oauth'] }, {}],
74
+ allowsNone: true,
75
+ },
76
+ {
77
+ description: 'explicitly public token operation',
78
+ operationSecurity: [],
79
+ allowsNone: true,
80
+ },
81
+ {
82
+ description: 'token operation without security',
83
+ allowsNone: true,
84
+ },
85
+ {
86
+ description: 'inherited authentication requirement',
87
+ security: [{ basicAuth: ['oauth'] }],
88
+ allowsNone: false,
89
+ },
90
+ {
91
+ description: 'public operation overriding inherited authentication',
92
+ security: [{ basicAuth: ['oauth'] }],
93
+ operationSecurity: [],
94
+ allowsNone: true,
95
+ },
96
+ {
97
+ description: 'required operation overriding inherited anonymous access',
98
+ security: [{}],
99
+ operationSecurity: [{ basicAuth: ['oauth'] }],
100
+ allowsNone: false,
101
+ },
102
+ {
103
+ description: 'missing token operation',
104
+ missingOperation: true,
105
+ allowsNone: false,
106
+ },
107
+ ])(
108
+ 'should derive anonymous token authentication for $description',
109
+ async ({ security, operationSecurity, missingOperation, allowsNone }) => {
110
+ const getOAuth2WellKnown = await initGetOAuth2WellKnown({
111
+ BASE_URL: 'https://server.example.com',
112
+ OAUTH2_PAR: { mode: 'disabled' },
113
+ DEFINITIONS: {
114
+ configs: {
115
+ postOAuth2Token: { type: 'route', path: '/v0/oauth2/token' },
116
+ },
117
+ } as unknown as WhookDefinitions,
118
+ API: {
119
+ openapi: '3.2',
120
+ info: { title: 'Test', version: '1.0.0' },
121
+ security,
122
+ paths: missingOperation
123
+ ? {}
124
+ : {
125
+ '/v0/oauth2/token': {
126
+ post: { security: operationSecurity, responses: {} },
127
+ },
128
+ },
129
+ },
130
+ oAuth2Granters,
131
+ log,
132
+ });
133
+
134
+ const { body } = await getOAuth2WellKnown();
135
+
136
+ expect(body.token_endpoint_auth_methods_supported.includes('none')).toBe(
137
+ allowsNone,
138
+ );
139
+ },
140
+ );
141
+
58
142
  test('should return OAuth2 metadata', async () => {
59
143
  [
60
144
  codeGranter.authorize,
@@ -89,6 +173,10 @@ describe('getOAuth2WellKnown', () => {
89
173
  type: 'route',
90
174
  path: '/v0/oauth2/par',
91
175
  },
176
+ postOAuth2DeviceAuthorization: {
177
+ type: 'route',
178
+ path: '/oauth2/device_authorization',
179
+ },
92
180
  },
93
181
  } as unknown as WhookDefinitions,
94
182
  API: {
@@ -98,6 +186,12 @@ describe('getOAuth2WellKnown', () => {
98
186
  version: '1.0.0',
99
187
  },
100
188
  paths: {
189
+ '/v0/oauth2/token': {
190
+ post: {
191
+ security: [],
192
+ responses: {},
193
+ },
194
+ },
101
195
  '/test': {
102
196
  get: {
103
197
  security: [
@@ -134,6 +228,7 @@ describe('getOAuth2WellKnown', () => {
134
228
  "response": {
135
229
  "body": {
136
230
  "authorization_endpoint": "https://server.example.com/v0/oauth2/authorize",
231
+ "device_authorization_endpoint": "https://server.example.com/oauth2/device_authorization",
137
232
  "grant_types_supported": [
138
233
  "authorization_code",
139
234
  "implicit",
@@ -158,6 +253,7 @@ describe('getOAuth2WellKnown', () => {
158
253
  "token_endpoint_auth_methods_supported": [
159
254
  "client_secret_basic",
160
255
  "client_secret_post",
256
+ "none",
161
257
  ],
162
258
  },
163
259
  "status": 200,