@whook/oauth2 24.1.1 → 25.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 (64) hide show
  1. package/README.md +12 -10
  2. package/dist/index.d.ts +7 -6
  3. package/dist/index.js +5 -4
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +173 -168
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/routes/getOAuth2Authorize.d.ts +39 -2
  8. package/dist/routes/getOAuth2Authorize.js +53 -2
  9. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  10. package/dist/routes/getOAuth2WellKnown.d.ts +239 -0
  11. package/dist/routes/getOAuth2WellKnown.js +299 -0
  12. package/dist/routes/getOAuth2WellKnown.js.map +1 -0
  13. package/dist/routes/getOAuth2WellKnown.test.d.ts +1 -0
  14. package/dist/routes/getOAuth2WellKnown.test.js +100 -0
  15. package/dist/routes/getOAuth2WellKnown.test.js.map +1 -0
  16. package/dist/routes/postOAuth2Acknowledge.d.ts +1 -1
  17. package/dist/routes/postOAuth2Acknowledge.js +12 -10
  18. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  19. package/dist/routes/postOAuth2Acknowledge.test.js +26 -21
  20. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  21. package/dist/routes/postOAuth2Token.d.ts +10 -0
  22. package/dist/routes/postOAuth2Token.js +10 -0
  23. package/dist/routes/postOAuth2Token.js.map +1 -1
  24. package/dist/services/authCookies.d.ts +10 -9
  25. package/dist/services/authCookies.js.map +1 -1
  26. package/dist/services/authCookies.test.js.map +1 -1
  27. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +5 -5
  28. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  29. package/dist/services/oAuth2CodeGranter.d.ts +31 -6
  30. package/dist/services/oAuth2CodeGranter.js +37 -9
  31. package/dist/services/oAuth2CodeGranter.js.map +1 -1
  32. package/dist/services/oAuth2CodeGranter.test.js +56 -20
  33. package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
  34. package/dist/services/oAuth2Granters.d.ts +30 -26
  35. package/dist/services/oAuth2Granters.js +14 -0
  36. package/dist/services/oAuth2Granters.js.map +1 -1
  37. package/dist/services/oAuth2PasswordGranter.d.ts +5 -5
  38. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  39. package/dist/services/oAuth2RefreshTokenGranter.d.ts +5 -5
  40. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  41. package/dist/services/oAuth2TokenGranter.d.ts +13 -5
  42. package/dist/services/oAuth2TokenGranter.js +11 -7
  43. package/dist/services/oAuth2TokenGranter.js.map +1 -1
  44. package/package.json +9 -9
  45. package/src/__snapshots__/index.test.ts.snap +3 -0
  46. package/src/index.test.ts +177 -167
  47. package/src/index.ts +34 -2
  48. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +1 -2
  49. package/src/routes/getOAuth2Authorize.ts +67 -2
  50. package/src/routes/getOAuth2WellKnown.test.ts +132 -0
  51. package/src/routes/getOAuth2WellKnown.ts +335 -0
  52. package/src/routes/postOAuth2Acknowledge.test.ts +26 -21
  53. package/src/routes/postOAuth2Acknowledge.ts +19 -18
  54. package/src/routes/postOAuth2Token.ts +12 -1
  55. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +3 -0
  56. package/src/services/authCookies.test.ts +5 -5
  57. package/src/services/authCookies.ts +13 -9
  58. package/src/services/oAuth2ClientCredentialsGranter.ts +5 -4
  59. package/src/services/oAuth2CodeGranter.test.ts +97 -30
  60. package/src/services/oAuth2CodeGranter.ts +89 -15
  61. package/src/services/oAuth2Granters.ts +60 -64
  62. package/src/services/oAuth2PasswordGranter.ts +5 -4
  63. package/src/services/oAuth2RefreshTokenGranter.ts +5 -4
  64. package/src/services/oAuth2TokenGranter.ts +27 -15
@@ -33,6 +33,16 @@ export interface OAuth2YErrorRegistry {
33
33
  * Thrown when refresh_token cookie is absent and required
34
34
  */
35
35
  E_REFRESH_COOKIE: [cookie: string];
36
+
37
+ /**
38
+ * Thrown when server requires PKCE
39
+ */
40
+ E_PKCE_REQUIRED: [responseType: string];
41
+
42
+ /**
43
+ * Thrown when server does not support PKCE
44
+ */
45
+ E_PKCE_NOT_SUPPORTED: [responseType: string];
36
46
  }
37
47
 
38
48
  export const OAUTH2_ERRORS_DESCRIPTORS: Record<
@@ -87,35 +97,35 @@ export const OAUTH2_ERRORS_DESCRIPTORS: Record<
87
97
  uri: DEFAULT_ERROR_URI,
88
98
  help: DEFAULT_HELP_URI,
89
99
  },
100
+ E_PKCE_REQUIRED: {
101
+ code: 'invalid_request',
102
+ status: 400,
103
+ description: 'Code challenge required for this response type ($0)',
104
+ uri: DEFAULT_ERROR_URI,
105
+ help: DEFAULT_HELP_URI,
106
+ },
107
+ E_PKCE_NOT_SUPPORTED: {
108
+ code: 'invalid_request',
109
+ status: 400,
110
+ description: 'Code challenge not supported for this response type ($0)',
111
+ uri: DEFAULT_ERROR_URI,
112
+ help: DEFAULT_HELP_URI,
113
+ },
90
114
  };
91
115
 
92
- export type OAuth2CodeService<CODE = string> = {
93
- create: (
94
- authenticationData: WhookAuthenticationData,
95
- redirectURI: string,
96
- additionalParameters: Record<string, unknown>,
97
- ) => Promise<CODE>;
98
- check: (
99
- authenticationData: WhookAuthenticationData,
100
- code: CODE,
101
- redirectURI: string,
102
- ) => Promise<
103
- WhookAuthenticationData & {
104
- redirectURI: string;
105
- } & Record<string, unknown>
106
- >;
107
- };
108
-
109
- export type OAuth2PasswordService<USERNAME = string, PASSWORD = string> = {
116
+ export interface OAuth2PasswordService<
117
+ USERNAME extends string = string,
118
+ PASSWORD extends string = string,
119
+ > {
110
120
  check: (
111
121
  authenticationData: WhookAuthenticationData,
112
122
  username: USERNAME,
113
123
  password: PASSWORD,
114
124
  demandedScope: WhookAuthenticationData['scope'],
115
125
  ) => Promise<WhookAuthenticationData>;
116
- };
126
+ }
117
127
 
118
- export type OAuth2AccessTokenService<TOKEN = string> = {
128
+ export interface OAuth2AccessTokenService<TOKEN extends string = string> {
119
129
  create: (
120
130
  authenticationData: WhookAuthenticationData,
121
131
  tokenAuthenticationData: WhookAuthenticationData,
@@ -129,9 +139,9 @@ export type OAuth2AccessTokenService<TOKEN = string> = {
129
139
  token: TOKEN,
130
140
  scope?: WhookAuthenticationData['scope'],
131
141
  ) => Promise<WhookAuthenticationData>;
132
- };
142
+ }
133
143
 
134
- export type OAuth2RefreshTokenService<TOKEN = string> =
144
+ export type OAuth2RefreshTokenService<TOKEN extends string = string> =
135
145
  OAuth2AccessTokenService<TOKEN>;
136
146
 
137
147
  export type CheckApplicationService = (context: {
@@ -146,36 +156,27 @@ export type CheckApplicationService = (context: {
146
156
  redirectURI: string;
147
157
  }>;
148
158
 
149
- export type OAuth2GranterAuthorize<
150
- AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
151
- string,
152
- unknown
153
- >,
154
- > = (
159
+ export type OAuth2GranterAuthorize<P extends object> = (
155
160
  context: {
156
161
  clientId: WhookAuthenticationData['applicationId'];
157
162
  redirectURI: string;
158
163
  scope: WhookAuthenticationData['scope'];
159
164
  },
160
- authorizeParameters?: AUTHORIZE_PARAMETERS,
165
+ authorizeParameters: P,
161
166
  ) => Promise<{
162
167
  applicationId: WhookAuthenticationData['applicationId'];
163
168
  redirectURI: string;
164
169
  scope: WhookAuthenticationData['scope'];
165
170
  }>;
166
171
 
167
- export type Oauth2GranterAuthenticate<
168
- GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
169
- > = (
170
- grantParameters: GRANT_PARAMETERS,
172
+ export type Oauth2GranterAuthenticate<P extends object> = (
173
+ authenticateParameters: P,
171
174
  authenticationData: WhookAuthenticationData,
172
175
  ) => Promise<WhookAuthenticationData>;
173
176
 
174
177
  export type OAuth2GranterAcknowledge<
175
- ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
176
- string,
177
- unknown
178
- >,
178
+ A extends object,
179
+ P extends object = Record<string, unknown>,
179
180
  > = (
180
181
  authenticationData: WhookAuthenticationData,
181
182
  acknowledgeParameters: {
@@ -183,49 +184,44 @@ export type OAuth2GranterAcknowledge<
183
184
  redirectURI: string;
184
185
  scope: WhookAuthenticationData['scope'];
185
186
  },
186
- additionalParameters: ACKNOWLEDGE_PARAMETERS,
187
- ) => Promise<
188
- WhookAuthenticationData & {
189
- redirectURI: string;
190
- [name: string]: unknown;
191
- }
192
- >;
193
-
194
- export type OAuth2GranterService<
195
- AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
196
- string,
197
- unknown
198
- >,
199
- ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
200
- string,
201
- unknown
202
- >,
203
- GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
204
- > = {
187
+ additionalParameters: P,
188
+ ) => Promise<{
189
+ authenticationData: WhookAuthenticationData;
190
+ redirectURI: string;
191
+ acknowledgedData: A;
192
+ }>;
193
+
194
+ export interface OAuth2GranterService<
195
+ T extends object = Record<string, unknown>,
196
+ U extends object = Record<string, unknown>,
197
+ V extends object = Record<string, unknown>,
198
+ W extends object = Record<string, unknown>,
199
+ > {
205
200
  type: string;
206
201
  authorizer?: {
207
202
  responseType: string;
208
- authorize: OAuth2GranterAuthorize<AUTHORIZE_PARAMETERS>;
203
+ authorize: OAuth2GranterAuthorize<T>;
209
204
  };
210
205
  acknowledger?: {
211
206
  acknowledgmentType: string;
212
- acknowledge: OAuth2GranterAcknowledge<ACKNOWLEDGE_PARAMETERS>;
207
+ acknowledge: OAuth2GranterAcknowledge<U, V>;
213
208
  };
214
209
  authenticator?: {
215
210
  grantType: string;
216
- authenticate: Oauth2GranterAuthenticate<GRANT_PARAMETERS>;
211
+ authenticate: Oauth2GranterAuthenticate<W>;
217
212
  };
218
- };
213
+ }
219
214
 
220
- export type OAuth2Options = {
215
+ export interface OAuth2Options {
221
216
  authenticateURL: string;
222
217
  defaultToClientScope?: boolean;
223
- };
218
+ forcePKCE?: boolean;
219
+ }
224
220
 
225
- export type OAuth2Config = {
221
+ export interface OAuth2Config {
226
222
  OAUTH2: OAuth2Options;
227
223
  ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
228
- };
224
+ }
229
225
 
230
226
  export default location(
231
227
  initializer(
@@ -8,17 +8,18 @@ import {
8
8
  type CheckApplicationService,
9
9
  } from './oAuth2Granters.js';
10
10
 
11
- export type OAuth2PasswordGranterDependencies = {
11
+ export interface OAuth2PasswordGranterDependencies {
12
12
  oAuth2Password: OAuth2PasswordService;
13
13
  checkApplication: CheckApplicationService;
14
14
  log?: LogService;
15
- };
16
- export type OAuth2PasswordGranterParameters = {
15
+ }
16
+ export interface OAuth2PasswordGranterParameters {
17
17
  username: string;
18
18
  password: string;
19
19
  scope?: string;
20
- };
20
+ }
21
21
  export type OAuth2PasswordGranterService = OAuth2GranterService<
22
+ Record<string, unknown>,
22
23
  Record<string, unknown>,
23
24
  Record<string, unknown>,
24
25
  OAuth2PasswordGranterParameters
@@ -8,16 +8,17 @@ import {
8
8
  type OAuth2RefreshTokenService,
9
9
  } from './oAuth2Granters.js';
10
10
 
11
- export type OAuth2RefreshTokenGranterDependencies = {
11
+ export interface OAuth2RefreshTokenGranterDependencies {
12
12
  checkApplication: CheckApplicationService;
13
13
  oAuth2RefreshToken: OAuth2RefreshTokenService;
14
14
  log?: LogService;
15
- };
16
- export type OAuth2RefreshTokenGranterParameters = {
15
+ }
16
+ export interface OAuth2RefreshTokenGranterParameters {
17
17
  refreshToken: string;
18
18
  scope?: string;
19
- };
19
+ }
20
20
  export type OAuth2RefreshTokenGranterService = OAuth2GranterService<
21
+ Record<string, unknown>,
21
22
  Record<string, unknown>,
22
23
  Record<string, unknown>,
23
24
  OAuth2RefreshTokenGranterParameters
@@ -5,23 +5,31 @@ import {
5
5
  type OAuth2GranterService,
6
6
  type OAuth2AccessTokenService,
7
7
  type CheckApplicationService,
8
- type OAuth2CodeService,
9
8
  } from './oAuth2Granters.js';
9
+ import { type OAuth2CodeService } from './oAuth2CodeGranter.js';
10
10
 
11
11
  export const IMPLICIT_GRANTER_TYPE = 'token';
12
12
 
13
- export type OAuth2TokenGranterDependencies = {
13
+ export interface OAuth2TokenGranterDependencies {
14
14
  oAuth2Code: OAuth2CodeService;
15
15
  checkApplication: CheckApplicationService;
16
16
  oAuth2AccessToken: OAuth2AccessTokenService;
17
17
  time?: TimeService;
18
18
  log?: LogService;
19
- };
20
- export type OAuth2TokenGranterParameters = Record<string, unknown>;
19
+ }
20
+ export type OAuth2TokenGranterAuthorizeParameters = Record<string, unknown>;
21
+ export interface OAuth2TokenGranterAcknowledgedData {
22
+ accessToken: string;
23
+ tokenType: 'bearer';
24
+ expiresIn: number;
25
+ }
26
+ export type OAuth2TokenGranterAcknowledgeParameters = Record<string, unknown>;
27
+ export type OAuth2TokenGranterGrantParameters = Record<string, unknown>;
21
28
  export type OAuth2TokenGranterService = OAuth2GranterService<
22
- Record<string, unknown>,
23
- Record<string, unknown>,
24
- OAuth2TokenGranterParameters
29
+ OAuth2TokenGranterAuthorizeParameters,
30
+ OAuth2TokenGranterAcknowledgedData,
31
+ OAuth2TokenGranterAcknowledgeParameters,
32
+ OAuth2TokenGranterGrantParameters
25
33
  >;
26
34
 
27
35
  export default location(autoService(initOAuth2TokenGranter), import.meta.url);
@@ -54,7 +62,7 @@ async function initOAuth2TokenGranter({
54
62
  // Access Token Response:
55
63
  // https://tools.ietf.org/html/rfc6749#section-4.2.2
56
64
  const acknowledgeWithToken: NonNullable<
57
- OAuth2GranterService<OAuth2TokenGranterParameters>['acknowledger']
65
+ OAuth2TokenGranterService['acknowledger']
58
66
  >['acknowledge'] = async (
59
67
  authenticationData,
60
68
  { clientId, redirectURI, scope: providedScope },
@@ -80,14 +88,18 @@ async function initOAuth2TokenGranter({
80
88
  );
81
89
 
82
90
  return {
83
- // TODO: check a way to avoid this by adding params
84
- ...authenticationData,
85
- applicationId: clientId,
91
+ authenticationData: {
92
+ ...authenticationData,
93
+ applicationId: clientId,
94
+ scope: providedScope,
95
+ },
86
96
  redirectURI: finalRedirectURI,
87
- scope: providedScope,
88
- accessToken,
89
- tokenType: 'bearer',
90
- expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
97
+ acknowledgedData: {
98
+ accessToken,
99
+ tokenType: 'bearer',
100
+ expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
101
+ },
102
+ additionalParameters,
91
103
  };
92
104
  };
93
105