@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
@@ -134,30 +134,30 @@ async function initPostOAuth2Acknowledge({
134
134
  log,
135
135
  }: HandlerDependencies) {
136
136
  return async ({
137
- authenticationData,
137
+ authenticationData: baseAuthenticationData,
138
138
  body: {
139
139
  responseType,
140
140
  clientId,
141
- redirectURI,
141
+ redirectURI: baseRedirectURI,
142
142
  scope: demandedScope,
143
143
  state,
144
144
  acknowledged = false,
145
- ...additionalParameters
145
+ ...additionalProperties
146
146
  },
147
147
  }: HandlerParameters) => {
148
- if (!authenticationData) {
148
+ if (!baseAuthenticationData) {
149
149
  throw new YError('E_UNAUTHORIZED');
150
150
  }
151
151
 
152
152
  // Here we check the applicationId has the right to authenticate a user
153
153
  // with the special type 'root'
154
154
  await checkApplication({
155
- applicationId: authenticationData.applicationId,
155
+ applicationId: baseAuthenticationData.applicationId,
156
156
  type: 'root',
157
157
  scope: '',
158
158
  });
159
159
 
160
- const url = new URL(redirectURI);
160
+ let url: URL;
161
161
 
162
162
  try {
163
163
  if (!acknowledged) {
@@ -174,29 +174,30 @@ async function initPostOAuth2Acknowledge({
174
174
  throw new YError('E_UNKNOWN_ACKNOWLEDGER_TYPE', [responseType]);
175
175
  }
176
176
 
177
- const { applicationId, scope, ...additionalProperties } =
177
+ const { authenticationData, acknowledgedData, redirectURI } =
178
178
  await granter.acknowledger.acknowledge(
179
- authenticationData,
179
+ baseAuthenticationData,
180
180
  {
181
181
  clientId,
182
- redirectURI,
182
+ redirectURI: baseRedirectURI,
183
183
  scope: demandedScope,
184
184
  },
185
- additionalParameters,
185
+ additionalProperties,
186
186
  );
187
187
 
188
- url.searchParams.set('client_id', applicationId);
189
- url.searchParams.set('scope', scope);
188
+ url = new URL(redirectURI);
189
+
190
+ url.searchParams.set('client_id', authenticationData.applicationId);
191
+ url.searchParams.set('scope', authenticationData.scope);
190
192
  url.searchParams.set('state', state);
191
- Object.keys(additionalProperties).forEach((key) =>
192
- url.searchParams.set(
193
- snakeCase(key),
194
- additionalProperties[key] as unknown as string,
195
- ),
193
+ Object.keys(acknowledgedData).forEach((key) =>
194
+ url.searchParams.set(snakeCase(key), acknowledgedData[key] as string),
196
195
  );
197
196
  } catch (err) {
198
197
  log('debug', '👫 - OAuth2 acknowledge error', (err as YError).code);
199
- log('debug-stack', printStackTrace(err as Error));
198
+ log('debug-stack', printStackTrace(err));
199
+
200
+ url = new URL(baseRedirectURI);
200
201
 
201
202
  setURLError(
202
203
  url,
@@ -19,6 +19,16 @@ import { type WhookAuthenticationData } from '@whook/authorization';
19
19
  This endpoint is to be used by the authentication server page
20
20
  to acknowlege that the user accepted the client request.
21
21
  */
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
+
22
32
  export const authorizationCodeTokenRequestBodySchema = {
23
33
  name: 'AuthorizationCodeRequestBody',
24
34
  schema: {
@@ -42,6 +52,7 @@ export const authorizationCodeTokenRequestBodySchema = {
42
52
  pattern: '^https?://',
43
53
  format: 'uri',
44
54
  },
55
+ code_verifier: refersTo(codeVerifierSchema),
45
56
  },
46
57
  },
47
58
  } as const satisfies WhookAPISchemaDefinition;
@@ -276,7 +287,7 @@ async function initPostOAuth2Token({
276
287
  };
277
288
  } catch (err) {
278
289
  log('debug', '👫 - OAuth2 token issuing error', (err as YError).code);
279
- log('error-stack', printStackTrace(err as Error));
290
+ log('error-stack', printStackTrace(err));
280
291
 
281
292
  throw YError.cast(err as Error, 'E_OAUTH2');
282
293
  }
@@ -33,6 +33,7 @@ exports[`OAuth2CodeGranter should work with a complete valid flow 2`] = `
33
33
  },
34
34
  "yolo",
35
35
  "https://www.example.com/oauth2/code",
36
+ "",
36
37
  ],
37
38
  ],
38
39
  "oAuth2CodeCreateCalls": [
@@ -42,6 +43,8 @@ exports[`OAuth2CodeGranter should work with a complete valid flow 2`] = `
42
43
  "scope": "user",
43
44
  },
44
45
  "https://www.example.com/oauth2/code",
46
+ "",
47
+ "plain",
45
48
  {},
46
49
  ],
47
50
  ],
@@ -1,11 +1,11 @@
1
1
  import { describe, test, expect } from '@jest/globals';
2
2
  import initAuthCookies from './authCookies.js';
3
- import { type AuthCookiesConfig } from './authCookies.js';
3
+ import { type AuthCookiesOptions } 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: AuthCookiesConfig['COOKIES'] = {
8
+ const COOKIES: AuthCookiesOptions = {
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: AuthCookiesConfig['COOKIES'] = {
30
+ const COOKIES: AuthCookiesOptions = {
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: AuthCookiesConfig['COOKIES'] = {
54
+ const COOKIES: AuthCookiesOptions = {
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: AuthCookiesConfig['COOKIES'] = {
68
+ const COOKIES: AuthCookiesOptions = {
69
69
  domain: 'api.example.com',
70
70
  };
71
71
 
@@ -6,29 +6,33 @@ import { type Jsonify } from 'type-fest';
6
6
 
7
7
  export const AUTH_API_PREFIX = '/auth';
8
8
 
9
- export type AuthHandlersConfig = {
9
+ export interface AuthHandlersConfig {
10
10
  ROOT_AUTHENTICATION_DATA: WhookAuthenticationData;
11
- };
11
+ }
12
+
13
+ export type AuthCookiesOptions = Jsonify<
14
+ Omit<SerializeOptions, 'maxAge' | 'path' | 'expires'>
15
+ >;
12
16
 
13
- export type AuthCookiesConfig = {
14
- COOKIES: Jsonify<Omit<SerializeOptions, 'maxAge' | 'path' | 'expires'>>;
17
+ export interface AuthCookiesConfig {
18
+ COOKIES: AuthCookiesOptions;
15
19
  BASE_PATH?: string;
16
- };
20
+ }
17
21
 
18
22
  export type AuthCookiesDependencies = AuthCookiesConfig;
19
23
 
20
- export type AuthCookiesData = {
24
+ export interface AuthCookiesData {
21
25
  refresh_token: string;
22
26
  access_token: string;
23
- };
27
+ }
24
28
 
25
- export type AuthCookiesService = {
29
+ export interface AuthCookiesService {
26
30
  build: (
27
31
  data?: Partial<AuthCookiesData>,
28
32
  options?: { session: boolean },
29
33
  ) => string[];
30
34
  parse: (content: string) => Partial<AuthCookiesData>;
31
- };
35
+ }
32
36
 
33
37
  export default location(autoService(initAuthCookies), import.meta.url);
34
38
 
@@ -9,17 +9,18 @@ import {
9
9
  import { type WhookAuthenticationData } from '@whook/authorization';
10
10
  import { type OAuth2Options } from '../services/oAuth2Granters.js';
11
11
 
12
- export type OAuth2ClientCredentialsGranterDependencies = {
12
+ export interface OAuth2ClientCredentialsGranterDependencies {
13
13
  OAUTH2: OAuth2Options;
14
14
  checkApplication: CheckApplicationService;
15
15
  log?: LogService;
16
- };
17
- export type OAuth2ClientCredentialsGranterParameters = {
16
+ }
17
+ export interface OAuth2ClientCredentialsGranterParameters {
18
18
  username: string;
19
19
  password: string;
20
20
  scope?: WhookAuthenticationData['scope'];
21
- };
21
+ }
22
22
  export type OAuth2ClientCredentialsGranterService = OAuth2GranterService<
23
+ Record<string, unknown>,
23
24
  Record<string, unknown>,
24
25
  Record<string, unknown>,
25
26
  OAuth2ClientCredentialsGranterParameters
@@ -1,15 +1,16 @@
1
1
  import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
2
  import { type WhookAuthenticationData } from '@whook/authorization';
3
- import initOAuth2CodeGranter from './oAuth2CodeGranter.js';
4
- import {
5
- type CheckApplicationService,
3
+ import { type CheckApplicationService } from './oAuth2Granters.js';
4
+ import initOAuth2CodeGranter, {
5
+ base64UrlEncode,
6
+ hashCodeVerifier,
6
7
  type OAuth2CodeService,
7
- } from './oAuth2Granters.js';
8
+ } from './oAuth2CodeGranter.js';
8
9
 
9
10
  describe('OAuth2CodeGranter', () => {
10
11
  const oAuth2Code = {
11
- create: jest.fn<OAuth2CodeService<string>['create']>(),
12
- check: jest.fn<OAuth2CodeService<string>['check']>(),
12
+ create: jest.fn<OAuth2CodeService['create']>(),
13
+ check: jest.fn<OAuth2CodeService['check']>(),
13
14
  };
14
15
  const checkApplication = jest.fn<CheckApplicationService>();
15
16
  const log = jest.fn();
@@ -44,11 +45,17 @@ describe('OAuth2CodeGranter', () => {
44
45
  [name: string]: unknown;
45
46
  });
46
47
 
47
- const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize({
48
- clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
49
- redirectURI: 'https://www.example.com/oauth2/code',
50
- scope: 'user',
51
- });
48
+ const authorizerResult = await oAuth2CodeGranter.authorizer?.authorize(
49
+ {
50
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
51
+ redirectURI: 'https://www.example.com/oauth2/code',
52
+ scope: 'user',
53
+ },
54
+ {
55
+ codeChallenge: '',
56
+ codeChallengeMethod: 'plain',
57
+ },
58
+ );
52
59
  const acknowledgerResult =
53
60
  await oAuth2CodeGranter.acknowledger?.acknowledge(
54
61
  {
@@ -60,7 +67,10 @@ describe('OAuth2CodeGranter', () => {
60
67
  redirectURI: 'https://www.example.com/oauth2/code',
61
68
  scope: 'user',
62
69
  },
63
- {},
70
+ {
71
+ codeChallenge: '',
72
+ codeChallengeMethod: 'plain',
73
+ },
64
74
  );
65
75
  const authenticatorResult =
66
76
  await oAuth2CodeGranter.authenticator?.authenticate(
@@ -68,6 +78,7 @@ describe('OAuth2CodeGranter', () => {
68
78
  clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
69
79
  redirectURI: 'https://www.example.com/oauth2/code',
70
80
  code: 'yolo',
81
+ codeVerifier: '',
71
82
  },
72
83
  {
73
84
  applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
@@ -80,24 +91,29 @@ describe('OAuth2CodeGranter', () => {
80
91
  acknowledgerResult,
81
92
  authenticatorResult,
82
93
  }).toMatchInlineSnapshot(`
83
- {
84
- "acknowledgerResult": {
85
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
86
- "code": "yolo",
87
- "redirectURI": "https://www.example.com/oauth2/code",
88
- "scope": "user",
89
- },
90
- "authenticatorResult": {
91
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
92
- "redirectURI": "https://www.example2.com",
93
- "scope": "user",
94
- },
95
- "authorizerResult": {
96
- "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
97
- "redirectURI": "https://www.example.com",
98
- "scope": "user",
99
- },
100
- }
94
+ {
95
+ "acknowledgerResult": {
96
+ "acknowledgedData": {
97
+ "code": "yolo",
98
+ },
99
+ "additionalParameters": {},
100
+ "authenticationData": {
101
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
102
+ "scope": "user",
103
+ },
104
+ "redirectURI": "https://www.example.com/oauth2/code",
105
+ },
106
+ "authenticatorResult": {
107
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
108
+ "redirectURI": "https://www.example2.com",
109
+ "scope": "user",
110
+ },
111
+ "authorizerResult": {
112
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
113
+ "redirectURI": "https://www.example.com",
114
+ "scope": "user",
115
+ },
116
+ }
101
117
  `);
102
118
  expect({
103
119
  oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
@@ -107,3 +123,54 @@ describe('OAuth2CodeGranter', () => {
107
123
  }).toMatchSnapshot();
108
124
  });
109
125
  });
126
+
127
+ describe('base64UrlEncode()', () => {
128
+ test('should work like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
129
+ expect(
130
+ base64UrlEncode(
131
+ Buffer.from([
132
+ 116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187,
133
+ 186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141,
134
+ 121,
135
+ ]),
136
+ ),
137
+ ).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
138
+ });
139
+ });
140
+
141
+ describe('base64UrlEncode()', () => {
142
+ test('should work with plain method', () => {
143
+ expect(
144
+ hashCodeVerifier(
145
+ Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
146
+ 'plain',
147
+ ),
148
+ ).toEqual(Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'));
149
+ });
150
+
151
+ test('should work with S256 like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
152
+ expect(
153
+ hashCodeVerifier(
154
+ Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
155
+ 'S256',
156
+ ),
157
+ ).toEqual(
158
+ Buffer.from([
159
+ 19, 211, 30, 150, 26, 26, 216, 236, 47, 22, 177, 12, 76, 152, 46, 8,
160
+ 118, 168, 120, 173, 109, 241, 68, 86, 110, 225, 137, 74, 203, 112, 249,
161
+ 195,
162
+ ]),
163
+ );
164
+ });
165
+
166
+ test('should work base64 url encode like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
167
+ expect(
168
+ base64UrlEncode(
169
+ hashCodeVerifier(
170
+ Buffer.from('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'),
171
+ 'S256',
172
+ ),
173
+ ),
174
+ ).toEqual('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
175
+ });
176
+ });
@@ -4,26 +4,71 @@ import { YError } from 'yerror';
4
4
  import { type LogService } from 'common-services';
5
5
  import {
6
6
  type OAuth2GranterService,
7
- type OAuth2CodeService,
8
7
  type CheckApplicationService,
9
8
  } from './oAuth2Granters.js';
9
+ import { createHash } from 'crypto';
10
+ import { type WhookAuthenticationData } from '@whook/authorization';
10
11
 
11
12
  export const CODE_GRANTER_TYPE = 'code';
13
+ export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
14
+ export const S256_CODE_CHALLENGE_METHOD = 'S256';
15
+ export const CODE_CHALLENGE_METHODS = [
16
+ PLAIN_CODE_CHALLENGE_METHOD,
17
+ S256_CODE_CHALLENGE_METHOD,
18
+ ] as const;
12
19
 
13
- export type OAuth2CodeGranterDependencies = {
20
+ export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
21
+
22
+ export interface OAuth2CodeService<
23
+ P extends object = Record<string, unknown>,
24
+ C extends string = string,
25
+ > {
26
+ create: (
27
+ authenticationData: WhookAuthenticationData,
28
+ redirectURI: string,
29
+ codeChallenge: string | undefined,
30
+ codeChallengeMethod: CodeChallengeMethod | undefined,
31
+ additionalParameters: P,
32
+ ) => Promise<C>;
33
+ check: (
34
+ authenticationData: WhookAuthenticationData,
35
+ code: C,
36
+ redirectURI: string,
37
+ codeVerifier: string | undefined,
38
+ ) => Promise<
39
+ WhookAuthenticationData & {
40
+ redirectURI: string;
41
+ } & P
42
+ >;
43
+ }
44
+
45
+ export interface OAuth2CodeGranterDependencies {
14
46
  oAuth2Code: OAuth2CodeService;
15
47
  checkApplication: CheckApplicationService;
16
48
  log?: LogService;
17
- };
18
- export type OAuth2CodeGranterParameters = {
49
+ }
50
+ export interface OAuth2CodeGranterAuthorizeParameters {
51
+ codeChallenge: string;
52
+ codeChallengeMethod: CodeChallengeMethod;
53
+ }
54
+ export interface OAuth2CodeGranterAcknowledgedData {
55
+ code: string;
56
+ }
57
+ export interface OAuth2CodeGranterAcknowledgeParameters {
58
+ codeChallenge: string;
59
+ codeChallengeMethod: CodeChallengeMethod;
60
+ }
61
+ export interface OAuth2CodeGranterGrantParameters {
19
62
  code: string;
20
63
  redirectURI: string;
21
64
  clientId: string;
22
- };
65
+ codeVerifier: string;
66
+ }
23
67
  export type OAuth2CodeGranterService = OAuth2GranterService<
24
- Record<string, unknown>,
25
- Record<string, unknown>,
26
- OAuth2CodeGranterParameters
68
+ OAuth2CodeGranterAuthorizeParameters,
69
+ OAuth2CodeGranterAcknowledgedData,
70
+ OAuth2CodeGranterAcknowledgeParameters,
71
+ OAuth2CodeGranterGrantParameters
27
72
  >;
28
73
 
29
74
  export default location(autoService(initOAuth2CodeGranter), import.meta.url);
@@ -59,28 +104,34 @@ async function initOAuth2CodeGranter({
59
104
  >['acknowledge'] = async (
60
105
  authenticationData,
61
106
  { clientId, redirectURI, scope },
62
- additionalParameters,
107
+ { codeChallenge, codeChallengeMethod, ...additionalParameters },
63
108
  ) => {
64
109
  const code = await oAuth2Code.create(
65
110
  { ...authenticationData, applicationId: clientId, scope },
66
111
  redirectURI,
112
+ codeChallenge,
113
+ codeChallengeMethod,
67
114
  additionalParameters,
68
115
  );
69
116
 
70
117
  return {
71
- // TODO: check a way to avoid this by adding params
72
- ...authenticationData,
73
- applicationId: clientId,
118
+ authenticationData: {
119
+ ...authenticationData,
120
+ applicationId: clientId,
121
+ scope,
122
+ },
74
123
  redirectURI,
75
- scope,
76
- code,
124
+ acknowledgedData: {
125
+ code,
126
+ },
127
+ additionalParameters,
77
128
  };
78
129
  };
79
130
 
80
131
  const authenticateWithCode: NonNullable<
81
132
  OAuth2CodeGranterService['authenticator']
82
133
  >['authenticate'] = async (
83
- { code, clientId, redirectURI },
134
+ { code, clientId, redirectURI, codeVerifier },
84
135
  authenticationData,
85
136
  ) => {
86
137
  // The client must be authenticated (for now, see below)
@@ -111,6 +162,7 @@ async function initOAuth2CodeGranter({
111
162
  authenticationData,
112
163
  code,
113
164
  redirectURI,
165
+ codeVerifier,
114
166
  );
115
167
 
116
168
  return newAuthenticationData;
@@ -134,3 +186,25 @@ async function initOAuth2CodeGranter({
134
186
  },
135
187
  };
136
188
  }
189
+
190
+ // See https://tools.ietf.org/html/rfc7636#appendix-A
191
+ export function base64UrlEncode(buf: Buffer): string {
192
+ let s = buf.toString('base64');
193
+ s = s.split('=')[0];
194
+ s = s.replaceAll('+', '-');
195
+ s = s.replaceAll('/', '_');
196
+ return s;
197
+ }
198
+
199
+ export function hashCodeVerifier(
200
+ codeVerifier: Buffer,
201
+ method: CodeChallengeMethod,
202
+ ): Buffer {
203
+ if (PLAIN_CODE_CHALLENGE_METHOD === method) {
204
+ return codeVerifier;
205
+ }
206
+ if (S256_CODE_CHALLENGE_METHOD === method) {
207
+ return createHash('sha256').update(codeVerifier).digest();
208
+ }
209
+ throw new YError('E_UNSUPPORTED_CODE_CHALLENGE_METHOD', [method]);
210
+ }