@whook/oauth2 25.2.1 → 26.0.2

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 (197) hide show
  1. package/README.md +147 -98
  2. package/dist/index.d.ts +34 -11
  3. package/dist/index.js +18 -8
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.test.js +1671 -393
  6. package/dist/index.test.js.map +1 -1
  7. package/dist/libs/authorizationRequests.d.ts +6 -0
  8. package/dist/libs/authorizationRequests.js +22 -0
  9. package/dist/libs/authorizationRequests.js.map +1 -0
  10. package/dist/libs/authorizationRequests.test.js +24 -0
  11. package/dist/libs/authorizationRequests.test.js.map +1 -0
  12. package/dist/libs/clients.d.ts +2 -0
  13. package/dist/libs/clients.js +13 -0
  14. package/dist/libs/clients.js.map +1 -0
  15. package/dist/libs/errors.d.ts +143 -0
  16. package/dist/libs/errors.js +227 -0
  17. package/dist/libs/errors.js.map +1 -0
  18. package/dist/libs/grants.d.ts +2 -0
  19. package/dist/libs/grants.js +10 -0
  20. package/dist/libs/grants.js.map +1 -0
  21. package/dist/libs/redirectURI.d.ts +8 -0
  22. package/dist/libs/redirectURI.js +94 -0
  23. package/dist/libs/redirectURI.js.map +1 -0
  24. package/dist/libs/schemas.d.ts +31 -0
  25. package/dist/libs/schemas.js +33 -0
  26. package/dist/libs/schemas.js.map +1 -0
  27. package/dist/libs/scopes.d.ts +16 -0
  28. package/dist/libs/scopes.js +37 -0
  29. package/dist/libs/scopes.js.map +1 -1
  30. package/dist/libs/utils.d.ts +2 -0
  31. package/dist/libs/utils.js +17 -0
  32. package/dist/libs/utils.js.map +1 -0
  33. package/dist/libs/verifier.d.ts +10 -0
  34. package/dist/libs/verifier.js +51 -0
  35. package/dist/libs/verifier.js.map +1 -0
  36. package/dist/libs/verifier.test.d.ts +1 -0
  37. package/dist/libs/verifier.test.js +21 -0
  38. package/dist/libs/verifier.test.js.map +1 -0
  39. package/dist/routes/getOAuth2Authorize.d.ts +38 -59
  40. package/dist/routes/getOAuth2Authorize.js +129 -93
  41. package/dist/routes/getOAuth2Authorize.js.map +1 -1
  42. package/dist/routes/getOAuth2Authorize.test.js +959 -73
  43. package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
  44. package/dist/routes/getOAuth2WellKnownMetadata.d.ts +18 -11
  45. package/dist/routes/getOAuth2WellKnownMetadata.js +43 -11
  46. package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
  47. package/dist/routes/getOAuth2WellKnownMetadata.test.js +51 -49
  48. package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
  49. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +3 -6
  50. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +3 -6
  51. package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
  52. package/dist/routes/postAuthLogin.d.ts +14 -9
  53. package/dist/routes/postAuthLogin.js +10 -5
  54. package/dist/routes/postAuthLogin.js.map +1 -1
  55. package/dist/routes/postAuthLogin.test.js +79 -55
  56. package/dist/routes/postAuthLogin.test.js.map +1 -1
  57. package/dist/routes/postAuthLogout.d.ts +5 -2
  58. package/dist/routes/postAuthLogout.js +12 -7
  59. package/dist/routes/postAuthLogout.js.map +1 -1
  60. package/dist/routes/postAuthLogout.test.js +35 -17
  61. package/dist/routes/postAuthLogout.test.js.map +1 -1
  62. package/dist/routes/postAuthRefresh.d.ts +14 -9
  63. package/dist/routes/postAuthRefresh.js +10 -6
  64. package/dist/routes/postAuthRefresh.js.map +1 -1
  65. package/dist/routes/postAuthRefresh.test.js +90 -59
  66. package/dist/routes/postAuthRefresh.test.js.map +1 -1
  67. package/dist/routes/postOAuth2Acknowledge.d.ts +11 -10
  68. package/dist/routes/postOAuth2Acknowledge.js +43 -37
  69. package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
  70. package/dist/routes/postOAuth2Acknowledge.test.js +135 -70
  71. package/dist/routes/postOAuth2Acknowledge.test.js.map +1 -1
  72. package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
  73. package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
  74. package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
  75. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
  76. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
  77. package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
  78. package/dist/routes/postOAuth2Revoke.d.ts +99 -0
  79. package/dist/routes/postOAuth2Revoke.js +102 -0
  80. package/dist/routes/postOAuth2Revoke.js.map +1 -0
  81. package/dist/routes/postOAuth2Revoke.test.d.ts +1 -0
  82. package/dist/routes/postOAuth2Revoke.test.js +161 -0
  83. package/dist/routes/postOAuth2Revoke.test.js.map +1 -0
  84. package/dist/routes/postOAuth2Token.d.ts +47 -108
  85. package/dist/routes/postOAuth2Token.js +65 -114
  86. package/dist/routes/postOAuth2Token.js.map +1 -1
  87. package/dist/routes/postOAuth2Token.test.js +493 -61
  88. package/dist/routes/postOAuth2Token.test.js.map +1 -1
  89. package/dist/services/authCookies.d.ts +9 -13
  90. package/dist/services/authCookies.js +9 -5
  91. package/dist/services/authCookies.js.map +1 -1
  92. package/dist/services/authCookies.test.js.map +1 -1
  93. package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +124 -0
  94. package/dist/services/oAuth2AuthorizationCodeGranter.js +204 -0
  95. package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -0
  96. package/dist/services/oAuth2AuthorizationCodeGranter.test.d.ts +1 -0
  97. package/dist/services/oAuth2AuthorizationCodeGranter.test.js +345 -0
  98. package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -0
  99. package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
  100. package/dist/services/oAuth2AuthorizationRequests.js +61 -0
  101. package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
  102. package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
  103. package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
  104. package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
  105. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +28 -12
  106. package/dist/services/oAuth2ClientCredentialsGranter.js +39 -21
  107. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
  108. package/dist/services/oAuth2ClientCredentialsGranter.test.d.ts +1 -0
  109. package/dist/services/oAuth2ClientCredentialsGranter.test.js +265 -0
  110. package/dist/services/oAuth2ClientCredentialsGranter.test.js.map +1 -0
  111. package/dist/services/oAuth2Granters.d.ts +79 -86
  112. package/dist/services/oAuth2Granters.js +2 -67
  113. package/dist/services/oAuth2Granters.js.map +1 -1
  114. package/dist/services/oAuth2ImplicitGranter.d.ts +38 -0
  115. package/dist/services/oAuth2ImplicitGranter.js +61 -0
  116. package/dist/services/oAuth2ImplicitGranter.js.map +1 -0
  117. package/dist/services/oAuth2ImplicitGranter.test.d.ts +1 -0
  118. package/dist/services/oAuth2ImplicitGranter.test.js +589 -0
  119. package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -0
  120. package/dist/services/oAuth2PasswordGranter.d.ts +43 -10
  121. package/dist/services/oAuth2PasswordGranter.js +53 -18
  122. package/dist/services/oAuth2PasswordGranter.js.map +1 -1
  123. package/dist/services/oAuth2PasswordGranter.test.d.ts +1 -0
  124. package/dist/services/oAuth2PasswordGranter.test.js +455 -0
  125. package/dist/services/oAuth2PasswordGranter.test.js.map +1 -0
  126. package/dist/services/oAuth2RefreshTokenGranter.d.ts +42 -9
  127. package/dist/services/oAuth2RefreshTokenGranter.js +60 -21
  128. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
  129. package/dist/services/oAuth2RefreshTokenGranter.test.d.ts +1 -0
  130. package/dist/services/oAuth2RefreshTokenGranter.test.js +403 -0
  131. package/dist/services/oAuth2RefreshTokenGranter.test.js.map +1 -0
  132. package/package.json +14 -13
  133. package/src/index.test.ts +1773 -407
  134. package/src/index.ts +63 -53
  135. package/src/libs/authorizationRequests.test.ts +40 -0
  136. package/src/libs/authorizationRequests.ts +42 -0
  137. package/src/libs/clients.ts +22 -0
  138. package/src/libs/errors.ts +411 -0
  139. package/src/libs/grants.ts +15 -0
  140. package/src/libs/redirectURI.ts +142 -0
  141. package/src/libs/schemas.ts +37 -0
  142. package/src/libs/scopes.ts +49 -0
  143. package/src/libs/utils.ts +24 -0
  144. package/src/libs/verifier.test.ts +37 -0
  145. package/src/libs/verifier.ts +75 -0
  146. package/src/routes/getOAuth2Authorize.test.ts +1102 -102
  147. package/src/routes/getOAuth2Authorize.ts +243 -151
  148. package/src/routes/getOAuth2WellKnownMetadata.test.ts +73 -74
  149. package/src/routes/getOAuth2WellKnownMetadata.ts +63 -16
  150. package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +4 -6
  151. package/src/routes/postAuthLogin.test.ts +86 -57
  152. package/src/routes/postAuthLogin.ts +24 -14
  153. package/src/routes/postAuthLogout.test.ts +42 -18
  154. package/src/routes/postAuthLogout.ts +22 -8
  155. package/src/routes/postAuthRefresh.test.ts +98 -61
  156. package/src/routes/postAuthRefresh.ts +25 -13
  157. package/src/routes/postOAuth2Acknowledge.test.ts +167 -108
  158. package/src/routes/postOAuth2Acknowledge.ts +90 -64
  159. package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
  160. package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
  161. package/src/routes/postOAuth2Revoke.test.ts +179 -0
  162. package/src/routes/postOAuth2Revoke.ts +156 -0
  163. package/src/routes/postOAuth2Token.test.ts +570 -108
  164. package/src/routes/postOAuth2Token.ts +104 -136
  165. package/src/services/authCookies.test.ts +5 -5
  166. package/src/services/authCookies.ts +24 -22
  167. package/src/services/oAuth2AuthorizationCodeGranter.test.ts +407 -0
  168. package/src/services/oAuth2AuthorizationCodeGranter.ts +365 -0
  169. package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
  170. package/src/services/oAuth2AuthorizationRequests.ts +141 -0
  171. package/src/services/oAuth2ClientCredentialsGranter.test.ts +303 -0
  172. package/src/services/oAuth2ClientCredentialsGranter.ts +68 -43
  173. package/src/services/oAuth2Granters.ts +100 -196
  174. package/src/services/oAuth2ImplicitGranter.test.ts +650 -0
  175. package/src/services/oAuth2ImplicitGranter.ts +147 -0
  176. package/src/services/oAuth2PasswordGranter.test.ts +493 -0
  177. package/src/services/oAuth2PasswordGranter.ts +116 -40
  178. package/src/services/oAuth2RefreshTokenGranter.test.ts +441 -0
  179. package/src/services/oAuth2RefreshTokenGranter.ts +118 -51
  180. package/src/types.d.ts +1 -0
  181. package/dist/services/oAuth2CodeGranter.d.ts +0 -41
  182. package/dist/services/oAuth2CodeGranter.js +0 -105
  183. package/dist/services/oAuth2CodeGranter.js.map +0 -1
  184. package/dist/services/oAuth2CodeGranter.test.js +0 -123
  185. package/dist/services/oAuth2CodeGranter.test.js.map +0 -1
  186. package/dist/services/oAuth2TokenGranter.d.ts +0 -22
  187. package/dist/services/oAuth2TokenGranter.js +0 -64
  188. package/dist/services/oAuth2TokenGranter.js.map +0 -1
  189. package/src/__snapshots__/index.test.ts.snap +0 -401
  190. package/src/routes/__snapshots__/getOAuth2Authorize.test.ts.snap +0 -37
  191. package/src/routes/__snapshots__/postOAuth2Acknowledge.test.ts.snap +0 -61
  192. package/src/routes/__snapshots__/postOAuth2Token.test.ts.snap +0 -52
  193. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +0 -52
  194. package/src/services/oAuth2CodeGranter.test.ts +0 -176
  195. package/src/services/oAuth2CodeGranter.ts +0 -210
  196. package/src/services/oAuth2TokenGranter.ts +0 -119
  197. /package/dist/{services/oAuth2CodeGranter.test.d.ts → libs/authorizationRequests.test.d.ts} +0 -0
@@ -0,0 +1,147 @@
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 ({ clientGrants, demandedScopes }) => {
74
+ checkGrantType(clientGrants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
75
+
76
+ return {
77
+ scopes: demandedScopes,
78
+ };
79
+ };
80
+
81
+ // Access Token Response:
82
+ // https://tools.ietf.org/html/rfc6749#section-4.2.2
83
+ const acknowledgeWithToken: NonNullable<
84
+ WhookOAuth2ImplicitGranterService['acknowledge']
85
+ > = async (
86
+ userAuthenticationData,
87
+ { clientId, demandedRedirectURI, demandedScopes },
88
+ ) => {
89
+ const grants = await readClientGrants(clientId);
90
+
91
+ if (!grants.isPublicClient) {
92
+ throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
93
+ grants.authenticationData.clientId,
94
+ ]);
95
+ }
96
+
97
+ const filteredScopes = filterScopes(
98
+ filterScopes(
99
+ demandedScopes,
100
+ grants.allowedScopes,
101
+ !!OAUTH2.strictScopesChecks,
102
+ ),
103
+ userAuthenticationData.scopes,
104
+ !!OAUTH2.strictScopesChecks,
105
+ );
106
+
107
+ checkGrantType(grants.allowedGrantTypes, IMPLICIT_GRANT_TYPE);
108
+ checkRedirectURI(grants.allowedRedirectURIS, demandedRedirectURI);
109
+
110
+ const { token: accessToken, expiresAt: accessTokenExpiresAt } =
111
+ await oAuth2AccessToken.create({
112
+ ...userAuthenticationData,
113
+ clientId,
114
+ scopes: filteredScopes,
115
+ });
116
+
117
+ return {
118
+ acknowledgedAuthenticationData: {
119
+ ...userAuthenticationData,
120
+ clientId,
121
+ scopes: filteredScopes,
122
+ },
123
+ acknowledgedRedirectURI: demandedRedirectURI,
124
+ acknowledgedScopes: filteredScopes,
125
+ acknowledgedData: {
126
+ accessToken,
127
+ tokenType: 'bearer',
128
+ expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
129
+ },
130
+ };
131
+ };
132
+
133
+ log('debug', '👫 - OAuth2ImplicitGranter Service Initialized!');
134
+
135
+ return {
136
+ grantType: IMPLICIT_GRANT_TYPE,
137
+ responseType: IMPLICIT_RESPONSE_TYPE,
138
+ issuesRefreshToken: false,
139
+ authorize: authorizeWithToken,
140
+ acknowledge: acknowledgeWithToken,
141
+ };
142
+ }
143
+
144
+ export default location(
145
+ autoService(initOAuth2ImplicitGranter),
146
+ import.meta.url,
147
+ );
@@ -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
+ });