@whook/oauth2 25.2.0 → 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 +20 -9
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +38 -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 +20 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +54 -38
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +137 -72
  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 +58 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +26 -11
  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 +169 -110
  138. package/src/routes/postOAuth2Acknowledge.ts +101 -61
  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,54 @@
1
+ import { type WhookAuthenticationScope } from '@whook/authorization';
2
+ import { identity } from '@whook/whook';
1
3
  import { type OpenAPI, PATH_ITEM_METHODS } from 'ya-open-api-types';
4
+ import { YError } from 'yerror';
2
5
 
6
+ const OAUTH2_SCOPES_SEPARATOR = ' ';
7
+
8
+ /**
9
+ * Splits a scope into its scope tokens
10
+ */
11
+ export function parseOAuth2Scope(oAuth2Scope: string): string[] {
12
+ const scopes = [
13
+ ...new Set(oAuth2Scope.split(OAUTH2_SCOPES_SEPARATOR).filter(identity)),
14
+ ];
15
+
16
+ return scopes;
17
+ }
18
+
19
+ /**
20
+ * Join scopes into scope tokens
21
+ */
22
+ export function stringifyScopes(scopes: WhookAuthenticationScope[]): string {
23
+ return scopes.join(OAUTH2_SCOPES_SEPARATOR);
24
+ }
25
+
26
+ /**
27
+ * Filter scopes tokens to become valid ones
28
+ */
29
+ export function filterScopes(
30
+ scopes: string[],
31
+ allowedScopes: WhookAuthenticationScope[],
32
+ strict: boolean,
33
+ ): WhookAuthenticationScope[] {
34
+ const filteredScopes: WhookAuthenticationScope[] = [];
35
+
36
+ for (const scope of scopes) {
37
+ if (allowedScopes.includes(scope)) {
38
+ filteredScopes.push(scope);
39
+ continue;
40
+ }
41
+ if (strict) {
42
+ throw new YError('E_OAUTH2_BAD_SCOPE', [scope]);
43
+ }
44
+ }
45
+
46
+ return filteredScopes;
47
+ }
48
+
49
+ /**
50
+ * Collects all scopes tokens from an Open API
51
+ */
3
52
  export function collectScopesFromAPI(API: OpenAPI) {
4
53
  const scopes: string[] = [];
5
54
 
@@ -0,0 +1,24 @@
1
+ import camelCase from 'camelcase';
2
+
3
+ // The OAuth2 standard uses snake case names so we are
4
+ // converting them to the project standards asap
5
+ export function camelCaseObjectProperties<T>(
6
+ object: Record<string, T>,
7
+ ): Record<string, T> {
8
+ return Object.keys(object).reduce(
9
+ (camelCasedObject, key) => {
10
+ const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
11
+
12
+ camelCasedObject[newKey] = object[key];
13
+ return camelCasedObject;
14
+ },
15
+ {} as Record<string, T>,
16
+ );
17
+ }
18
+
19
+ export function snakeCase(s: string): string {
20
+ return s
21
+ .split(/(?=(?<![A-Z])[A-Z])|[^a-zA-Z]+/)
22
+ .map((s) => s.toLowerCase())
23
+ .join('_');
24
+ }
@@ -0,0 +1,37 @@
1
+ import { describe, test, expect } from '@jest/globals';
2
+ import { base64UrlEncode, computeCodeChallenge } from './verifier.js';
3
+
4
+ describe('base64UrlEncode()', () => {
5
+ test('should work like here https://tools.ietf.org/html/rfc7636#appendix-A', () => {
6
+ expect(
7
+ base64UrlEncode(
8
+ Buffer.from([
9
+ 116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187,
10
+ 186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141,
11
+ 121,
12
+ ]),
13
+ ),
14
+ ).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
15
+ });
16
+ });
17
+
18
+ describe('computeCodeChallenge()', () => {
19
+ test('should work with plain method', () => {
20
+ expect(
21
+ computeCodeChallenge(
22
+ 'plain',
23
+ 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk',
24
+ ),
25
+ ).toEqual('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk');
26
+ });
27
+
28
+ // See https://tools.ietf.org/html/rfc7636#appendix-A
29
+ test('should work like for the spec example', () => {
30
+ expect(
31
+ computeCodeChallenge(
32
+ 'S256',
33
+ 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk',
34
+ ),
35
+ ).toEqual('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
36
+ });
37
+ });
@@ -0,0 +1,57 @@
1
+ import { createHash } from 'crypto';
2
+ import { YError } from 'yerror';
3
+
4
+ export type CodeChallengeMethod = (typeof CODE_CHALLENGE_METHODS)[number];
5
+ export const PLAIN_CODE_CHALLENGE_METHOD = 'plain';
6
+ export const S256_CODE_CHALLENGE_METHOD = 'S256';
7
+
8
+ export const CODE_CHALLENGE_METHODS = [
9
+ PLAIN_CODE_CHALLENGE_METHOD,
10
+ S256_CODE_CHALLENGE_METHOD,
11
+ ] as const;
12
+
13
+ // See https://tools.ietf.org/html/rfc7636#appendix-A
14
+ export function base64UrlEncode(buf: Buffer): string {
15
+ let s = buf.toString('base64');
16
+ s = s.split('=')[0];
17
+ s = s.replaceAll('+', '-');
18
+ s = s.replaceAll('/', '_');
19
+ return s;
20
+ }
21
+
22
+ export function computeCodeChallenge(
23
+ codeChallengeMethod: CodeChallengeMethod,
24
+ codeVerifier: string,
25
+ ): string {
26
+ if (PLAIN_CODE_CHALLENGE_METHOD === codeChallengeMethod) {
27
+ return codeVerifier;
28
+ }
29
+
30
+ if (S256_CODE_CHALLENGE_METHOD === codeChallengeMethod) {
31
+ return base64UrlEncode(
32
+ createHash('sha256').update(Buffer.from(codeVerifier)).digest(),
33
+ );
34
+ }
35
+
36
+ throw new YError('E_OAUTH2_UNSUPPORTED_CODE_CHALLENGE_METHOD', [
37
+ codeChallengeMethod,
38
+ ]);
39
+ }
40
+
41
+ export function checkCodeChallenge(
42
+ codeChallengeMethod: CodeChallengeMethod,
43
+ codeChallenge: string,
44
+ codeVerifier: string,
45
+ ) {
46
+ const computedCodeChallenge = computeCodeChallenge(
47
+ codeChallengeMethod,
48
+ codeVerifier,
49
+ );
50
+
51
+ if (computedCodeChallenge !== codeChallenge) {
52
+ throw new YError('E_OAUTH2_BAD_CODE_VERIFIER', [
53
+ codeChallenge,
54
+ computedCodeChallenge,
55
+ ]);
56
+ }
57
+ }
@@ -2,16 +2,22 @@
2
2
  import { describe, test, beforeEach, jest, expect } from '@jest/globals';
3
3
  import initGetOAuth2Authorize from './getOAuth2Authorize.js';
4
4
  import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
5
- import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
5
+ import { type WhookOAuth2GranterDefinitions } from '../services/oAuth2Granters.js';
6
6
  import { YError } from 'yerror';
7
- import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
8
- import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
7
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
8
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
9
9
  import { type LogService } from 'common-services';
10
- import { type OAuth2Options, type OAuth2GranterService } from '../index.js';
10
+ import {
11
+ type WhookOAuth2Options,
12
+ type WhookOAuth2GranterService,
13
+ } from '../index.js';
14
+ import { OAUTH2_ERRORS_DESCRIPTORS } from '../libs/errors.js';
11
15
 
12
16
  describe('getOAuth2Authorize', () => {
13
- const OAUTH2: OAuth2Options = {
17
+ const OAUTH2: WhookOAuth2Options = {
18
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
14
19
  authenticateURL: 'https://auth.example.com/sign_in',
20
+ allowedScopes: ['user'],
15
21
  };
16
22
  const ERRORS_DESCRIPTORS = {
17
23
  ...DEFAULT_ERRORS_DESCRIPTORS,
@@ -19,77 +25,62 @@ describe('getOAuth2Authorize', () => {
19
25
  };
20
26
  const log = jest.fn<LogService>();
21
27
  const codeGranter = {
22
- type: 'code',
23
- authorizer: {
24
- responseType: 'code',
25
- authorize:
26
- jest.fn<
27
- NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
28
- >(),
29
- },
30
- acknowledger: {
31
- acknowledgmentType: 'code',
32
- acknowledge:
33
- jest.fn<
34
- NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
35
- >(),
36
- },
37
- authenticator: {
38
- grantType: 'authorization_code',
39
- authenticate:
40
- jest.fn<
41
- NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
42
- >(),
43
- },
44
- };
28
+ grantType: 'authorization_code',
29
+ responseType: 'code',
30
+ issuesRefreshToken: true,
31
+ authorize:
32
+ jest.fn<
33
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
34
+ >(),
35
+ acknowledge:
36
+ jest.fn<
37
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
38
+ >(),
39
+ authenticate:
40
+ jest.fn<
41
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
42
+ >(),
43
+ } satisfies WhookOAuth2AuthorizationCodeGranterService;
45
44
  const tokenGranter = {
46
- type: 'token',
47
- authorizer: {
48
- responseType: 'token',
49
- authorize:
50
- jest.fn<
51
- NonNullable<
52
- NonNullable<OAuth2TokenGranterService['authorizer']>['authorize']
53
- >
54
- >(),
55
- },
56
- acknowledger: {
57
- acknowledgmentType: 'token',
58
- acknowledge:
59
- jest.fn<
60
- NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
61
- >(),
62
- },
63
- };
45
+ grantType: 'implicit',
46
+ responseType: 'token',
47
+ issuesRefreshToken: false,
48
+ authorize:
49
+ jest.fn<
50
+ NonNullable<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>
51
+ >(),
52
+ acknowledge:
53
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
54
+ } satisfies WhookOAuth2ImplicitGranterService;
64
55
  const oAuth2Granters = [
65
56
  codeGranter,
66
57
  tokenGranter,
67
- ] as unknown as OAuth2GranterService[];
58
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
68
59
 
69
60
  beforeEach(() => {
70
61
  log.mockReset();
71
62
  [
72
- codeGranter.authorizer.authorize,
73
- codeGranter.acknowledger.acknowledge,
74
- codeGranter.authenticator.authenticate,
75
- tokenGranter.authorizer.authorize,
76
- tokenGranter.acknowledger.acknowledge,
63
+ codeGranter.authorize,
64
+ codeGranter.acknowledge,
65
+ codeGranter.authenticate,
66
+ tokenGranter.authorize,
67
+ tokenGranter.acknowledge,
77
68
  ].forEach((mock) => mock.mockReset());
78
69
  });
79
70
 
80
71
  test('should redirect', async () => {
81
72
  [
82
- codeGranter.acknowledger.acknowledge,
83
- codeGranter.authenticator.authenticate,
84
- tokenGranter.authorizer.authorize,
85
- tokenGranter.acknowledger.acknowledge,
73
+ codeGranter.acknowledge,
74
+ codeGranter.authenticate,
75
+ tokenGranter.authorize,
76
+ tokenGranter.acknowledge,
86
77
  ].forEach((mock: any) =>
87
78
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
88
79
  );
89
- codeGranter.authorizer.authorize.mockResolvedValueOnce({
90
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
80
+ codeGranter.authorize.mockResolvedValueOnce({
81
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
91
82
  redirectURI: 'http://lol',
92
- scope: 'user',
83
+ scopes: ['user'],
93
84
  });
94
85
 
95
86
  const getOAuth2Authorize = await initGetOAuth2Authorize({
@@ -108,36 +99,53 @@ describe('getOAuth2Authorize', () => {
108
99
  },
109
100
  });
110
101
 
111
- expect(response).toMatchInlineSnapshot(`
112
- {
113
- "headers": {
114
- "location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
115
- },
116
- "status": 302,
117
- }
118
- `);
119
102
  expect({
103
+ response,
120
104
  logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
121
- codeGranterAuthorizerAuthorizeCalls:
122
- codeGranter.authorizer.authorize.mock.calls,
105
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
123
106
  codeGranterAcknowledgerAcknowledgeCalls:
124
- codeGranter.acknowledger.acknowledge.mock.calls,
107
+ codeGranter.acknowledge.mock.calls,
125
108
  codeGranterAuthenticatorAuthenticateCalls:
126
- codeGranter.authenticator.authenticate.mock.calls,
127
- tokenGranterAuthorizerAuthorizeCalls:
128
- tokenGranter.authorizer.authorize.mock.calls,
109
+ codeGranter.authenticate.mock.calls,
110
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
129
111
  tokenGranterAcknowledgerAcknowledgeCalls:
130
- tokenGranter.acknowledger.acknowledge.mock.calls,
131
- }).toMatchSnapshot();
112
+ tokenGranter.acknowledge.mock.calls,
113
+ }).toMatchInlineSnapshot(`
114
+ {
115
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
116
+ "codeGranterAuthenticatorAuthenticateCalls": [],
117
+ "codeGranterAuthorizerAuthorizeCalls": [
118
+ [
119
+ {
120
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
121
+ "demandedRedirectURI": "https://www.example.com",
122
+ "demandedScopes": [
123
+ "user",
124
+ ],
125
+ },
126
+ {},
127
+ ],
128
+ ],
129
+ "logCalls": [],
130
+ "response": {
131
+ "headers": {
132
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
133
+ },
134
+ "status": 302,
135
+ },
136
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
137
+ "tokenGranterAuthorizerAuthorizeCalls": [],
138
+ }
139
+ `);
132
140
  });
133
141
 
134
142
  test('should redirect with an error when some', async () => {
135
143
  [
136
- codeGranter.authorizer.authorize,
137
- codeGranter.acknowledger.acknowledge,
138
- codeGranter.authenticator.authenticate,
139
- tokenGranter.authorizer.authorize,
140
- tokenGranter.acknowledger.acknowledge,
144
+ codeGranter.authorize,
145
+ codeGranter.acknowledge,
146
+ codeGranter.authenticate,
147
+ tokenGranter.authorize,
148
+ tokenGranter.acknowledge,
141
149
  ].forEach((mock: any) =>
142
150
  mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
143
151
  );
@@ -158,26 +166,101 @@ describe('getOAuth2Authorize', () => {
158
166
  },
159
167
  });
160
168
 
161
- expect(response).toMatchInlineSnapshot(`
162
- {
163
- "headers": {
164
- "location": "https://auth.example.com/sign_in?redirect_uri=https%3A%2F%2Fwww.example.com&error=unsupported_response_type&error_description=The+type+%22yolo%22+is+not+supported.&state=bancal",
165
- },
166
- "status": 302,
167
- }
168
- `);
169
169
  expect({
170
+ response,
170
171
  logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
171
- codeGranterAuthorizerAuthorizeCalls:
172
- codeGranter.authorizer.authorize.mock.calls,
172
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
173
173
  codeGranterAcknowledgerAcknowledgeCalls:
174
- codeGranter.acknowledger.acknowledge.mock.calls,
174
+ codeGranter.acknowledge.mock.calls,
175
175
  codeGranterAuthenticatorAuthenticateCalls:
176
- codeGranter.authenticator.authenticate.mock.calls,
177
- tokenGranterAuthorizerAuthorizeCalls:
178
- tokenGranter.authorizer.authorize.mock.calls,
176
+ codeGranter.authenticate.mock.calls,
177
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
179
178
  tokenGranterAcknowledgerAcknowledgeCalls:
180
- tokenGranter.acknowledger.acknowledge.mock.calls,
181
- }).toMatchSnapshot();
179
+ tokenGranter.acknowledge.mock.calls,
180
+ }).toMatchInlineSnapshot(`
181
+ {
182
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
183
+ "codeGranterAuthenticatorAuthenticateCalls": [],
184
+ "codeGranterAuthorizerAuthorizeCalls": [],
185
+ "logCalls": [
186
+ [
187
+ "debug",
188
+ "👫 - OAuth2 authorize error.",
189
+ ],
190
+ ],
191
+ "response": {
192
+ "headers": {
193
+ "location": "https://www.example.com/?error=unsupported_response_type&error_description=The+response+type+%22yolo%22+is+not+supported.&state=bancal",
194
+ },
195
+ "status": 302,
196
+ },
197
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
198
+ "tokenGranterAuthorizerAuthorizeCalls": [],
199
+ }
200
+ `);
201
+ });
202
+
203
+ test('should redirect with an error with bad scopes', async () => {
204
+ [
205
+ codeGranter.authorize,
206
+ codeGranter.acknowledge,
207
+ codeGranter.authenticate,
208
+ tokenGranter.authorize,
209
+ tokenGranter.acknowledge,
210
+ ].forEach((mock: any) =>
211
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
212
+ );
213
+
214
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
215
+ OAUTH2: {
216
+ ...OAUTH2,
217
+ strictScopesChecks: true,
218
+ },
219
+ ERRORS_DESCRIPTORS,
220
+ oAuth2Granters,
221
+ log,
222
+ });
223
+ const response = await getOAuth2Authorize({
224
+ query: {
225
+ response_type: 'code',
226
+ client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
227
+ redirect_uri: 'https://www.example.com',
228
+ scope: 'god',
229
+ state: 'bancal',
230
+ },
231
+ });
232
+
233
+ expect({
234
+ response,
235
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
236
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
237
+ codeGranterAcknowledgerAcknowledgeCalls:
238
+ codeGranter.acknowledge.mock.calls,
239
+ codeGranterAuthenticatorAuthenticateCalls:
240
+ codeGranter.authenticate.mock.calls,
241
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
242
+ tokenGranterAcknowledgerAcknowledgeCalls:
243
+ tokenGranter.acknowledge.mock.calls,
244
+ }).toMatchInlineSnapshot(`
245
+ {
246
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
247
+ "codeGranterAuthenticatorAuthenticateCalls": [],
248
+ "codeGranterAuthorizerAuthorizeCalls": [],
249
+ "logCalls": [
250
+ [
251
+ "debug",
252
+ "👫 - OAuth2 authorize error.",
253
+ ],
254
+ ],
255
+ "response": {
256
+ "headers": {
257
+ "location": "https://www.example.com/?error=invalid_scope&error_description=This+scope+is+not+supported+%28god%29.&state=bancal",
258
+ },
259
+ "status": 302,
260
+ },
261
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
262
+ "tokenGranterAuthorizerAuthorizeCalls": [],
263
+ }
264
+ `);
182
265
  });
183
266
  });