@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
@@ -14,6 +14,7 @@ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAu
14
14
  import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
15
15
  import { type WhookOAuth2PasswordGranterService } from '../services/oAuth2PasswordGranter.js';
16
16
  import { type WhookOAuth2RefreshTokenGranterService } from '../services/oAuth2RefreshTokenGranter.js';
17
+ import { type WhookOAuth2DeviceAuthorizationGranterService } from '../services/oAuth2DeviceAuthorizationGranter.js';
17
18
  import { type WhookAuthenticationData } from '@whook/authorization';
18
19
 
19
20
  describe('postOAuth2Token', () => {
@@ -80,11 +81,22 @@ describe('postOAuth2Token', () => {
80
81
  NonNullable<WhookOAuth2RefreshTokenGranterService['authenticate']>
81
82
  >(),
82
83
  } satisfies WhookOAuth2RefreshTokenGranterService;
84
+ const deviceCodeGranter = {
85
+ grantType: 'urn:ietf:params:oauth:grant-type:device_code',
86
+ issuesRefreshToken: true,
87
+ authenticate:
88
+ jest.fn<
89
+ NonNullable<
90
+ WhookOAuth2DeviceAuthorizationGranterService['authenticate']
91
+ >
92
+ >(),
93
+ } satisfies WhookOAuth2DeviceAuthorizationGranterService;
83
94
  const oAuth2Granters = [
84
95
  codeGranter,
85
96
  tokenGranter,
86
97
  passwordGranter,
87
98
  refreshTokenGranter,
99
+ deviceCodeGranter,
88
100
  clientCredentialsGranter,
89
101
  ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
90
102
 
@@ -102,6 +114,7 @@ describe('postOAuth2Token', () => {
102
114
  tokenGranter.acknowledge.mockReset();
103
115
  passwordGranter.authenticate.mockReset();
104
116
  refreshTokenGranter.authenticate.mockReset();
117
+ deviceCodeGranter.authenticate.mockReset();
105
118
  clientCredentialsGranter.authenticate.mockReset();
106
119
  });
107
120
 
@@ -485,10 +498,12 @@ describe('postOAuth2Token', () => {
485
498
  token: 'an_access_token',
486
499
  expiresAt: Date.parse('2010-03-07T00:00:00Z'),
487
500
  });
501
+
488
502
  oAuth2RefreshToken.create.mockResolvedValueOnce({
489
503
  token: 'a_refresh_token',
490
504
  expiresAt: Date.parse('2180-03-06T00:00:00Z'),
491
505
  });
506
+
492
507
  passwordGranter.authenticate.mockResolvedValueOnce({
493
508
  clientId: 'authenticate_app_id',
494
509
  userId: 'authenticate_user_id',
@@ -597,6 +612,110 @@ describe('postOAuth2Token', () => {
597
612
  `);
598
613
  });
599
614
 
615
+ test('should create a token with the device authorization flow', async () => {
616
+ time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
617
+ oAuth2AccessToken.create.mockResolvedValueOnce({
618
+ token: 'an_access_token',
619
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
620
+ });
621
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
622
+ token: 'a_refresh_token',
623
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
624
+ });
625
+ deviceCodeGranter.authenticate.mockResolvedValueOnce({
626
+ clientId: 'authenticate_app_id',
627
+ userId: 'authenticate_user_id',
628
+ scopes: ['user', 'admin'],
629
+ });
630
+
631
+ const postOAuth2Token = await initPostOAuth2Token({
632
+ OAUTH2,
633
+ oAuth2Granters,
634
+ oAuth2AccessToken,
635
+ oAuth2RefreshToken,
636
+ time,
637
+ log,
638
+ });
639
+ const response = await postOAuth2Token({
640
+ authenticationData: {
641
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
642
+ scopes: ['user'],
643
+ } as WhookAuthenticationData,
644
+ body: {
645
+ grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
646
+ device_code: 'a_device_code',
647
+ },
648
+ });
649
+
650
+ expect({
651
+ response,
652
+ deviceCodeGranterAuthenticatorAuthenticateCalls:
653
+ deviceCodeGranter.authenticate.mock.calls,
654
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
655
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
656
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
657
+ }).toMatchInlineSnapshot(`
658
+ {
659
+ "deviceCodeGranterAuthenticatorAuthenticateCalls": [
660
+ [
661
+ {
662
+ "demandedScopes": [],
663
+ "deviceCode": "a_device_code",
664
+ },
665
+ {
666
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
667
+ "scopes": [
668
+ "user",
669
+ ],
670
+ },
671
+ ],
672
+ ],
673
+ "logCalls": [],
674
+ "oAuth2AccessTokenCreateCalls": [
675
+ [
676
+ {
677
+ "clientId": "authenticate_app_id",
678
+ "scopes": [
679
+ "user",
680
+ "admin",
681
+ ],
682
+ "userId": "authenticate_user_id",
683
+ },
684
+ ],
685
+ ],
686
+ "oAuth2RefreshTokenCreateCalls": [
687
+ [
688
+ {
689
+ "clientId": "authenticate_app_id",
690
+ "scopes": [
691
+ "user",
692
+ "admin",
693
+ ],
694
+ "userId": "authenticate_user_id",
695
+ },
696
+ ],
697
+ ],
698
+ "response": {
699
+ "body": {
700
+ "access_token": "an_access_token",
701
+ "expiration_date": "2010-03-07T00:00:00.000Z",
702
+ "expires_in": 86400,
703
+ "refresh_token": "a_refresh_token",
704
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
705
+ "refresh_token_expires_in": 5364748800,
706
+ "scope": "user admin",
707
+ "token_type": "bearer",
708
+ },
709
+ "headers": {
710
+ "Cache-Control": "no-store",
711
+ "Pragma": "no-cache",
712
+ },
713
+ "status": 200,
714
+ },
715
+ }
716
+ `);
717
+ });
718
+
600
719
  test('should fail with a bad grant type', async () => {
601
720
  const postOAuth2Token = await initPostOAuth2Token({
602
721
  OAUTH2,
@@ -19,27 +19,16 @@ import {
19
19
  stringifyScopes,
20
20
  } from '../libs/scopes.js';
21
21
  import { type WhookOAuth2AccessTokenService } from '../services/oAuth2ImplicitGranter.js';
22
- import {
23
- refreshTokenRequestBodySchema,
24
- type WhookOAuth2RefreshTokenService,
25
- } from '../services/oAuth2RefreshTokenGranter.js';
22
+ import { type WhookOAuth2RefreshTokenService } from '../services/oAuth2RefreshTokenGranter.js';
26
23
  import {
27
24
  authorizationCodeTokenRequestBodySchema,
28
- codeVerifierSchema,
29
- } from '../services/oAuth2AuthorizationCodeGranter.js';
30
- import { passwordTokenRequestBodySchema } from '../services/oAuth2PasswordGranter.js';
31
- import { clientCredentialsTokenRequestBodySchema } from '../services/oAuth2ClientCredentialsGranter.js';
32
- import { scopeSchema } from '../libs/schemas.js';
33
- import { camelCaseObjectProperties } from '../libs/utils.js';
34
-
35
- export {
25
+ clientCredentialsTokenRequestBodySchema,
26
+ deviceAuthorizationTokenRequestBodySchema,
36
27
  passwordTokenRequestBodySchema,
37
28
  refreshTokenRequestBodySchema,
38
- clientCredentialsTokenRequestBodySchema,
39
- authorizationCodeTokenRequestBodySchema,
40
- codeVerifierSchema,
41
29
  scopeSchema,
42
- };
30
+ } from './schemas.js';
31
+ import { camelCaseObjectProperties } from '../libs/utils.js';
43
32
 
44
33
  /* Architecture Note #2: OAuth2 acknowledge
45
34
  This endpoint is to be used by the authentication server page
@@ -54,6 +43,7 @@ export const tokenBodySchema = {
54
43
  refersTo(authorizationCodeTokenRequestBodySchema),
55
44
  refersTo(clientCredentialsTokenRequestBodySchema),
56
45
  refersTo(refreshTokenRequestBodySchema),
46
+ refersTo(deviceAuthorizationTokenRequestBodySchema),
57
47
  ],
58
48
  },
59
49
  } as const satisfies WhookAPISchemaDefinition;
@@ -0,0 +1,210 @@
1
+ import { refersTo, type WhookAPISchemaDefinition } from '@whook/whook';
2
+
3
+ export const clientIdSchema = {
4
+ name: 'clientId',
5
+ schema: {
6
+ type: 'string',
7
+ minLength: 1,
8
+ },
9
+ } as const satisfies WhookAPISchemaDefinition;
10
+
11
+ export const scopeTokenSchema = {
12
+ name: 'ScopeToken',
13
+ schema: {
14
+ description:
15
+ 'OAuth 2.1 scope token, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
16
+ type: 'string',
17
+ pattern: '^[!#-[\\]-~]+$',
18
+ },
19
+ } as const satisfies WhookAPISchemaDefinition;
20
+ export const scopeTokensSchema = {
21
+ name: 'ScopeTokens',
22
+ schema: {
23
+ type: 'array',
24
+ items: refersTo(scopeTokenSchema),
25
+ },
26
+ } as const satisfies WhookAPISchemaDefinition;
27
+ export const scopeSchema = {
28
+ name: 'Scope',
29
+ schema: {
30
+ description:
31
+ 'OAuth 2.1 scope, see https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#name-access-token-scope',
32
+ type: 'string',
33
+ pattern: '^[!#-[\\]-~]+(?: [!#-[\\]-~]+)*$',
34
+ },
35
+ } as const satisfies WhookAPISchemaDefinition;
36
+
37
+ export const requestURISchema = {
38
+ name: 'RequestURI',
39
+ schema: {
40
+ description:
41
+ 'OAuth2 PAR request_uri, see https://datatracker.ietf.org/doc/html/rfc9126#section-2.2.',
42
+ type: 'string',
43
+ pattern: '^urn:ietf:params:oauth:request_uri:',
44
+ },
45
+ } as const satisfies WhookAPISchemaDefinition;
46
+
47
+ export const codeVerifierSchema = {
48
+ name: 'CodeVerifier',
49
+ schema: {
50
+ type: 'string',
51
+ minLength: 43,
52
+ maxLength: 128,
53
+ pattern: '^[A-Za-z0-9._~-]+$',
54
+ },
55
+ } as const satisfies WhookAPISchemaDefinition;
56
+
57
+ export const authorizationCodeSchema = {
58
+ name: 'AuthorizationCode',
59
+ schema: {
60
+ type: 'string',
61
+ minLength: 1,
62
+ },
63
+ } as WhookAPISchemaDefinition;
64
+
65
+ export const codeChallengeSchema = {
66
+ name: 'CodeChallenge',
67
+ schema: {
68
+ type: 'string',
69
+ minLength: 43,
70
+ maxLength: 128,
71
+ pattern: '^[A-Za-z0-9\\-._~]{43,128}$',
72
+ },
73
+ } as const satisfies WhookAPISchemaDefinition;
74
+
75
+ export const authorizationCodeTokenRequestBodySchema = {
76
+ name: 'AuthorizationCodeRequestBody',
77
+ schema: {
78
+ description:
79
+ 'Implements the [Token Endpoint](https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#section-3.2) as defined per the OAuth2.1 RFC.',
80
+ type: 'object',
81
+ required: ['grant_type', 'code'],
82
+ properties: {
83
+ grant_type: {
84
+ type: 'string',
85
+ const: 'authorization_code',
86
+ },
87
+ code: refersTo(authorizationCodeSchema),
88
+ client_id: refersTo(clientIdSchema),
89
+ redirect_uri: {
90
+ type: 'string',
91
+ pattern: '^https?://',
92
+ format: 'uri',
93
+ },
94
+ code_verifier: refersTo(codeVerifierSchema),
95
+ },
96
+ },
97
+ } as const satisfies WhookAPISchemaDefinition;
98
+
99
+ export const plainCodeChallengeMethodSchema = {
100
+ name: 'PlainCodeChallengeMethod',
101
+ schema: {
102
+ type: 'string',
103
+ const: 'plain',
104
+ },
105
+ } as const satisfies WhookAPISchemaDefinition;
106
+
107
+ export const s256CodeChallengeMethodSchema = {
108
+ name: 'S256CodeChallengeMethod',
109
+ schema: {
110
+ type: 'string',
111
+ const: 'S256',
112
+ },
113
+ } as const satisfies WhookAPISchemaDefinition;
114
+
115
+ export const codeChallengeMethodSchema = {
116
+ name: 'CodeChallengeMethod',
117
+ schema: {
118
+ type: 'string',
119
+ oneOf: [
120
+ refersTo(plainCodeChallengeMethodSchema),
121
+ refersTo(s256CodeChallengeMethodSchema),
122
+ ],
123
+ },
124
+ } as const satisfies WhookAPISchemaDefinition;
125
+
126
+ export const refreshTokenRequestBodySchema = {
127
+ name: 'RefreshTokenRequestBody',
128
+ schema: {
129
+ type: 'object',
130
+ description:
131
+ 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
132
+ required: ['grant_type', 'refresh_token'],
133
+ properties: {
134
+ grant_type: {
135
+ type: 'string',
136
+ const: 'refresh_token',
137
+ },
138
+ refresh_token: {
139
+ type: 'string',
140
+ },
141
+ scope: refersTo(scopeSchema),
142
+ },
143
+ },
144
+ } as const satisfies WhookAPISchemaDefinition;
145
+
146
+ export const deviceCodeSchema = {
147
+ name: 'OAuth2DeviceCode',
148
+ schema: {
149
+ type: 'string',
150
+ minLength: 1,
151
+ },
152
+ } as const satisfies WhookAPISchemaDefinition;
153
+
154
+ export const deviceAuthorizationTokenRequestBodySchema = {
155
+ name: 'DeviceAuthorizationTokenRequestBody',
156
+ schema: {
157
+ type: 'object',
158
+ description:
159
+ 'Device authorization grant poll request, see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4',
160
+ required: ['grant_type', 'device_code'],
161
+ properties: {
162
+ grant_type: {
163
+ type: 'string',
164
+ const: 'urn:ietf:params:oauth:grant-type:device_code',
165
+ },
166
+ device_code: refersTo(deviceCodeSchema),
167
+ client_id: refersTo(clientIdSchema),
168
+ },
169
+ },
170
+ } as const satisfies WhookAPISchemaDefinition;
171
+
172
+ export const passwordTokenRequestBodySchema = {
173
+ name: 'PasswordRequestBody',
174
+ schema: {
175
+ type: 'object',
176
+ description:
177
+ 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
178
+ required: ['grant_type', 'username', 'password'],
179
+ properties: {
180
+ grant_type: {
181
+ type: 'string',
182
+ const: 'password',
183
+ },
184
+ username: {
185
+ type: 'string',
186
+ },
187
+ password: {
188
+ type: 'string',
189
+ },
190
+ scope: refersTo(scopeSchema),
191
+ },
192
+ },
193
+ } as const satisfies WhookAPISchemaDefinition;
194
+
195
+ export const clientCredentialsTokenRequestBodySchema = {
196
+ name: 'ClientCredentialsRequestBody',
197
+ schema: {
198
+ type: 'object',
199
+ description:
200
+ 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
201
+ required: ['grant_type'],
202
+ properties: {
203
+ grant_type: {
204
+ type: 'string',
205
+ const: 'client_credentials',
206
+ },
207
+ scope: refersTo(scopeSchema),
208
+ },
209
+ },
210
+ } as const satisfies WhookAPISchemaDefinition;
@@ -1,10 +1,5 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import {
3
- noop,
4
- refersTo,
5
- type WhookAPIParameterDefinition,
6
- type WhookAPISchemaDefinition,
7
- } from '@whook/whook';
2
+ import { noop } from '@whook/whook';
8
3
  import { YError } from 'yerror';
9
4
  import { type LogService } from 'common-services';
10
5
  import {
@@ -25,86 +20,14 @@ import {
25
20
  checkCodeChallenge,
26
21
  AUTHORIZATION_CODE_GRANT_TYPE,
27
22
  AUTHORIZATION_CODE_RESPONSE_TYPE,
28
- CODE_CHALLENGE_METHODS,
29
23
  PLAIN_CODE_CHALLENGE_METHOD,
30
24
  type CodeChallengeMethod,
31
25
  } from '../libs/verifier.js';
32
- import { toUsableClientId } from '../libs/clients.js';
33
-
34
- export { AUTHORIZATION_CODE_GRANT_TYPE, AUTHORIZATION_CODE_RESPONSE_TYPE };
35
-
36
- export const codeVerifierSchema = {
37
- name: 'CodeVerifier',
38
- schema: {
39
- type: 'string',
40
- minLength: 43,
41
- maxLength: 128,
42
- pattern: '^[A-Za-z0-9._~-]+$',
43
- },
44
- } as const satisfies WhookAPISchemaDefinition;
45
-
46
- export const authorizationCodeTokenRequestBodySchema = {
47
- name: 'AuthorizationCodeRequestBody',
48
- schema: {
49
- description:
50
- 'Implements the [Token Endpoint](https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-15.html#section-3.2) as defined per the OAuth2.1 RFC.',
51
- type: 'object',
52
- required: ['grant_type', 'code'],
53
- properties: {
54
- grant_type: {
55
- type: 'string',
56
- const: AUTHORIZATION_CODE_GRANT_TYPE,
57
- },
58
- code: {
59
- type: 'string',
60
- },
61
- client_id: {
62
- type: 'string',
63
- },
64
- redirect_uri: {
65
- type: 'string',
66
- pattern: '^https?://',
67
- format: 'uri',
68
- },
69
- code_verifier: refersTo(codeVerifierSchema),
70
- },
71
- },
72
- } as const satisfies WhookAPISchemaDefinition;
73
-
74
- export const codeChallengeSchema = {
75
- name: 'CodeChallenge',
76
- schema: {
77
- type: 'string',
78
- minLength: 43,
79
- maxLength: 128,
80
- pattern: '^[A-Za-z0-9\\-._~]{43,128}$',
81
- },
82
- } as const satisfies WhookAPISchemaDefinition;
83
- export const codeChallengeParameter = {
84
- name: 'code_challenge',
85
- parameter: {
86
- in: 'query',
87
- name: 'code_challenge',
88
- required: false,
89
- schema: refersTo(codeChallengeSchema),
90
- },
91
- } as const satisfies WhookAPIParameterDefinition;
92
- export const codeChallengeMethodSchema = {
93
- name: 'CodeChallengeMethod',
94
- schema: {
95
- type: 'string',
96
- enum: CODE_CHALLENGE_METHODS.concat(),
97
- },
98
- } as const satisfies WhookAPISchemaDefinition;
99
- export const codeChallengeMethodParameter = {
100
- name: 'code_challenge_method',
101
- parameter: {
102
- in: 'query',
103
- name: 'code_challenge_method',
104
- required: false,
105
- schema: refersTo(codeChallengeMethodSchema),
106
- },
107
- } as const satisfies WhookAPIParameterDefinition;
26
+ import {
27
+ checkClientsIds,
28
+ checkGrants,
29
+ toUsableClientId,
30
+ } from '../libs/clients.js';
108
31
 
109
32
  /**
110
33
  * Utility code for providing your own code type
@@ -268,12 +191,7 @@ async function initOAuth2AuthorizationCodeGranter({
268
191
  ]);
269
192
  const grants = await readClientGrants(usableClientId);
270
193
 
271
- if (usableClientId !== grants.authenticationData.clientId) {
272
- throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
273
- usableClientId,
274
- grants.authenticationData.clientId,
275
- ]);
276
- }
194
+ checkGrants(usableClientId, grants);
277
195
 
278
196
  if (!grants.isPublicClient) {
279
197
  if (!optionalAuthenticationData) {
@@ -304,13 +222,12 @@ async function initOAuth2AuthorizationCodeGranter({
304
222
  ]);
305
223
  }
306
224
 
307
- if (usableClientId !== codeAuthenticationData.clientId) {
308
- throw new YError('E_OAUTH2_CLIENT_MISMATCH', [
309
- usableClientId,
310
- codeAuthenticationData.clientId,
311
- ]);
225
+ if (!codeAuthenticationData.clientId) {
226
+ throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
312
227
  }
313
228
 
229
+ checkClientsIds(usableClientId, [codeAuthenticationData.clientId]);
230
+
314
231
  if (codeVerifier) {
315
232
  if (!context.codeChallenge || !context.codeChallengeMethod) {
316
233
  throw new YError(
@@ -14,6 +14,7 @@ import {
14
14
  type WhookOAuth2AuthorizationRequestURI,
15
15
  } from '../libs/authorizationRequests.js';
16
16
  import { type WhookOAuth2AuthorizeRequestParameters } from '../routes/getOAuth2Authorize.js';
17
+ import { checkClientsIds } from '../libs/clients.js';
17
18
 
18
19
  export interface WhookOAuth2AuthorizationRequestData {
19
20
  clientId: WhookOAuth2ClientId;
@@ -121,13 +122,12 @@ async function initOAuth2AuthorizationRequests({
121
122
  ]);
122
123
  }
123
124
 
124
- if (clientId !== request.clientId) {
125
- throw new YError('E_OAUTH2_CLIENT_MISMATCH', [
126
- clientId,
127
- request.clientId,
128
- ]);
125
+ if (!request.clientId) {
126
+ throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
129
127
  }
130
128
 
129
+ checkClientsIds(clientId, [request.clientId]);
130
+
131
131
  return request;
132
132
  },
133
133
  };
@@ -1,5 +1,5 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import { noop, refersTo, type WhookAPISchemaDefinition } from '@whook/whook';
2
+ import { noop } from '@whook/whook';
3
3
  import { YError } from 'yerror';
4
4
  import { type LogService } from 'common-services';
5
5
  import {
@@ -11,26 +11,11 @@ import {
11
11
  import { type WhookAuthenticationScope } from '@whook/authorization';
12
12
  import { checkGrantType } from '../libs/grants.js';
13
13
  import { filterScopes } from '../libs/scopes.js';
14
- import { scopeSchema } from '../libs/schemas.js';
14
+ import { checkGrants } from '../libs/clients.js';
15
+ import { clientCredentialsTokenRequestBodySchema } from '../routes/schemas.js';
15
16
 
16
- export const CLIENT_CREDENTIALS_GRANT_TYPE = 'client_credentials';
17
-
18
- export const clientCredentialsTokenRequestBodySchema = {
19
- name: 'ClientCredentialsRequestBody',
20
- schema: {
21
- type: 'object',
22
- description:
23
- 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
24
- required: ['grant_type'],
25
- properties: {
26
- grant_type: {
27
- type: 'string',
28
- const: CLIENT_CREDENTIALS_GRANT_TYPE,
29
- },
30
- scope: refersTo(scopeSchema),
31
- },
32
- },
33
- } as const satisfies WhookAPISchemaDefinition;
17
+ export const CLIENT_CREDENTIALS_GRANT_TYPE =
18
+ clientCredentialsTokenRequestBodySchema.schema.properties.grant_type.const;
34
19
 
35
20
  export interface WhookOAuth2ClientCredentialsGranterDependencies {
36
21
  OAUTH2: WhookOAuth2Options;
@@ -67,14 +52,10 @@ async function initOAuth2ClientCredentialsGranter({
67
52
  throw new YError('E_UNAUTHORIZED');
68
53
  }
69
54
 
70
- const grants = await readClientGrants(authenticationData.clientId);
55
+ const usableClientId = authenticationData.clientId;
56
+ const grants = await readClientGrants(usableClientId);
71
57
 
72
- if (authenticationData.clientId !== grants.authenticationData.clientId) {
73
- throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
74
- authenticationData.clientId,
75
- grants.authenticationData.clientId,
76
- ]);
77
- }
58
+ checkGrants(usableClientId, grants);
78
59
 
79
60
  checkGrantType(grants.allowedGrantTypes, CLIENT_CREDENTIALS_GRANT_TYPE);
80
61