@whook/oauth2 25.2.1 → 26.0.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 (173) hide show
  1. package/README.md +147 -98
  2. package/dist/index.d.ts +30 -11
  3. package/dist/index.js +15 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1258 -399
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/clients.d.ts +2 -0
  8. package/dist/libs/clients.js +13 -0
  9. package/dist/libs/clients.js.map +1 -0
  10. package/dist/libs/errors.d.ts +113 -0
  11. package/dist/libs/errors.js +178 -0
  12. package/dist/libs/errors.js.map +1 -0
  13. package/dist/libs/grants.d.ts +2 -0
  14. package/dist/libs/grants.js +10 -0
  15. package/dist/libs/grants.js.map +1 -0
  16. package/dist/libs/redirectURI.d.ts +7 -0
  17. package/dist/libs/redirectURI.js +78 -0
  18. package/dist/libs/redirectURI.js.map +1 -0
  19. package/dist/libs/schemas.d.ts +23 -0
  20. package/dist/libs/schemas.js +25 -0
  21. package/dist/libs/schemas.js.map +1 -0
  22. package/dist/libs/scopes.d.ts +16 -0
  23. package/dist/libs/scopes.js +37 -0
  24. package/dist/libs/scopes.js.map +1 -1
  25. package/dist/libs/utils.d.ts +2 -0
  26. package/dist/libs/utils.js +17 -0
  27. package/dist/libs/utils.js.map +1 -0
  28. package/dist/libs/verifier.d.ts +7 -0
  29. package/dist/libs/verifier.js +37 -0
  30. package/dist/libs/verifier.js.map +1 -0
  31. package/dist/libs/verifier.test.js +21 -0
  32. package/dist/libs/verifier.test.js.map +1 -0
  33. package/dist/routes/getOAuth2Authorize.d.ts +13 -50
  34. package/dist/routes/getOAuth2Authorize.js +60 -86
  35. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  36. package/dist/routes/getOAuth2Authorize.test.js +147 -68
  37. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  38. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +8 -8
  39. package/dist/routes/getOAuth2WellKnownMetadata.js +15 -8
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +37 -37
  42. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  43. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  44. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  45. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  46. package/dist/routes/postAuthLogin.d.ts +14 -9
  47. package/dist/routes/postAuthLogin.js +10 -5
  48. package/dist/routes/postAuthLogin.js.map +1 -1
  49. package/dist/routes/postAuthLogin.test.js +79 -55
  50. package/dist/routes/postAuthLogin.test.js.map +1 -1
  51. package/dist/routes/postAuthLogout.d.ts +5 -2
  52. package/dist/routes/postAuthLogout.js +12 -7
  53. package/dist/routes/postAuthLogout.js.map +1 -1
  54. package/dist/routes/postAuthLogout.test.js +35 -17
  55. package/dist/routes/postAuthLogout.test.js.map +1 -1
  56. package/dist/routes/postAuthRefresh.d.ts +14 -9
  57. package/dist/routes/postAuthRefresh.js +10 -6
  58. package/dist/routes/postAuthRefresh.js.map +1 -1
  59. package/dist/routes/postAuthRefresh.test.js +90 -59
  60. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  61. package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  65. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  66. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  67. package/dist/routes/postOAuth2Revoke.js +102 -0
  68. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  69. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  70. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  71. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  72. package/dist/routes/postOAuth2Token.d.ts +35 -108
  73. package/dist/routes/postOAuth2Token.js +53 -114
  74. package/dist/routes/postOAuth2Token.js.map +1 -1
  75. package/dist/routes/postOAuth2Token.test.js +493 -61
  76. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  77. package/dist/services/authCookies.d.ts +9 -13
  78. package/dist/services/authCookies.js +1 -1
  79. package/dist/services/authCookies.js.map +1 -1
  80. package/dist/services/authCookies.test.js.map +1 -1
  81. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +129 -0
  82. package/dist/services/oAuth2AuthorizationCodeGranter.js +221 -0
  83. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  84. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  85. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +474 -0
  86. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  88. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  89. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  90. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  91. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  92. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  93. package/dist/services/oAuth2Granters.d.ts +81 -86
  94. package/dist/services/oAuth2Granters.js +2 -67
  95. package/dist/services/oAuth2Granters.js.map +1 -1
  96. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  97. package/dist/services/oAuth2ImplicitGranter.js +72 -0
  98. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  99. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  100. package/dist/services/oAuth2ImplicitGranter.test.js +727 -0
  101. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  102. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  103. package/dist/services/oAuth2PasswordGranter.js +53 -18
  104. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  105. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  106. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  107. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  109. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  111. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  113. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  114. package/package.json +11 -11
  115. package/src/index.test.ts +1314 -404
  116. package/src/index.ts +46 -53
  117. package/src/libs/clients.ts +22 -0
  118. package/src/libs/errors.ts +325 -0
  119. package/src/libs/grants.ts +15 -0
  120. package/src/libs/redirectURI.ts +120 -0
  121. package/src/libs/schemas.ts +27 -0
  122. package/src/libs/scopes.ts +49 -0
  123. package/src/libs/utils.ts +24 -0
  124. package/src/libs/verifier.test.ts +37 -0
  125. package/src/libs/verifier.ts +57 -0
  126. package/src/routes/getOAuth2Authorize.test.ts +179 -96
  127. package/src/routes/getOAuth2Authorize.ts +97 -124
  128. package/src/routes/getOAuth2WellKnownMetadata.test.ts +57 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +21 -10
  130. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  131. package/src/routes/postAuthLogin.test.ts +86 -57
  132. package/src/routes/postAuthLogin.ts +24 -14
  133. package/src/routes/postAuthLogout.test.ts +42 -18
  134. package/src/routes/postAuthLogout.ts +22 -8
  135. package/src/routes/postAuthRefresh.test.ts +98 -61
  136. package/src/routes/postAuthRefresh.ts +25 -13
  137. package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
  138. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  139. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  140. package/src/routes/postOAuth2Revoke.ts +156 -0
  141. package/src/routes/postOAuth2Token.test.ts +570 -108
  142. package/src/routes/postOAuth2Token.ts +92 -136
  143. package/src/services/authCookies.test.ts +5 -5
  144. package/src/services/authCookies.ts +16 -18
  145. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +564 -0
  146. package/src/services/oAuth2AuthorizationCodeGranter.ts +388 -0
  147. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  148. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  149. package/src/services/oAuth2Granters.ts +101 -195
  150. package/src/services/oAuth2ImplicitGranter.test.ts +811 -0
  151. package/src/services/oAuth2ImplicitGranter.ts +163 -0
  152. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  153. package/src/services/oAuth2PasswordGranter.ts +116 -40
  154. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  155. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  156. package/src/types.d.ts +1 -0
  157. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  158. package/dist/services/oAuth2CodeGranter.js +0 -105
  159. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  160. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  161. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  162. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  163. package/dist/services/oAuth2TokenGranter.js +0 -64
  164. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  165. package/src/__snapshots__/index.test.ts.snap +0 -401
  166. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  167. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  168. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  169. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  170. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  171. package/src/services/oAuth2CodeGranter.ts +0 -210
  172. package/src/services/oAuth2TokenGranter.ts +0 -119
  173. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/verifier.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',
@@ -179,11 +99,24 @@ export const definition = {
179
99
  description: 'The lifetime in seconds of the access token',
180
100
  type: 'number',
181
101
  },
102
+ expiration_date: {
103
+ type: 'string',
104
+ format: 'date-time',
105
+ },
182
106
  refresh_token: {
183
107
  description:
184
108
  'See https://tools.ietf.org/html/rfc6749#section-6',
185
109
  type: 'string',
186
110
  },
111
+ refresh_token_expires_in: {
112
+ description: 'The lifetime in seconds of the refresh token',
113
+ type: 'number',
114
+ },
115
+ refresh_token_expiration_date: {
116
+ type: 'string',
117
+ format: 'date-time',
118
+ },
119
+ scope: refersTo(scopeSchema),
187
120
  },
188
121
  },
189
122
  },
@@ -221,75 +154,98 @@ export const definition = {
221
154
  } as const satisfies WhookRouteDefinition;
222
155
 
223
156
  async function initPostOAuth2Token({
157
+ OAUTH2,
224
158
  oAuth2Granters,
225
159
  oAuth2AccessToken,
226
160
  oAuth2RefreshToken,
227
161
  time = Date.now.bind(Date),
228
162
  log = noop,
229
163
  }: {
230
- oAuth2Granters: OAuth2GranterService[];
231
- oAuth2AccessToken: OAuth2AccessTokenService;
232
- oAuth2RefreshToken: OAuth2RefreshTokenService;
164
+ OAUTH2: WhookOAuth2Options;
165
+ oAuth2Granters: WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
166
+ oAuth2AccessToken: WhookOAuth2AccessTokenService;
167
+ oAuth2RefreshToken: WhookOAuth2RefreshTokenService;
233
168
  log: LogService;
234
169
  time: TimeService;
235
170
  }) {
236
171
  return async ({
237
- body: { grant_type: grantType, ...grantParameters },
238
- authenticationData,
172
+ body: { grant_type: grantType, scope: demandedScope, ...grantParameters },
173
+ authenticationData: optionalAuthenticationData,
239
174
  }: {
240
175
  body: {
241
176
  grant_type: string;
242
177
  [name: string]: unknown;
243
178
  };
244
- authenticationData: WhookAuthenticationData;
179
+ authenticationData?: WhookAuthenticationData;
245
180
  }) => {
246
181
  try {
247
182
  const granter = oAuth2Granters.find(
248
- (granter) =>
249
- granter.authenticator &&
250
- granter.authenticator.grantType === grantType,
183
+ (granter) => granter.grantType && granter.grantType === grantType,
251
184
  );
252
185
 
253
- if (!granter || !granter.authenticator) {
254
- throw new YError('E_UNKNOWN_AUTHENTICATOR_TYPE', [grantType]);
186
+ if (!granter || !granter.authenticate) {
187
+ throw new YError('E_OAUTH2_UNKNOWN_GRANT_TYPE', [grantType]);
255
188
  }
256
189
 
257
- const newAuthenticationData = await granter.authenticator.authenticate(
258
- camelCaseObjectProperties(grantParameters),
259
- authenticationData,
190
+ const newAuthenticationData = await granter.authenticate(
191
+ {
192
+ ...camelCaseObjectProperties(grantParameters),
193
+ demandedScopes:
194
+ typeof demandedScope === 'string'
195
+ ? filterScopes(
196
+ parseOAuth2Scope(demandedScope),
197
+ OAUTH2.allowedScopes,
198
+ !!OAUTH2.strictScopesChecks,
199
+ )
200
+ : [],
201
+ },
202
+ optionalAuthenticationData,
260
203
  );
261
204
 
262
205
  const [
263
206
  { token: accessToken, expiresAt: accessTokenExpiresAt },
264
207
  { token: refreshToken, expiresAt: refreshTokenExpiresAt },
265
208
  ] = await Promise.all([
266
- oAuth2AccessToken.create(authenticationData, newAuthenticationData),
267
- oAuth2RefreshToken.create(authenticationData, newAuthenticationData),
209
+ oAuth2AccessToken.create(newAuthenticationData),
210
+ granter.issuesRefreshToken
211
+ ? oAuth2RefreshToken.create(newAuthenticationData)
212
+ : Promise.resolve({
213
+ token: undefined,
214
+ expiresAt: undefined,
215
+ }),
268
216
  ]);
269
217
  const currentTime = time();
270
218
 
271
219
  return {
272
220
  status: 200,
273
- headers: {},
221
+ headers: {
222
+ 'Cache-Control': 'no-store',
223
+ Pragma: 'no-cache',
224
+ },
274
225
  body: {
275
226
  access_token: accessToken,
276
227
  token_type: 'bearer',
277
228
  expires_in: Math.ceil((accessTokenExpiresAt - currentTime) / 1000),
278
229
  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(),
230
+ ...(refreshToken
231
+ ? {
232
+ refresh_token: refreshToken,
233
+ refresh_token_expires_in: Math.ceil(
234
+ (refreshTokenExpiresAt - currentTime) / 1000,
235
+ ),
236
+ refresh_token_expiration_date: new Date(
237
+ refreshTokenExpiresAt,
238
+ ).toISOString(),
239
+ }
240
+ : {}),
241
+ scope: stringifyScopes(newAuthenticationData.scopes),
286
242
  },
287
243
  };
288
244
  } catch (err) {
289
245
  log('debug', '👫 - OAuth2 token issuing error', (err as YError).code);
290
246
  log('error-stack', printStackTrace(err));
291
247
 
292
- throw YError.cast(err as Error, 'E_OAUTH2');
248
+ throw YError.cast(err as Error, 'E_OAUTH2_UNEXPECTED_ERROR');
293
249
  }
294
250
  };
295
251
  }
@@ -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,47 +1,43 @@
1
1
  import ms from 'ms';
2
2
  import cookie, { 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
42
  cookie.serialize('access_token', data.access_token || '', {
47
43
  path: BASE_PATH + AUTH_API_PREFIX,
@@ -62,7 +58,7 @@ async function initAuthCookies({
62
58
  ];
63
59
  }
64
60
 
65
- function parse(cookieHeader: string): Partial<AuthCookiesData> {
61
+ function parse(cookieHeader: string): Partial<WhookAuthCookiesData> {
66
62
  const data = cookie.parse(cookieHeader);
67
63
 
68
64
  if (data.access_token && data.refresh_token) {
@@ -80,3 +76,5 @@ async function initAuthCookies({
80
76
  parse,
81
77
  };
82
78
  }
79
+
80
+ export default location(autoService(initAuthCookies), import.meta.url);