@whook/oauth2 8.4.2 → 9.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 (86) hide show
  1. package/README.md +21 -7
  2. package/dist/handlers/getOAuth2Authorize.d.ts +5 -3
  3. package/dist/handlers/getOAuth2Authorize.js +6 -13
  4. package/dist/handlers/getOAuth2Authorize.js.map +1 -1
  5. package/dist/handlers/getOAuth2Authorize.mjs +3 -12
  6. package/dist/handlers/getOAuth2Authorize.mjs.map +1 -1
  7. package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
  8. package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -1
  9. package/dist/handlers/postAuthLogin.d.ts +2 -2
  10. package/dist/handlers/postAuthLogin.js +2 -1
  11. package/dist/handlers/postAuthLogin.js.map +1 -1
  12. package/dist/handlers/postAuthLogin.mjs +1 -0
  13. package/dist/handlers/postAuthLogin.mjs.map +1 -1
  14. package/dist/handlers/postAuthLogin.test.js.map +1 -1
  15. package/dist/handlers/postAuthLogin.test.mjs.map +1 -1
  16. package/dist/handlers/postAuthLogout.js +1 -1
  17. package/dist/handlers/postAuthLogout.js.map +1 -1
  18. package/dist/handlers/postAuthLogout.mjs.map +1 -1
  19. package/dist/handlers/postAuthLogout.test.js.map +1 -1
  20. package/dist/handlers/postAuthLogout.test.mjs.map +1 -1
  21. package/dist/handlers/postAuthRefresh.d.ts +3 -3
  22. package/dist/handlers/postAuthRefresh.js +5 -4
  23. package/dist/handlers/postAuthRefresh.js.map +1 -1
  24. package/dist/handlers/postAuthRefresh.mjs +3 -3
  25. package/dist/handlers/postAuthRefresh.mjs.map +1 -1
  26. package/dist/handlers/postAuthRefresh.test.js.map +1 -1
  27. package/dist/handlers/postAuthRefresh.test.mjs.map +1 -1
  28. package/dist/handlers/postOAuth2Acknowledge.d.ts +10 -7
  29. package/dist/handlers/postOAuth2Acknowledge.js +3 -3
  30. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
  31. package/dist/handlers/postOAuth2Acknowledge.mjs +2 -2
  32. package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -1
  33. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
  34. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -1
  35. package/dist/handlers/postOAuth2Token.d.ts +3 -3
  36. package/dist/handlers/postOAuth2Token.js +6 -18
  37. package/dist/handlers/postOAuth2Token.js.map +1 -1
  38. package/dist/handlers/postOAuth2Token.mjs +6 -17
  39. package/dist/handlers/postOAuth2Token.mjs.map +1 -1
  40. package/dist/handlers/postOAuth2Token.test.js.map +1 -1
  41. package/dist/handlers/postOAuth2Token.test.mjs.map +1 -1
  42. package/dist/index.js +62 -62
  43. package/dist/index.js.map +1 -1
  44. package/dist/index.mjs.map +1 -1
  45. package/dist/index.test.js +6 -7
  46. package/dist/index.test.js.map +1 -1
  47. package/dist/index.test.mjs +6 -7
  48. package/dist/index.test.mjs.map +1 -1
  49. package/dist/services/authCookies.js.map +1 -1
  50. package/dist/services/authCookies.mjs.map +1 -1
  51. package/dist/services/authCookies.test.js.map +1 -1
  52. package/dist/services/authCookies.test.mjs.map +1 -1
  53. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  54. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -1
  55. package/dist/services/oAuth2CodeGranter.js.map +1 -1
  56. package/dist/services/oAuth2CodeGranter.mjs.map +1 -1
  57. package/dist/services/oAuth2CodeGranter.test.js +8 -6
  58. package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
  59. package/dist/services/oAuth2CodeGranter.test.mjs +8 -6
  60. package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -1
  61. package/dist/services/oAuth2Granters.d.ts +22 -19
  62. package/dist/services/oAuth2Granters.js.map +1 -1
  63. package/dist/services/oAuth2Granters.mjs.map +1 -1
  64. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  65. package/dist/services/oAuth2PasswordGranter.mjs.map +1 -1
  66. package/dist/services/oAuth2RefreshTokenGranter.js +1 -1
  67. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  68. package/dist/services/oAuth2RefreshTokenGranter.mjs +1 -1
  69. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -1
  70. package/dist/services/oAuth2TokenGranter.js.map +1 -1
  71. package/dist/services/oAuth2TokenGranter.mjs.map +1 -1
  72. package/package.json +35 -35
  73. package/src/handlers/getOAuth2Authorize.ts +21 -29
  74. package/src/handlers/postAuthLogin.ts +4 -3
  75. package/src/handlers/postAuthRefresh.ts +6 -8
  76. package/src/handlers/postOAuth2Acknowledge.ts +34 -26
  77. package/src/handlers/postOAuth2Token.test.ts +2 -2
  78. package/src/handlers/postOAuth2Token.ts +11 -22
  79. package/src/index.test.ts +8 -9
  80. package/src/services/oAuth2ClientCredentialsGranter.ts +25 -21
  81. package/src/services/oAuth2CodeGranter.test.ts +15 -14
  82. package/src/services/oAuth2CodeGranter.ts +66 -60
  83. package/src/services/oAuth2Granters.ts +59 -29
  84. package/src/services/oAuth2PasswordGranter.ts +23 -22
  85. package/src/services/oAuth2RefreshTokenGranter.ts +28 -24
  86. package/src/services/oAuth2TokenGranter.ts +44 -42
@@ -193,6 +193,54 @@ export type CheckApplicationService = {
193
193
  }>;
194
194
  };
195
195
 
196
+ export type OAuth2GranterAuthorize<
197
+ AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
198
+ string,
199
+ unknown
200
+ >,
201
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
202
+ > = (
203
+ context: {
204
+ clientId: AUTHENTICATION_DATA['applicationId'];
205
+ redirectURI: string;
206
+ scope: AUTHENTICATION_DATA['scope'];
207
+ },
208
+ authorizeParameters?: AUTHORIZE_PARAMETERS,
209
+ ) => Promise<{
210
+ applicationId: AUTHENTICATION_DATA['applicationId'];
211
+ redirectURI: string;
212
+ scope: AUTHENTICATION_DATA['scope'];
213
+ }>;
214
+
215
+ export type Oauth2GranterAuthenticate<
216
+ GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
217
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
218
+ > = (
219
+ grantParameters: GRANT_PARAMETERS,
220
+ authenticationData: AUTHENTICATION_DATA,
221
+ ) => Promise<AUTHENTICATION_DATA>;
222
+
223
+ export type OAuth2GranterAcknowledge<
224
+ ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
225
+ string,
226
+ unknown
227
+ >,
228
+ AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
229
+ > = (
230
+ authenticationData: AUTHENTICATION_DATA,
231
+ acknowledgeParameters: {
232
+ clientId: AUTHENTICATION_DATA['applicationId'];
233
+ redirectURI: string;
234
+ scope: AUTHENTICATION_DATA['applicationId'];
235
+ },
236
+ additionalParameters: ACKNOWLEDGE_PARAMETERS,
237
+ ) => Promise<
238
+ AUTHENTICATION_DATA & {
239
+ redirectURI: string;
240
+ [name: string]: unknown;
241
+ }
242
+ >;
243
+
196
244
  export type OAuth2GranterService<
197
245
  AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
198
246
  string,
@@ -208,42 +256,24 @@ export type OAuth2GranterService<
208
256
  type: string;
209
257
  authorizer?: {
210
258
  responseType: string;
211
- authorize: (
212
- context: {
213
- clientId: AUTHENTICATION_DATA['applicationId'];
214
- redirectURI: string;
215
- scope: AUTHENTICATION_DATA['scope'];
216
- },
217
- authorizeParameters?: AUTHORIZE_PARAMETERS,
218
- ) => Promise<{
219
- applicationId: AUTHENTICATION_DATA['applicationId'];
220
- redirectURI: string;
221
- scope: AUTHENTICATION_DATA['scope'];
222
- }>;
259
+ authorize: OAuth2GranterAuthorize<
260
+ AUTHORIZE_PARAMETERS,
261
+ AUTHENTICATION_DATA
262
+ >;
223
263
  };
224
264
  acknowledger?: {
225
265
  acknowledgmentType: string;
226
- acknowledge: (
227
- authenticationData: AUTHENTICATION_DATA,
228
- acknowledgeParameters: {
229
- clientId: AUTHENTICATION_DATA['applicationId'];
230
- redirectURI: string;
231
- scope: AUTHENTICATION_DATA['applicationId'];
232
- },
233
- additionalParameters: ACKNOWLEDGE_PARAMETERS,
234
- ) => Promise<
235
- AUTHENTICATION_DATA & {
236
- redirectURI: string;
237
- [name: string]: unknown;
238
- }
266
+ acknowledge: OAuth2GranterAcknowledge<
267
+ ACKNOWLEDGE_PARAMETERS,
268
+ AUTHENTICATION_DATA
239
269
  >;
240
270
  };
241
271
  authenticator?: {
242
272
  grantType: string;
243
- authenticate: (
244
- grantParameters: GRANT_PARAMETERS,
245
- authenticationData: AUTHENTICATION_DATA,
246
- ) => Promise<AUTHENTICATION_DATA>;
273
+ authenticate: Oauth2GranterAuthenticate<
274
+ GRANT_PARAMETERS,
275
+ AUTHENTICATION_DATA
276
+ >;
247
277
  };
248
278
  };
249
279
 
@@ -37,31 +37,32 @@ async function initOAuth2PasswordGranter({
37
37
  checkApplication,
38
38
  log = noop,
39
39
  }: OAuth2PasswordGranterDependencies): Promise<OAuth2PasswordGranterService> {
40
- const authenticateWithPassword: OAuth2PasswordGranterService['authenticator']['authenticate'] =
41
- async (
42
- { username, password, scope: demandedScope = '' },
43
- authenticationData,
44
- ) => {
45
- // The client must be authenticated
46
- if (!authenticationData) {
47
- throw new YError('E_UNAUTHORIZED');
48
- }
40
+ const authenticateWithPassword: NonNullable<
41
+ OAuth2PasswordGranterService['authenticator']
42
+ >['authenticate'] = async (
43
+ { username, password, scope: demandedScope = '' },
44
+ authenticationData,
45
+ ) => {
46
+ // The client must be authenticated
47
+ if (!authenticationData) {
48
+ throw new YError('E_UNAUTHORIZED');
49
+ }
49
50
 
50
- await checkApplication({
51
- applicationId: authenticationData.applicationId,
52
- type: 'password',
53
- scope: demandedScope,
54
- });
51
+ await checkApplication({
52
+ applicationId: authenticationData.applicationId,
53
+ type: 'password',
54
+ scope: demandedScope,
55
+ });
55
56
 
56
- const finalAuthenticationData = await oAuth2Password.check(
57
- authenticationData,
58
- username,
59
- password,
60
- demandedScope,
61
- );
57
+ const finalAuthenticationData = await oAuth2Password.check(
58
+ authenticationData,
59
+ username,
60
+ password,
61
+ demandedScope,
62
+ );
62
63
 
63
- return finalAuthenticationData;
64
- };
64
+ return finalAuthenticationData;
65
+ };
65
66
 
66
67
  log('debug', '👫 - OAuth2PasswordGranter Service Initialized!');
67
68
 
@@ -36,34 +36,38 @@ async function initOAuth2RefreshTokenGranter({
36
36
  oAuth2RefreshToken,
37
37
  log = noop,
38
38
  }: OAuth2RefreshTokenGranterDependencies): Promise<OAuth2RefreshTokenGranterService> {
39
- const authenticateWithRefreshToken: OAuth2RefreshTokenGranterService['authenticator']['authenticate'] =
40
- async ({ refreshToken, scope: demandedScope }, authenticationData) => {
41
- try {
42
- // The client must be authenticated
43
- if (!authenticationData) {
44
- throw new YError('E_UNAUTHORIZED');
45
- }
39
+ const authenticateWithRefreshToken: NonNullable<
40
+ OAuth2RefreshTokenGranterService['authenticator']
41
+ >['authenticate'] = async (
42
+ { refreshToken, scope: demandedScope = '' },
43
+ authenticationData,
44
+ ) => {
45
+ try {
46
+ // The client must be authenticated
47
+ if (!authenticationData) {
48
+ throw new YError('E_UNAUTHORIZED');
49
+ }
46
50
 
47
- await checkApplication({
48
- applicationId: authenticationData.applicationId,
49
- type: 'refresh',
50
- scope: demandedScope,
51
- });
51
+ await checkApplication({
52
+ applicationId: authenticationData.applicationId,
53
+ type: 'refresh',
54
+ scope: demandedScope,
55
+ });
52
56
 
53
- const newAuthenticationData = await oAuth2RefreshToken.check(
54
- authenticationData,
55
- refreshToken,
56
- demandedScope,
57
- );
57
+ const newAuthenticationData = await oAuth2RefreshToken.check(
58
+ authenticationData,
59
+ refreshToken,
60
+ demandedScope,
61
+ );
58
62
 
59
- return newAuthenticationData;
60
- } catch (err) {
61
- if (err.code === 'E_BAD_TOKEN') {
62
- throw YError.wrap(err, 'E_BAD_REFRESH_TOKEN');
63
- }
64
- throw err;
63
+ return newAuthenticationData;
64
+ } catch (err) {
65
+ if ((err as YError).code === 'E_BAD_TOKEN') {
66
+ throw YError.wrap(err as Error, 'E_BAD_REFRESH_TOKEN');
65
67
  }
66
- };
68
+ throw err;
69
+ }
70
+ };
67
71
 
68
72
  log('debug', '👫 - OAuth2RefreshTokenGranter Service Initialized!');
69
73
 
@@ -38,56 +38,58 @@ async function initOAuth2TokenGranter({
38
38
  time = Date.now.bind(Date),
39
39
  log = noop,
40
40
  }: OAuth2TokenGranterDependencies): Promise<OAuth2TokenGranterService> {
41
- const authorizeWithToken: OAuth2TokenGranterService['authorizer']['authorize'] =
42
- async ({ clientId, redirectURI, scope }) => {
43
- const { redirectURI: finalRedirectURI } = await checkApplication({
44
- applicationId: clientId,
45
- type: IMPLICIT_GRANTER_TYPE,
46
- redirectURI,
47
- scope,
48
- });
41
+ const authorizeWithToken: NonNullable<
42
+ OAuth2TokenGranterService['authorizer']
43
+ >['authorize'] = async ({ clientId, redirectURI, scope }) => {
44
+ const { redirectURI: finalRedirectURI } = await checkApplication({
45
+ applicationId: clientId,
46
+ type: IMPLICIT_GRANTER_TYPE,
47
+ redirectURI,
48
+ scope,
49
+ });
49
50
 
50
- return {
51
- applicationId: clientId,
52
- redirectURI: finalRedirectURI,
53
- scope,
54
- };
51
+ return {
52
+ applicationId: clientId,
53
+ redirectURI: finalRedirectURI,
54
+ scope,
55
55
  };
56
+ };
56
57
 
57
58
  // Access Token Response:
58
59
  // https://tools.ietf.org/html/rfc6749#section-4.2.2
59
- const acknowledgeWithToken: OAuth2GranterService<OAuth2TokenGranterParameters>['acknowledger']['acknowledge'] =
60
- async (
61
- authenticationData,
62
- { clientId, redirectURI, scope: providedScope },
63
- additionalParameters,
64
- ) => {
65
- const { redirectURI: finalRedirectURI } = await checkApplication({
66
- applicationId: clientId,
67
- redirectURI,
68
- scope: providedScope,
69
- type: IMPLICIT_GRANTER_TYPE,
70
- });
60
+ const acknowledgeWithToken: NonNullable<
61
+ OAuth2GranterService<OAuth2TokenGranterParameters>['acknowledger']
62
+ >['acknowledge'] = async (
63
+ authenticationData,
64
+ { clientId, redirectURI, scope: providedScope },
65
+ additionalParameters,
66
+ ) => {
67
+ const { redirectURI: finalRedirectURI } = await checkApplication({
68
+ applicationId: clientId,
69
+ redirectURI,
70
+ scope: providedScope,
71
+ type: IMPLICIT_GRANTER_TYPE,
72
+ });
71
73
 
72
- const { token: accessToken, expiresAt: accessTokenExpiresAt } =
73
- await oAuth2AccessToken.create(
74
- authenticationData,
75
- {
76
- applicationId: clientId,
77
- scope: providedScope,
78
- },
79
- additionalParameters,
80
- );
74
+ const { token: accessToken, expiresAt: accessTokenExpiresAt } =
75
+ await oAuth2AccessToken.create(
76
+ authenticationData,
77
+ {
78
+ applicationId: clientId,
79
+ scope: providedScope,
80
+ },
81
+ additionalParameters,
82
+ );
81
83
 
82
- return {
83
- applicationId: clientId,
84
- redirectURI: finalRedirectURI,
85
- scope: providedScope,
86
- accessToken,
87
- tokenType: 'bearer',
88
- expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
89
- };
84
+ return {
85
+ applicationId: clientId,
86
+ redirectURI: finalRedirectURI,
87
+ scope: providedScope,
88
+ accessToken,
89
+ tokenType: 'bearer',
90
+ expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
90
91
  };
92
+ };
91
93
 
92
94
  log('debug', '👫 - OAuth2TokenGranter Service Initialized!');
93
95