@whook/oauth2 25.2.1 → 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 +15 -8
  40. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  41. package/dist/routes/getOAuth2WellKnownMetadata.test.js +37 -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 +11 -10
  62. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  63. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  64. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  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 +57 -60
  129. package/src/routes/getOAuth2WellKnownMetadata.ts +21 -10
  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 +167 -108
  138. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  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
@@ -0,0 +1,163 @@
1
+ import { autoService, location } from 'knifecycle';
2
+ import { noop } from '@whook/whook';
3
+ import { type LogService, type TimeService } from 'common-services';
4
+ import {
5
+ type WhookOAuth2GranterService,
6
+ type WhookOAuth2ReadClientGrantsService,
7
+ type WhookOAuth2Options,
8
+ type WhookOAuth2GranterDefinitions,
9
+ } from './oAuth2Granters.js';
10
+ import { YError } from 'yerror';
11
+ import { checkGrantType } from '../libs/grants.js';
12
+ import { filterScopes } from '../libs/scopes.js';
13
+ import { checkRedirectURI } from '../libs/redirectURI.js';
14
+ import { type WhookAuthenticationData } from '@whook/authorization';
15
+
16
+ export const IMPLICIT_GRANT_TYPE = 'implicit';
17
+ export const IMPLICIT_RESPONSE_TYPE = 'token';
18
+
19
+ /**
20
+ * A type allowing to override the access token types
21
+ */
22
+ export type WhookOAuth2AccessToken = string;
23
+
24
+ /**
25
+ * A service to create and check access tokens
26
+ */
27
+ export interface WhookOAuth2AccessTokenService<
28
+ T extends string = WhookOAuth2AccessToken,
29
+ > {
30
+ create: (authenticationData: WhookAuthenticationData) => Promise<{
31
+ token: T;
32
+ expiresAt: number;
33
+ }>;
34
+ check: (token: T) => Promise<WhookAuthenticationData>;
35
+ }
36
+
37
+ export interface WhookOAuth2ImplicitGranterDependencies {
38
+ OAUTH2: WhookOAuth2Options;
39
+ readClientGrants: WhookOAuth2ReadClientGrantsService;
40
+ oAuth2AccessToken: Pick<WhookOAuth2AccessTokenService, 'create'>;
41
+ time?: TimeService;
42
+ log?: LogService;
43
+ }
44
+ export interface WhookOAuth2ImplicitGranterDefinitions extends WhookOAuth2GranterDefinitions {
45
+ grantType: typeof IMPLICIT_GRANT_TYPE;
46
+ responseType: typeof IMPLICIT_RESPONSE_TYPE;
47
+ acknowledgedData: {
48
+ accessToken: string;
49
+ tokenType: 'bearer';
50
+ expiresIn: number;
51
+ };
52
+ }
53
+
54
+ export type WhookOAuth2ImplicitGranterService =
55
+ WhookOAuth2GranterService<WhookOAuth2ImplicitGranterDefinitions>;
56
+
57
+ // Implicit Grant
58
+ // https://tools.ietf.org/html/rfc6749#section-4.2
59
+ async function initOAuth2ImplicitGranter({
60
+ OAUTH2,
61
+ readClientGrants,
62
+ oAuth2AccessToken,
63
+ time = Date.now.bind(Date),
64
+ log = noop,
65
+ }: WhookOAuth2ImplicitGranterDependencies): Promise<WhookOAuth2ImplicitGranterService> {
66
+ log(
67
+ 'warning',
68
+ `⚠️ - Using the token flow is deprecated and not recommended.`,
69
+ );
70
+
71
+ const authorizeWithToken: NonNullable<
72
+ WhookOAuth2ImplicitGranterService['authorize']
73
+ > = async ({ clientId, demandedRedirectURI, demandedScopes }) => {
74
+ const grants = await readClientGrants(clientId);
75
+
76
+ checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
77
+ if (demandedRedirectURI) {
78
+ checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
79
+ } else {
80
+ demandedRedirectURI = grants.allowedRedirectURIS[0];
81
+ }
82
+
83
+ const filteredScopes = filterScopes(
84
+ demandedScopes,
85
+ grants.allowedScopes,
86
+ !!OAUTH2.strictScopesChecks,
87
+ );
88
+
89
+ return {
90
+ ...grants.authenticationData,
91
+ clientId,
92
+ redirectURI: demandedRedirectURI,
93
+ scopes: filteredScopes,
94
+ };
95
+ };
96
+
97
+ // Access Token Response:
98
+ // https://tools.ietf.org/html/rfc6749#section-4.2.2
99
+ const acknowledgeWithToken: NonNullable<
100
+ WhookOAuth2ImplicitGranterService['acknowledge']
101
+ > = async (
102
+ userAuthenticationData,
103
+ { clientId, demandedRedirectURI, demandedScopes },
104
+ ) => {
105
+ const grants = await readClientGrants(clientId);
106
+
107
+ if (!grants.isPublicClient) {
108
+ throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
109
+ grants.authenticationData.clientId,
110
+ ]);
111
+ }
112
+
113
+ const filteredScopes = filterScopes(
114
+ filterScopes(
115
+ demandedScopes,
116
+ grants.allowedScopes,
117
+ !!OAUTH2.strictScopesChecks,
118
+ ),
119
+ userAuthenticationData.scopes,
120
+ !!OAUTH2.strictScopesChecks,
121
+ );
122
+
123
+ checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
124
+ checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
125
+
126
+ const { token: accessToken, expiresAt: accessTokenExpiresAt } =
127
+ await oAuth2AccessToken.create({
128
+ ...userAuthenticationData,
129
+ clientId,
130
+ scopes: filteredScopes,
131
+ });
132
+
133
+ return {
134
+ acknowledgedAuthenticationData: {
135
+ ...userAuthenticationData,
136
+ clientId,
137
+ scopes: filteredScopes,
138
+ },
139
+ acknowledgedRedirectURI: demandedRedirectURI,
140
+ acknowledgedScopes: filteredScopes,
141
+ acknowledgedData: {
142
+ accessToken,
143
+ tokenType: 'bearer',
144
+ expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
145
+ },
146
+ };
147
+ };
148
+
149
+ log('debug', '👫 - OAuth2ImplicitGranter Service Initialized!');
150
+
151
+ return {
152
+ grantType: IMPLICIT_GRANT_TYPE,
153
+ responseType: IMPLICIT_RESPONSE_TYPE,
154
+ issuesRefreshToken: false,
155
+ authorize: authorizeWithToken,
156
+ acknowledge: acknowledgeWithToken,
157
+ };
158
+ }
159
+
160
+ export default location(
161
+ autoService(initOAuth2ImplicitGranter),
162
+ import.meta.url,
163
+ );
@@ -0,0 +1,493 @@
1
+ import { describe, test, beforeEach, jest, expect } from '@jest/globals';
2
+ import {
3
+ type WhookOAuth2Options,
4
+ type WhookOAuth2ReadClientGrantsService,
5
+ } from './oAuth2Granters.js';
6
+ import initOAuth2PasswordGranter, {
7
+ type WhookOAuth2PasswordService,
8
+ } from './oAuth2PasswordGranter.js';
9
+ import { YError } from 'yerror';
10
+
11
+ describe('OAuth2PasswordGranter', () => {
12
+ const OAUTH2: WhookOAuth2Options = {
13
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
14
+ authenticateURL: 'https://auth.example.com/sign_in',
15
+ allowedScopes: ['user', 'admin', 'root'],
16
+ };
17
+ const oAuth2Password = {
18
+ check: jest.fn<WhookOAuth2PasswordService['check']>(),
19
+ };
20
+ const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
21
+ const log = jest.fn();
22
+
23
+ beforeEach(() => {
24
+ oAuth2Password.check.mockReset();
25
+ readClientGrants.mockReset();
26
+ log.mockReset();
27
+ });
28
+
29
+ test('should work with a complete valid flow', async () => {
30
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
31
+ OAUTH2,
32
+ readClientGrants,
33
+ oAuth2Password,
34
+ log,
35
+ });
36
+
37
+ readClientGrants.mockResolvedValue({
38
+ allowedRedirectURIS: ['https://www.example.com'],
39
+ allowedGrantTypes: ['password', 'authorization_code'],
40
+ allowedScopes: ['user', 'admin'],
41
+ authenticationData: {
42
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
43
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
44
+ scopes: [],
45
+ },
46
+ isPublicClient: false,
47
+ });
48
+ oAuth2Password.check.mockResolvedValueOnce({
49
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
50
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
51
+ scopes: [],
52
+ });
53
+
54
+ const authenticatorResult = await oAuth2PasswordGranter.authenticate?.(
55
+ {
56
+ username: 'a_username',
57
+ password: 'a_password',
58
+ demandedScopes: ['user', 'root'],
59
+ },
60
+ {
61
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
62
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
63
+ scopes: ['user'],
64
+ },
65
+ );
66
+
67
+ expect({
68
+ authenticatorResult,
69
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
70
+ readClientGrantsCalls: readClientGrants.mock.calls,
71
+ logCalls: log.mock.calls,
72
+ }).toMatchInlineSnapshot(`
73
+ {
74
+ "authenticatorResult": {
75
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
76
+ "scopes": [],
77
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
78
+ },
79
+ "logCalls": [
80
+ [
81
+ "warning",
82
+ "⚠️ - Using the password flow is deprecated and not recommended.",
83
+ ],
84
+ [
85
+ "debug",
86
+ "👫 - OAuth2PasswordGranter Service Initialized!",
87
+ ],
88
+ ],
89
+ "oAuth2PasswordCheckCalls": [
90
+ [
91
+ {
92
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
93
+ "scopes": [
94
+ "user",
95
+ ],
96
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
97
+ },
98
+ "a_username",
99
+ "a_password",
100
+ ],
101
+ ],
102
+ "readClientGrantsCalls": [
103
+ [
104
+ "abbacaca-abba-caca-abba-cacaabbacaca",
105
+ ],
106
+ ],
107
+ }
108
+ `);
109
+ });
110
+
111
+ test('should work with a complete valid flow and filter scopes', async () => {
112
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
113
+ OAUTH2,
114
+ readClientGrants,
115
+ oAuth2Password,
116
+ log,
117
+ });
118
+
119
+ readClientGrants.mockResolvedValue({
120
+ allowedRedirectURIS: ['https://www.example.com'],
121
+ allowedGrantTypes: ['password', 'authorization_code'],
122
+ allowedScopes: ['user', 'admin'],
123
+ authenticationData: {
124
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
125
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
126
+ scopes: [],
127
+ },
128
+ isPublicClient: false,
129
+ });
130
+ oAuth2Password.check.mockResolvedValueOnce({
131
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
132
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
133
+ scopes: ['user', 'oauth2'],
134
+ });
135
+
136
+ const authenticatorResult = await oAuth2PasswordGranter.authenticate?.(
137
+ {
138
+ username: 'a_username',
139
+ password: 'a_password',
140
+ demandedScopes: ['user', 'admin', 'oauth2', 'a_scope'],
141
+ },
142
+ {
143
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
144
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
145
+ scopes: ['user'],
146
+ },
147
+ );
148
+
149
+ expect({
150
+ authenticatorResult,
151
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
152
+ readClientGrantsCalls: readClientGrants.mock.calls,
153
+ logCalls: log.mock.calls,
154
+ }).toMatchInlineSnapshot(`
155
+ {
156
+ "authenticatorResult": {
157
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
158
+ "scopes": [
159
+ "user",
160
+ ],
161
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
162
+ },
163
+ "logCalls": [
164
+ [
165
+ "warning",
166
+ "⚠️ - Using the password flow is deprecated and not recommended.",
167
+ ],
168
+ [
169
+ "debug",
170
+ "👫 - OAuth2PasswordGranter Service Initialized!",
171
+ ],
172
+ ],
173
+ "oAuth2PasswordCheckCalls": [
174
+ [
175
+ {
176
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
177
+ "scopes": [
178
+ "user",
179
+ ],
180
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
181
+ },
182
+ "a_username",
183
+ "a_password",
184
+ ],
185
+ ],
186
+ "readClientGrantsCalls": [
187
+ [
188
+ "abbacaca-abba-caca-abba-cacaabbacaca",
189
+ ],
190
+ ],
191
+ }
192
+ `);
193
+ });
194
+
195
+ test('should fail with a secret client and no authentication', async () => {
196
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
197
+ OAUTH2,
198
+ readClientGrants,
199
+ oAuth2Password,
200
+ log,
201
+ });
202
+
203
+ readClientGrants.mockResolvedValue({
204
+ allowedRedirectURIS: ['https://www.example.com'],
205
+ allowedGrantTypes: ['password', 'authorization_code'],
206
+ allowedScopes: ['user', 'admin'],
207
+ authenticationData: {
208
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
209
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
210
+ scopes: [],
211
+ },
212
+ isPublicClient: false,
213
+ });
214
+ oAuth2Password.check.mockResolvedValueOnce({
215
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
216
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
217
+ scopes: [],
218
+ });
219
+
220
+ try {
221
+ await oAuth2PasswordGranter.authenticate?.({
222
+ username: 'a_username',
223
+ password: 'a_password',
224
+ demandedScopes: ['user'],
225
+ });
226
+ throw new YError('E_UNEXPECTED_SUCCESS');
227
+ } catch (err) {
228
+ expect({
229
+ err,
230
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
231
+ readClientGrantsCalls: readClientGrants.mock.calls,
232
+ logCalls: log.mock.calls,
233
+ }).toMatchInlineSnapshot(`
234
+ {
235
+ "err": [YError: E_OAUTH2_AUTHENTICATION_REQUIRED (["abbacaca-abba-caca-abba-cacaabbacaca"]): E_OAUTH2_AUTHENTICATION_REQUIRED],
236
+ "logCalls": [
237
+ [
238
+ "warning",
239
+ "⚠️ - Using the password flow is deprecated and not recommended.",
240
+ ],
241
+ [
242
+ "debug",
243
+ "👫 - OAuth2PasswordGranter Service Initialized!",
244
+ ],
245
+ ],
246
+ "oAuth2PasswordCheckCalls": [],
247
+ "readClientGrantsCalls": [
248
+ [
249
+ "abbacaca-abba-caca-abba-cacaabbacaca",
250
+ ],
251
+ ],
252
+ }
253
+ `);
254
+ }
255
+ });
256
+
257
+ test('should fail with a client with not password grant', async () => {
258
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
259
+ OAUTH2,
260
+ readClientGrants,
261
+ oAuth2Password,
262
+ log,
263
+ });
264
+
265
+ readClientGrants.mockResolvedValue({
266
+ allowedRedirectURIS: ['https://www.example.com'],
267
+ allowedGrantTypes: ['authorization_code'],
268
+ allowedScopes: ['user', 'admin'],
269
+ authenticationData: {
270
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
271
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
272
+ scopes: [],
273
+ },
274
+ isPublicClient: false,
275
+ });
276
+ oAuth2Password.check.mockResolvedValueOnce({
277
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
278
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
279
+ scopes: ['user'],
280
+ });
281
+
282
+ try {
283
+ await oAuth2PasswordGranter.authenticate?.(
284
+ {
285
+ username: 'a_username',
286
+ password: 'a_password',
287
+ demandedScopes: ['user'],
288
+ },
289
+ {
290
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
291
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
292
+ scopes: ['user'],
293
+ },
294
+ );
295
+ throw new YError('E_UNEXPECTED_SUCCESS');
296
+ } catch (err) {
297
+ expect({
298
+ err,
299
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
300
+ readClientGrantsCalls: readClientGrants.mock.calls,
301
+ logCalls: log.mock.calls,
302
+ }).toMatchInlineSnapshot(`
303
+ {
304
+ "err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["password",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
305
+ "logCalls": [
306
+ [
307
+ "warning",
308
+ "⚠️ - Using the password flow is deprecated and not recommended.",
309
+ ],
310
+ [
311
+ "debug",
312
+ "👫 - OAuth2PasswordGranter Service Initialized!",
313
+ ],
314
+ ],
315
+ "oAuth2PasswordCheckCalls": [],
316
+ "readClientGrantsCalls": [
317
+ [
318
+ "abbacaca-abba-caca-abba-cacaabbacaca",
319
+ ],
320
+ ],
321
+ }
322
+ `);
323
+ }
324
+ });
325
+
326
+ test('should fail with a client with a bad scope for the client', async () => {
327
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
328
+ OAUTH2: {
329
+ ...OAUTH2,
330
+ strictScopesChecks: true,
331
+ },
332
+ readClientGrants,
333
+ oAuth2Password,
334
+ log,
335
+ });
336
+
337
+ readClientGrants.mockResolvedValue({
338
+ allowedRedirectURIS: ['https://www.example.com'],
339
+ allowedGrantTypes: ['password'],
340
+ allowedScopes: ['user'],
341
+ authenticationData: {
342
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
343
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
344
+ scopes: [],
345
+ },
346
+ isPublicClient: false,
347
+ });
348
+ oAuth2Password.check.mockResolvedValueOnce({
349
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
350
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
351
+ scopes: ['user', 'admin'],
352
+ });
353
+
354
+ try {
355
+ await oAuth2PasswordGranter.authenticate?.(
356
+ {
357
+ username: 'a_username',
358
+ password: 'a_password',
359
+ demandedScopes: ['user', 'admin'],
360
+ },
361
+ {
362
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
363
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
364
+ scopes: ['user'],
365
+ },
366
+ );
367
+ throw new YError('E_UNEXPECTED_SUCCESS');
368
+ } catch (err) {
369
+ expect({
370
+ err,
371
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
372
+ readClientGrantsCalls: readClientGrants.mock.calls,
373
+ logCalls: log.mock.calls,
374
+ }).toMatchInlineSnapshot(`
375
+ {
376
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
377
+ "logCalls": [
378
+ [
379
+ "warning",
380
+ "⚠️ - Using the password flow is deprecated and not recommended.",
381
+ ],
382
+ [
383
+ "debug",
384
+ "👫 - OAuth2PasswordGranter Service Initialized!",
385
+ ],
386
+ ],
387
+ "oAuth2PasswordCheckCalls": [
388
+ [
389
+ {
390
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
391
+ "scopes": [
392
+ "user",
393
+ ],
394
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
395
+ },
396
+ "a_username",
397
+ "a_password",
398
+ ],
399
+ ],
400
+ "readClientGrantsCalls": [
401
+ [
402
+ "abbacaca-abba-caca-abba-cacaabbacaca",
403
+ ],
404
+ ],
405
+ }
406
+ `);
407
+ }
408
+ });
409
+
410
+ test('should fail with a client with a bad scope for the user', async () => {
411
+ const oAuth2PasswordGranter = await initOAuth2PasswordGranter({
412
+ OAUTH2: {
413
+ ...OAUTH2,
414
+ strictScopesChecks: true,
415
+ },
416
+ readClientGrants,
417
+ oAuth2Password,
418
+ log,
419
+ });
420
+
421
+ readClientGrants.mockResolvedValue({
422
+ allowedRedirectURIS: ['https://www.example.com'],
423
+ allowedGrantTypes: ['password'],
424
+ allowedScopes: ['user', 'admin'],
425
+ authenticationData: {
426
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
427
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
428
+ scopes: [],
429
+ },
430
+ isPublicClient: false,
431
+ });
432
+ oAuth2Password.check.mockResolvedValueOnce({
433
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
434
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
435
+ scopes: ['user'],
436
+ });
437
+
438
+ try {
439
+ await oAuth2PasswordGranter.authenticate?.(
440
+ {
441
+ username: 'a_username',
442
+ password: 'a_password',
443
+ demandedScopes: ['user', 'admin'],
444
+ },
445
+ {
446
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
447
+ userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
448
+ scopes: ['user'],
449
+ },
450
+ );
451
+ throw new YError('E_UNEXPECTED_SUCCESS');
452
+ } catch (err) {
453
+ expect({
454
+ err,
455
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
456
+ readClientGrantsCalls: readClientGrants.mock.calls,
457
+ logCalls: log.mock.calls,
458
+ }).toMatchInlineSnapshot(`
459
+ {
460
+ "err": [YError: E_OAUTH2_BAD_SCOPE (["admin"]): E_OAUTH2_BAD_SCOPE],
461
+ "logCalls": [
462
+ [
463
+ "warning",
464
+ "⚠️ - Using the password flow is deprecated and not recommended.",
465
+ ],
466
+ [
467
+ "debug",
468
+ "👫 - OAuth2PasswordGranter Service Initialized!",
469
+ ],
470
+ ],
471
+ "oAuth2PasswordCheckCalls": [
472
+ [
473
+ {
474
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
475
+ "scopes": [
476
+ "user",
477
+ ],
478
+ "userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
479
+ },
480
+ "a_username",
481
+ "a_password",
482
+ ],
483
+ ],
484
+ "readClientGrantsCalls": [
485
+ [
486
+ "abbacaca-abba-caca-abba-cacaabbacaca",
487
+ ],
488
+ ],
489
+ }
490
+ `);
491
+ }
492
+ });
493
+ });