@whook/oauth2 25.2.1 → 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 (197) hide show
  1. package/README.md +147 -98
  2. package/dist/index.d.ts +34 -11
  3. package/dist/index.js +18 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1671 -393
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/authorizationRequests.d.ts +6 -0
  8. package/dist/libs/authorizationRequests.js +22 -0
  9. package/dist/libs/authorizationRequests.js.map +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/clients.d.ts +2 -0
  13. package/dist/libs/clients.js +13 -0
  14. package/dist/libs/clients.js.map +1 -0
  15. package/dist/libs/errors.d.ts +143 -0
  16. package/dist/libs/errors.js +227 -0
  17. package/dist/libs/errors.js.map +1 -0
  18. package/dist/libs/grants.d.ts +2 -0
  19. package/dist/libs/grants.js +10 -0
  20. package/dist/libs/grants.js.map +1 -0
  21. package/dist/libs/redirectURI.d.ts +8 -0
  22. package/dist/libs/redirectURI.js +94 -0
  23. package/dist/libs/redirectURI.js.map +1 -0
  24. package/dist/libs/schemas.d.ts +31 -0
  25. package/dist/libs/schemas.js +33 -0
  26. package/dist/libs/schemas.js.map +1 -0
  27. package/dist/libs/scopes.d.ts +16 -0
  28. package/dist/libs/scopes.js +37 -0
  29. package/dist/libs/scopes.js.map +1 -1
  30. package/dist/libs/utils.d.ts +2 -0
  31. package/dist/libs/utils.js +17 -0
  32. package/dist/libs/utils.js.map +1 -0
  33. package/dist/libs/verifier.d.ts +10 -0
  34. package/dist/libs/verifier.js +51 -0
  35. package/dist/libs/verifier.js.map +1 -0
  36. package/dist/libs/verifier.test.d.ts +1 -0
  37. package/dist/libs/verifier.test.js +21 -0
  38. package/dist/libs/verifier.test.js.map +1 -0
  39. package/dist/routes/getOAuth2Authorize.d.ts +38 -59
  40. package/dist/routes/getOAuth2Authorize.js +129 -93
  41. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  42. package/dist/routes/getOAuth2Authorize.test.js +959 -73
  43. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  44. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
  45. package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
  46. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  47. package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
  48. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  49. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  50. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  51. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  52. package/dist/routes/postAuthLogin.d.ts +14 -9
  53. package/dist/routes/postAuthLogin.js +10 -5
  54. package/dist/routes/postAuthLogin.js.map +1 -1
  55. package/dist/routes/postAuthLogin.test.js +79 -55
  56. package/dist/routes/postAuthLogin.test.js.map +1 -1
  57. package/dist/routes/postAuthLogout.d.ts +5 -2
  58. package/dist/routes/postAuthLogout.js +12 -7
  59. package/dist/routes/postAuthLogout.js.map +1 -1
  60. package/dist/routes/postAuthLogout.test.js +35 -17
  61. package/dist/routes/postAuthLogout.test.js.map +1 -1
  62. package/dist/routes/postAuthRefresh.d.ts +14 -9
  63. package/dist/routes/postAuthRefresh.js +10 -6
  64. package/dist/routes/postAuthRefresh.js.map +1 -1
  65. package/dist/routes/postAuthRefresh.test.js +90 -59
  66. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  67. package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
  68. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  69. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  70. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  71. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  72. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  73. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  74. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  75. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  76. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  77. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  78. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  79. package/dist/routes/postOAuth2Revoke.js +102 -0
  80. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  81. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  82. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  83. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  84. package/dist/routes/postOAuth2Token.d.ts +47 -108
  85. package/dist/routes/postOAuth2Token.js +65 -114
  86. package/dist/routes/postOAuth2Token.js.map +1 -1
  87. package/dist/routes/postOAuth2Token.test.js +493 -61
  88. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  89. package/dist/services/authCookies.d.ts +9 -13
  90. package/dist/services/authCookies.js +9 -5
  91. package/dist/services/authCookies.js.map +1 -1
  92. package/dist/services/authCookies.test.js.map +1 -1
  93. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
  94. package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
  95. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  96. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  97. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
  98. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  99. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  100. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  101. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  102. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  103. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  104. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  105. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  106. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  107. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  108. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  109. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  110. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  111. package/dist/services/oAuth2Granters.d.ts +79 -86
  112. package/dist/services/oAuth2Granters.js +2 -67
  113. package/dist/services/oAuth2Granters.js.map +1 -1
  114. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  115. package/dist/services/oAuth2ImplicitGranter.js +61 -0
  116. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  117. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  118. package/dist/services/oAuth2ImplicitGranter.test.js +589 -0
  119. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  120. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  121. package/dist/services/oAuth2PasswordGranter.js +53 -18
  122. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  123. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  124. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  125. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  126. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  127. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  128. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  129. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  130. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  131. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  132. package/package.json +14 -13
  133. package/src/index.test.ts +1773 -407
  134. package/src/index.ts +63 -53
  135. package/src/libs/authorizationRequests.test.ts +40 -0
  136. package/src/libs/authorizationRequests.ts +42 -0
  137. package/src/libs/clients.ts +22 -0
  138. package/src/libs/errors.ts +411 -0
  139. package/src/libs/grants.ts +15 -0
  140. package/src/libs/redirectURI.ts +142 -0
  141. package/src/libs/schemas.ts +37 -0
  142. package/src/libs/scopes.ts +49 -0
  143. package/src/libs/utils.ts +24 -0
  144. package/src/libs/verifier.test.ts +37 -0
  145. package/src/libs/verifier.ts +75 -0
  146. package/src/routes/getOAuth2Authorize.test.ts +1102 -102
  147. package/src/routes/getOAuth2Authorize.ts +243 -151
  148. package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
  149. package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
  150. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  151. package/src/routes/postAuthLogin.test.ts +86 -57
  152. package/src/routes/postAuthLogin.ts +24 -14
  153. package/src/routes/postAuthLogout.test.ts +42 -18
  154. package/src/routes/postAuthLogout.ts +22 -8
  155. package/src/routes/postAuthRefresh.test.ts +98 -61
  156. package/src/routes/postAuthRefresh.ts +25 -13
  157. package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
  158. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  159. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  160. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  161. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  162. package/src/routes/postOAuth2Revoke.ts +156 -0
  163. package/src/routes/postOAuth2Token.test.ts +570 -108
  164. package/src/routes/postOAuth2Token.ts +104 -136
  165. package/src/services/authCookies.test.ts +5 -5
  166. package/src/services/authCookies.ts +24 -22
  167. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
  168. package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
  169. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  170. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  171. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  172. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  173. package/src/services/oAuth2Granters.ts +100 -196
  174. package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
  175. package/src/services/oAuth2ImplicitGranter.ts +147 -0
  176. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  177. package/src/services/oAuth2PasswordGranter.ts +116 -40
  178. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  179. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  180. package/src/types.d.ts +1 -0
  181. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  182. package/dist/services/oAuth2CodeGranter.js +0 -105
  183. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  184. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  185. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  186. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  187. package/dist/services/oAuth2TokenGranter.js +0 -64
  188. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  189. package/src/__snapshots__/index.test.ts.snap +0 -401
  190. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  191. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  192. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  193. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  194. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  195. package/src/services/oAuth2CodeGranter.ts +0 -210
  196. package/src/services/oAuth2TokenGranter.ts +0 -119
  197. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/authorizationRequests.test.d.ts} +0 -0
@@ -1,5 +1,4 @@
1
1
  import { autoService, location } from 'knifecycle';
2
- import { camelCaseObjectProperties } from './getOAuth2Authorize.js';
3
2
  import { YError, printStackTrace } from 'yerror';
4
3
  import { type LogService, type TimeService } from 'common-services';
5
4
  import {
@@ -9,122 +8,43 @@ import {
9
8
  type WhookRouteDefinition,
10
9
  } from '@whook/whook';
11
10
  import {
12
- type OAuth2GranterService,
13
- type OAuth2RefreshTokenService,
14
- type OAuth2AccessTokenService,
11
+ type WhookOAuth2GranterDefinitions,
12
+ type WhookOAuth2GranterService,
13
+ type WhookOAuth2Options,
15
14
  } from '../services/oAuth2Granters.js';
16
15
  import { type WhookAuthenticationData } from '@whook/authorization';
16
+ import {
17
+ filterScopes,
18
+ parseOAuth2Scope,
19
+ stringifyScopes,
20
+ } from '../libs/scopes.js';
21
+ import { type WhookOAuth2AccessTokenService } from '../services/oAuth2ImplicitGranter.js';
22
+ import {
23
+ refreshTokenRequestBodySchema,
24
+ type WhookOAuth2RefreshTokenService,
25
+ } from '../services/oAuth2RefreshTokenGranter.js';
26
+ import {
27
+ 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 {
36
+ passwordTokenRequestBodySchema,
37
+ refreshTokenRequestBodySchema,
38
+ clientCredentialsTokenRequestBodySchema,
39
+ authorizationCodeTokenRequestBodySchema,
40
+ codeVerifierSchema,
41
+ scopeSchema,
42
+ };
17
43
 
18
44
  /* Architecture Note #2: OAuth2 acknowledge
19
45
  This endpoint is to be used by the authentication server page
20
- to acknowlege that the user accepted the client request.
46
+ to acknowledge that the user accepted the client request.
21
47
  */
22
- export const codeVerifierSchema = {
23
- name: 'CodeVerifier',
24
- schema: {
25
- type: 'string',
26
- minLength: 43,
27
- maxLength: 128,
28
- pattern: '^[A-Za-z0-9._~-]+$',
29
- },
30
- } as const satisfies WhookAPISchemaDefinition;
31
-
32
- export const authorizationCodeTokenRequestBodySchema = {
33
- name: 'AuthorizationCodeRequestBody',
34
- schema: {
35
- type: 'object',
36
- description:
37
- 'Authorization code grant, see https://tools.ietf.org/html/rfc6749#section-4.1',
38
- required: ['grant_type'],
39
- properties: {
40
- grant_type: {
41
- type: 'string',
42
- enum: ['authorization_code'],
43
- },
44
- code: {
45
- type: 'string',
46
- },
47
- client_id: {
48
- type: 'string',
49
- },
50
- redirect_uri: {
51
- type: 'string',
52
- pattern: '^https?://',
53
- format: 'uri',
54
- },
55
- code_verifier: refersTo(codeVerifierSchema),
56
- },
57
- },
58
- } as const satisfies WhookAPISchemaDefinition;
59
-
60
- export const passwordTokenRequestBodySchema = {
61
- name: 'PasswordRequestBody',
62
- schema: {
63
- type: 'object',
64
- description:
65
- 'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
66
- required: ['grant_type', 'username', 'password'],
67
- properties: {
68
- grant_type: {
69
- type: 'string',
70
- enum: ['password'],
71
- },
72
- username: {
73
- type: 'string',
74
- },
75
- password: {
76
- type: 'string',
77
- },
78
- scope: {
79
- type: 'string',
80
- description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
81
- },
82
- },
83
- },
84
- } as const satisfies WhookAPISchemaDefinition;
85
-
86
- export const clientCredentialsTokenRequestBodySchema = {
87
- name: 'ClientCredentialsRequestBody',
88
- schema: {
89
- type: 'object',
90
- description:
91
- 'Client credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.4',
92
- required: ['grant_type'],
93
- properties: {
94
- grant_type: {
95
- type: 'string',
96
- enum: ['client_credentials'],
97
- },
98
- scope: {
99
- type: 'string',
100
- description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
101
- },
102
- },
103
- },
104
- } as const satisfies WhookAPISchemaDefinition;
105
-
106
- export const refreshTokenRequestBodySchema = {
107
- name: 'RefreshTokenRequestBody',
108
- schema: {
109
- type: 'object',
110
- description:
111
- 'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
112
- required: ['grant_type', 'refresh_token'],
113
- properties: {
114
- grant_type: {
115
- type: 'string',
116
- enum: ['refresh_token'],
117
- },
118
- refresh_token: {
119
- type: 'string',
120
- },
121
- scope: {
122
- type: 'string',
123
- description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
124
- },
125
- },
126
- },
127
- } as const satisfies WhookAPISchemaDefinition;
128
48
 
129
49
  export const tokenBodySchema = {
130
50
  name: 'TokenRequestBody',
@@ -162,6 +82,18 @@ export const definition = {
162
82
  '200': {
163
83
  description:
164
84
  'Token successfully issued, see https://tools.ietf.org/html/rfc6749#section-5 .',
85
+ headers: {
86
+ 'cache-control': {
87
+ schema: {
88
+ type: 'string',
89
+ },
90
+ },
91
+ pragma: {
92
+ schema: {
93
+ type: 'string',
94
+ },
95
+ },
96
+ },
165
97
  content: {
166
98
  'application/json': {
167
99
  schema: {
@@ -179,11 +111,24 @@ export const definition = {
179
111
  description: 'The lifetime in seconds of the access token',
180
112
  type: 'number',
181
113
  },
114
+ expiration_date: {
115
+ type: 'string',
116
+ format: 'date-time',
117
+ },
182
118
  refresh_token: {
183
119
  description:
184
120
  'See https://tools.ietf.org/html/rfc6749#section-6',
185
121
  type: 'string',
186
122
  },
123
+ refresh_token_expires_in: {
124
+ description: 'The lifetime in seconds of the refresh token',
125
+ type: 'number',
126
+ },
127
+ refresh_token_expiration_date: {
128
+ type: 'string',
129
+ format: 'date-time',
130
+ },
131
+ scope: refersTo(scopeSchema),
187
132
  },
188
133
  },
189
134
  },
@@ -221,75 +166,98 @@ export const definition = {
221
166
  } as const satisfies WhookRouteDefinition;
222
167
 
223
168
  async function initPostOAuth2Token({
169
+ OAUTH2,
224
170
  oAuth2Granters,
225
171
  oAuth2AccessToken,
226
172
  oAuth2RefreshToken,
227
173
  time = Date.now.bind(Date),
228
174
  log = noop,
229
175
  }: {
230
- oAuth2Granters: OAuth2GranterService[];
231
- oAuth2AccessToken: OAuth2AccessTokenService;
232
- oAuth2RefreshToken: OAuth2RefreshTokenService;
176
+ OAUTH2: WhookOAuth2Options;
177
+ oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
178
+ oAuth2AccessToken: WhookOAuth2AccessTokenService;
179
+ oAuth2RefreshToken: WhookOAuth2RefreshTokenService;
233
180
  log: LogService;
234
181
  time: TimeService;
235
182
  }) {
236
183
  return async ({
237
- body: { grant_type: grantType, ...grantParameters },
238
- authenticationData,
184
+ body: { grant_type: grantType, scope: demandedScope, ...grantParameters },
185
+ authenticationData: optionalAuthenticationData,
239
186
  }: {
240
187
  body: {
241
188
  grant_type: string;
242
189
  [name: string]: unknown;
243
190
  };
244
- authenticationData: WhookAuthenticationData;
191
+ authenticationData?: WhookAuthenticationData;
245
192
  }) => {
246
193
  try {
247
194
  const granter = oAuth2Granters.find(
248
- (granter) =>
249
- granter.authenticator &&
250
- granter.authenticator.grantType === grantType,
195
+ (granter) => granter.grantType && granter.grantType === grantType,
251
196
  );
252
197
 
253
- if (!granter || !granter.authenticator) {
254
- throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', [grantType]);
198
+ if (!granter || !granter.authenticate) {
199
+ throw new YError('E_OAUTH2_UNKNOWN_GRANT_TYPE', [grantType]);
255
200
  }
256
201
 
257
- const newAuthenticationData = await granter.authenticator.authenticate(
258
- camelCaseObjectProperties(grantParameters),
259
- authenticationData,
202
+ const newAuthenticationData = await granter.authenticate(
203
+ {
204
+ ...camelCaseObjectProperties(grantParameters),
205
+ demandedScopes:
206
+ typeof demandedScope === 'string'
207
+ ? filterScopes(
208
+ parseOAuth2Scope(demandedScope),
209
+ OAUTH2.allowedScopes,
210
+ !!OAUTH2.strictScopesChecks,
211
+ )
212
+ : [],
213
+ },
214
+ optionalAuthenticationData,
260
215
  );
261
216
 
262
217
  const [
263
218
  { token: accessToken, expiresAt: accessTokenExpiresAt },
264
219
  { token: refreshToken, expiresAt: refreshTokenExpiresAt },
265
220
  ] = await Promise.all([
266
- oAuth2AccessToken.create(authenticationData, newAuthenticationData),
267
- oAuth2RefreshToken.create(authenticationData, newAuthenticationData),
221
+ oAuth2AccessToken.create(newAuthenticationData),
222
+ granter.issuesRefreshToken
223
+ ? oAuth2RefreshToken.create(newAuthenticationData)
224
+ : Promise.resolve({
225
+ token: undefined,
226
+ expiresAt: undefined,
227
+ }),
268
228
  ]);
269
229
  const currentTime = time();
270
230
 
271
231
  return {
272
232
  status: 200,
273
- headers: {},
233
+ headers: {
234
+ 'Cache-Control': 'no-store',
235
+ Pragma: 'no-cache',
236
+ },
274
237
  body: {
275
238
  access_token: accessToken,
276
239
  token_type: 'bearer',
277
240
  expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),
278
241
  expiration_date: new Date(accessTokenExpiresAt).toISOString(),
279
- refresh_token: refreshToken,
280
- refresh_token_expires_in: Math.ceil(
281
- (refreshTokenExpiresAt - currentTime) / 1000,
282
- ),
283
- refresh_token_expiration_date: new Date(
284
- refreshTokenExpiresAt,
285
- ).toISOString(),
242
+ ...(refreshToken
243
+ ? {
244
+ refresh_token: refreshToken,
245
+ refresh_token_expires_in: Math.ceil(
246
+ (refreshTokenExpiresAt - currentTime) / 1000,
247
+ ),
248
+ refresh_token_expiration_date: new Date(
249
+ refreshTokenExpiresAt,
250
+ ).toISOString(),
251
+ }
252
+ : {}),
253
+ scope: stringifyScopes(newAuthenticationData.scopes),
286
254
  },
287
255
  };
288
256
  } catch (err) {
289
257
  log('debug', '👫 - OAuth2 token issuing error', (err as YError).code);
290
258
  log('error-stack', printStackTrace(err));
291
259
 
292
- throw YError.cast(err as Error, 'E_OAUTH2');
260
+ throw YError.cast(err as Error, 'E_OAUTH2_UNEXPECTED_ERROR');
293
261
  }
294
262
  };
295
263
  }
@@ -1,11 +1,11 @@
1
1
  import { describe, test, expect } from '@jest/globals';
2
2
  import initAuthCookies from './authCookies.js';
3
- import { type AuthCookiesOptions } from './authCookies.js';
3
+ import { type WhookAuthCookiesOptions } from './authCookies.js';
4
4
 
5
5
  describe('authCookies', () => {
6
6
  describe('.build()', () => {
7
7
  test('should work with new auth data', async () => {
8
- const COOKIES: AuthCookiesOptions = {
8
+ const COOKIES: WhookAuthCookiesOptions = {
9
9
  domain: 'api.example.com',
10
10
  };
11
11
 
@@ -27,7 +27,7 @@ describe('authCookies', () => {
27
27
  });
28
28
 
29
29
  test('should allow to reset auth data', async () => {
30
- const COOKIES: AuthCookiesOptions = {
30
+ const COOKIES: WhookAuthCookiesOptions = {
31
31
  domain: 'api.example.com',
32
32
  };
33
33
 
@@ -51,7 +51,7 @@ describe('authCookies', () => {
51
51
 
52
52
  describe('.parse()', () => {
53
53
  test('should work with no cookies', async () => {
54
- const COOKIES: AuthCookiesOptions = {
54
+ const COOKIES: WhookAuthCookiesOptions = {
55
55
  domain: 'api.example.com',
56
56
  };
57
57
 
@@ -65,7 +65,7 @@ describe('authCookies', () => {
65
65
  });
66
66
 
67
67
  test('should work with cookies', async () => {
68
- const COOKIES: AuthCookiesOptions = {
68
+ const COOKIES: WhookAuthCookiesOptions = {
69
69
  domain: 'api.example.com',
70
70
  };
71
71
 
@@ -1,49 +1,47 @@
1
1
  import ms from 'ms';
2
- import cookie, { type SerializeOptions } from 'cookie';
2
+ import { stringifySetCookie, parseCookie, type SerializeOptions } from 'cookie';
3
3
  import { autoService, location } from 'knifecycle';
4
- import { type WhookAuthenticationData } from '@whook/authorization';
5
4
  import { type Jsonify } from 'type-fest';
6
5
  import { type WhookRouteDefinitionBasePath } from '@whook/whook';
7
6
 
8
7
  export const AUTH_API_PREFIX = '/auth';
9
8
 
10
- export interface AuthHandlersConfig {
11
- ROOT_AUTHENTICATION_DATA: WhookAuthenticationData;
12
- }
13
-
14
- export type AuthCookiesOptions = Jsonify<
9
+ export type WhookAuthCookiesOptions = Jsonify<
15
10
  Omit<SerializeOptions, 'maxAge' | 'path' | 'expires'>
16
11
  >;
17
12
 
18
- export interface AuthCookiesConfig {
19
- COOKIES: AuthCookiesOptions;
13
+ export interface WhookAuthCookiesConfig {
14
+ COOKIES: WhookAuthCookiesOptions;
20
15
  BASE_PATH?: WhookRouteDefinitionBasePath;
21
16
  }
22
17
 
23
- export type AuthCookiesDependencies = AuthCookiesConfig;
18
+ export type AuthCookiesDependencies = WhookAuthCookiesConfig;
24
19
 
25
- export interface AuthCookiesData {
20
+ export interface WhookAuthCookiesData {
26
21
  refresh_token: string;
27
22
  access_token: string;
28
23
  }
29
24
 
30
- export interface AuthCookiesService {
25
+ export interface WhookAuthCookiesService {
31
26
  build: (
32
- data?: Partial<AuthCookiesData>,
27
+ data?: Partial<WhookAuthCookiesData>,
33
28
  options?: { session: boolean },
34
29
  ) => string[];
35
- parse: (content: string) => Partial<AuthCookiesData>;
30
+ parse: (content: string) => Partial<WhookAuthCookiesData>;
36
31
  }
37
32
 
38
- export default location(autoService(initAuthCookies), import.meta.url);
39
-
40
33
  async function initAuthCookies({
41
34
  COOKIES,
42
35
  BASE_PATH = '',
43
- }: AuthCookiesDependencies): Promise<AuthCookiesService> {
44
- function build(data: Partial<AuthCookiesData> = {}, { session = true } = {}) {
36
+ }: AuthCookiesDependencies): Promise<WhookAuthCookiesService> {
37
+ function build(
38
+ data: Partial<WhookAuthCookiesData> = {},
39
+ { session = true } = {},
40
+ ) {
45
41
  return [
46
- cookie.serialize('access_token', data.access_token || '', {
42
+ stringifySetCookie({
43
+ name: 'access_token',
44
+ value: data.access_token || '',
47
45
  path: BASE_PATH + AUTH_API_PREFIX,
48
46
  httpOnly: true,
49
47
  sameSite: true,
@@ -51,7 +49,9 @@ async function initAuthCookies({
51
49
  ...COOKIES,
52
50
  ...(data.access_token ? {} : { maxAge: 0 }),
53
51
  }),
54
- cookie.serialize('refresh_token', data.refresh_token || '', {
52
+ stringifySetCookie({
53
+ name: 'refresh_token',
54
+ value: data.refresh_token || '',
55
55
  path: BASE_PATH + AUTH_API_PREFIX,
56
56
  httpOnly: true,
57
57
  sameSite: true,
@@ -62,8 +62,8 @@ async function initAuthCookies({
62
62
  ];
63
63
  }
64
64
 
65
- function parse(cookieHeader: string): Partial<AuthCookiesData> {
66
- const data = cookie.parse(cookieHeader);
65
+ function parse(cookieHeader: string): Partial<WhookAuthCookiesData> {
66
+ const data = parseCookie(cookieHeader);
67
67
 
68
68
  if (data.access_token && data.refresh_token) {
69
69
  return {
@@ -80,3 +80,5 @@ async function initAuthCookies({
80
80
  parse,
81
81
  };
82
82
  }
83
+
84
+ export default location(autoService(initAuthCookies), import.meta.url);