@whook/oauth2 8.5.0 → 9.0.1

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 (88) hide show
  1. package/README.md +87 -56
  2. package/dist/handlers/getOAuth2Authorize.d.ts +5 -3
  3. package/dist/handlers/getOAuth2Authorize.js +1 -1
  4. package/dist/handlers/getOAuth2Authorize.js.map +1 -1
  5. package/dist/handlers/getOAuth2Authorize.mjs +1 -1
  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 +1 -0
  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.map +1 -1
  17. package/dist/handlers/postAuthLogout.mjs.map +1 -1
  18. package/dist/handlers/postAuthLogout.test.js.map +1 -1
  19. package/dist/handlers/postAuthLogout.test.mjs.map +1 -1
  20. package/dist/handlers/postAuthRefresh.d.ts +3 -3
  21. package/dist/handlers/postAuthRefresh.js +1 -0
  22. package/dist/handlers/postAuthRefresh.js.map +1 -1
  23. package/dist/handlers/postAuthRefresh.mjs +1 -0
  24. package/dist/handlers/postAuthRefresh.mjs.map +1 -1
  25. package/dist/handlers/postAuthRefresh.test.js.map +1 -1
  26. package/dist/handlers/postAuthRefresh.test.mjs.map +1 -1
  27. package/dist/handlers/postOAuth2Acknowledge.d.ts +10 -7
  28. package/dist/handlers/postOAuth2Acknowledge.js +2 -2
  29. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
  30. package/dist/handlers/postOAuth2Acknowledge.mjs +2 -2
  31. package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -1
  32. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
  33. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -1
  34. package/dist/handlers/postOAuth2Token.d.ts +3 -3
  35. package/dist/handlers/postOAuth2Token.js +2 -2
  36. package/dist/handlers/postOAuth2Token.js.map +1 -1
  37. package/dist/handlers/postOAuth2Token.mjs +2 -2
  38. package/dist/handlers/postOAuth2Token.mjs.map +1 -1
  39. package/dist/handlers/postOAuth2Token.test.js.map +1 -1
  40. package/dist/handlers/postOAuth2Token.test.mjs.map +1 -1
  41. package/dist/index.d.ts +2 -2
  42. package/dist/index.js.map +1 -1
  43. package/dist/index.mjs.map +1 -1
  44. package/dist/index.test.js +6 -7
  45. package/dist/index.test.js.map +1 -1
  46. package/dist/index.test.mjs +6 -7
  47. package/dist/index.test.mjs.map +1 -1
  48. package/dist/services/authCookies.d.ts +7 -4
  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 +34 -34
  73. package/src/handlers/getOAuth2Authorize.ts +15 -14
  74. package/src/handlers/postAuthLogin.ts +4 -3
  75. package/src/handlers/postAuthRefresh.ts +4 -3
  76. package/src/handlers/postOAuth2Acknowledge.ts +34 -26
  77. package/src/handlers/postOAuth2Token.test.ts +2 -2
  78. package/src/handlers/postOAuth2Token.ts +4 -4
  79. package/src/index.test.ts +8 -9
  80. package/src/index.ts +6 -4
  81. package/src/services/authCookies.ts +8 -4
  82. package/src/services/oAuth2ClientCredentialsGranter.ts +25 -21
  83. package/src/services/oAuth2CodeGranter.test.ts +15 -14
  84. package/src/services/oAuth2CodeGranter.ts +66 -60
  85. package/src/services/oAuth2Granters.ts +59 -29
  86. package/src/services/oAuth2PasswordGranter.ts +23 -22
  87. package/src/services/oAuth2RefreshTokenGranter.ts +28 -24
  88. package/src/services/oAuth2TokenGranter.ts +44 -42
@@ -39,79 +39,85 @@ async function initOAuth2CodeGranter({
39
39
  checkApplication,
40
40
  log = noop,
41
41
  }: OAuth2CodeGranterDependencies): Promise<OAuth2CodeGranterService> {
42
- const authorizeWithCode: OAuth2CodeGranterService['authorizer']['authorize'] =
43
- async ({ clientId, redirectURI, scope = '' }) => {
44
- const { redirectURI: finalRedirectURI } = await checkApplication({
45
- applicationId: clientId,
46
- type: CODE_GRANTER_TYPE,
47
- redirectURI,
48
- scope,
49
- });
42
+ const authorizeWithCode: NonNullable<
43
+ OAuth2CodeGranterService['authorizer']
44
+ >['authorize'] = async ({ clientId, redirectURI, scope = '' }) => {
45
+ const { redirectURI: finalRedirectURI } = await checkApplication({
46
+ applicationId: clientId,
47
+ type: CODE_GRANTER_TYPE,
48
+ redirectURI,
49
+ scope,
50
+ });
50
51
 
51
- return {
52
- applicationId: clientId,
53
- redirectURI: finalRedirectURI,
54
- scope,
55
- };
52
+ return {
53
+ applicationId: clientId,
54
+ redirectURI: finalRedirectURI,
55
+ scope,
56
56
  };
57
+ };
57
58
 
58
59
  // Authorization Code Response:
59
60
  // https://tools.ietf.org/html/rfc6749#section-4.1.2
60
- const acknowledgeWithCode: OAuth2CodeGranterService['acknowledger']['acknowledge'] =
61
- async (
62
- authenticationData,
63
- { clientId, redirectURI, scope },
61
+ const acknowledgeWithCode: NonNullable<
62
+ OAuth2CodeGranterService['acknowledger']
63
+ >['acknowledge'] = async (
64
+ authenticationData,
65
+ { clientId, redirectURI, scope },
66
+ additionalParameters,
67
+ ) => {
68
+ const code = await oAuth2Code.create(
69
+ { ...authenticationData, applicationId: clientId, scope },
70
+ redirectURI,
64
71
  additionalParameters,
65
- ) => {
66
- const code = await oAuth2Code.create(
67
- { ...authenticationData, applicationId: clientId, scope },
68
- redirectURI,
69
- additionalParameters,
70
- );
72
+ );
71
73
 
72
- return {
73
- applicationId: clientId,
74
- redirectURI,
75
- scope,
76
- code,
77
- };
74
+ return {
75
+ applicationId: clientId,
76
+ redirectURI,
77
+ scope,
78
+ code,
78
79
  };
80
+ };
79
81
 
80
- const authenticateWithCode: OAuth2CodeGranterService['authenticator']['authenticate'] =
81
- async ({ code, clientId, redirectURI }, authenticationData) => {
82
- // The client must be authenticated (for now, see below)
83
- if (!authenticationData) {
84
- throw new YError('E_UNAUTHORIZED');
85
- }
82
+ const authenticateWithCode: NonNullable<
83
+ OAuth2CodeGranterService['authenticator']
84
+ >['authenticate'] = async (
85
+ { code, clientId, redirectURI },
86
+ authenticationData,
87
+ ) => {
88
+ // The client must be authenticated (for now, see below)
89
+ if (!authenticationData) {
90
+ throw new YError('E_UNAUTHORIZED');
91
+ }
86
92
 
87
- // This check is not really necessary atm but it acts
88
- // as a reminder that this grant type could be used
89
- // without authenticating the client. In this
90
- // scenario, the authenticationData should be deducted
91
- // from the clientId and the code given in parameters
92
- // See https://tools.ietf.org/html/rfc6749#section-4.1.3
93
- if (clientId && clientId !== authenticationData.applicationId) {
94
- throw new YError(
95
- 'E_UNAUTHORIZED',
96
- clientId,
97
- authenticationData.applicationId,
98
- );
99
- }
93
+ // This check is not really necessary atm but it acts
94
+ // as a reminder that this grant type could be used
95
+ // without authenticating the client. In this
96
+ // scenario, the authenticationData should be deducted
97
+ // from the clientId and the code given in parameters
98
+ // See https://tools.ietf.org/html/rfc6749#section-4.1.3
99
+ if (clientId && clientId !== authenticationData.applicationId) {
100
+ throw new YError(
101
+ 'E_UNAUTHORIZED',
102
+ clientId,
103
+ authenticationData.applicationId,
104
+ );
105
+ }
100
106
 
101
- await checkApplication({
102
- applicationId: authenticationData.applicationId,
103
- type: CODE_GRANTER_TYPE,
104
- scope: '',
105
- });
107
+ await checkApplication({
108
+ applicationId: authenticationData.applicationId,
109
+ type: CODE_GRANTER_TYPE,
110
+ scope: '',
111
+ });
106
112
 
107
- const newAuthenticationData = await oAuth2Code.check(
108
- authenticationData,
109
- code,
110
- redirectURI,
111
- );
113
+ const newAuthenticationData = await oAuth2Code.check(
114
+ authenticationData,
115
+ code,
116
+ redirectURI,
117
+ );
112
118
 
113
- return newAuthenticationData;
114
- };
119
+ return newAuthenticationData;
120
+ };
115
121
 
116
122
  log('debug', '👫 - OAuth2CodeGranter Service Initialized!');
117
123
 
@@ -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