@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
@@ -3,111 +3,90 @@ import initPostOAuth2Token from './postOAuth2Token.js';
3
3
  import { YError } from 'yerror';
4
4
  import { type LogService, type TimeService } from 'common-services';
5
5
  import {
6
- type OAuth2GranterService,
7
- type OAuth2AccessTokenService,
8
- type OAuth2RefreshTokenService,
6
+ type WhookOAuth2GranterService,
7
+ type WhookOAuth2AccessTokenService,
8
+ type WhookOAuth2RefreshTokenService,
9
+ type WhookOAuth2Options,
10
+ type WhookOAuth2GranterDefinitions,
11
+ type WhookOAuth2ClientCredentialsGranterService,
9
12
  } from '../index.js';
10
- import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
11
- import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
12
- import { type OAuth2PasswordGranterService } from '../services/oAuth2PasswordGranter.js';
13
- import { type OAuth2RefreshTokenGranterService } from '../services/oAuth2RefreshTokenGranter.js';
13
+ import { type WhookOAuth2AuthorizationCodeGranterService } from '../services/oAuth2AuthorizationCodeGranter.js';
14
+ import { type WhookOAuth2ImplicitGranterService } from '../services/oAuth2ImplicitGranter.js';
15
+ import { type WhookOAuth2PasswordGranterService } from '../services/oAuth2PasswordGranter.js';
16
+ import { type WhookOAuth2RefreshTokenGranterService } from '../services/oAuth2RefreshTokenGranter.js';
14
17
  import { type WhookAuthenticationData } from '@whook/authorization';
15
18
 
16
19
  describe('postOAuth2Token', () => {
20
+ const OAUTH2: WhookOAuth2Options = {
21
+ authenticateURL: 'https://auth.example.com/sign_in',
22
+ allowedScopes: ['user'],
23
+ rootClientId: 'abbacaca-abba-caca-abba-cacaabbac0c0',
24
+ };
17
25
  const log = jest.fn<LogService>();
18
26
  const time = jest.fn<TimeService>();
19
27
  const oAuth2AccessToken = {
20
- create: jest.fn<OAuth2AccessTokenService['create']>(),
21
- check: jest.fn<OAuth2AccessTokenService['check']>(),
28
+ create: jest.fn<WhookOAuth2AccessTokenService['create']>(),
29
+ check: jest.fn<WhookOAuth2AccessTokenService['check']>(),
22
30
  };
23
31
  const oAuth2RefreshToken = {
24
- create: jest.fn<OAuth2RefreshTokenService['create']>(),
25
- check: jest.fn<OAuth2RefreshTokenService['check']>(),
32
+ create: jest.fn<WhookOAuth2RefreshTokenService['create']>(),
33
+ check: jest.fn<WhookOAuth2RefreshTokenService['check']>(),
26
34
  };
27
35
  const codeGranter = {
28
- type: 'code',
29
- authorizer: {
30
- responseType: 'code',
31
- authorize:
32
- jest.fn<
33
- NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
34
- >(),
35
- },
36
- acknowledger: {
37
- acknowledgmentType: 'code',
38
- acknowledge:
39
- jest.fn<
40
- NonNullable<OAuth2CodeGranterService['acknowledger']>['acknowledge']
41
- >(),
42
- },
43
- authenticator: {
44
- grantType: 'authorization_code',
45
- authenticate:
46
- jest.fn<
47
- NonNullable<OAuth2CodeGranterService['authenticator']>['authenticate']
48
- >(),
49
- },
50
- };
36
+ grantType: 'authorization_code',
37
+ responseType: 'code',
38
+ issuesRefreshToken: true,
39
+ authorize:
40
+ jest.fn<
41
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authorize']>
42
+ >(),
43
+ acknowledge:
44
+ jest.fn<
45
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['acknowledge']>
46
+ >(),
47
+ authenticate:
48
+ jest.fn<
49
+ NonNullable<WhookOAuth2AuthorizationCodeGranterService['authenticate']>
50
+ >(),
51
+ } satisfies WhookOAuth2AuthorizationCodeGranterService;
51
52
  const passwordGranter = {
52
- type: 'password',
53
- authenticator: {
54
- grantType: 'password',
55
- authenticate:
56
- jest.fn<
57
- NonNullable<
58
- OAuth2PasswordGranterService['authenticator']
59
- >['authenticate']
60
- >(),
61
- },
62
- };
53
+ grantType: 'password',
54
+ issuesRefreshToken: false,
55
+ authenticate:
56
+ jest.fn<NonNullable<WhookOAuth2PasswordGranterService['authenticate']>>(),
57
+ } satisfies WhookOAuth2PasswordGranterService;
63
58
  const clientCredentialsGranter = {
64
- type: 'client_credentials',
65
- authenticator: {
66
- grantType: 'client_credentials',
67
- authenticate:
68
- jest.fn<
69
- NonNullable<OAuth2CodeGranterService['authorizer']>['authorize']
70
- >(),
71
- },
72
- };
59
+ grantType: 'client_credentials',
60
+ issuesRefreshToken: false,
61
+ authenticate:
62
+ jest.fn<
63
+ NonNullable<WhookOAuth2ClientCredentialsGranterService['authenticate']>
64
+ >(),
65
+ } satisfies WhookOAuth2ClientCredentialsGranterService;
73
66
  const tokenGranter = {
74
- type: 'token',
75
- authorizer: {
76
- responseType: 'token',
77
- authorize:
78
- jest.fn<
79
- NonNullable<
80
- OAuth2TokenGranterService['authenticator']
81
- >['authenticate']
82
- >(),
83
- },
84
- acknowledger: {
85
- acknowledgmentType: 'token',
86
- acknowledge:
87
- jest.fn<
88
- NonNullable<OAuth2TokenGranterService['acknowledger']>['acknowledge']
89
- >(),
90
- },
91
- };
67
+ grantType: 'implicit',
68
+ responseType: 'token',
69
+ issuesRefreshToken: false,
70
+ authorize:
71
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['authorize']>>(),
72
+ acknowledge:
73
+ jest.fn<NonNullable<WhookOAuth2ImplicitGranterService['acknowledge']>>(),
74
+ } satisfies WhookOAuth2ImplicitGranterService;
92
75
  const refreshTokenGranter = {
93
- type: 'refresh',
94
- authenticator: {
95
- grantType: 'refresh',
96
- authenticate:
97
- jest.fn<
98
- NonNullable<
99
- OAuth2RefreshTokenGranterService['authenticator']
100
- >['authenticate']
101
- >(),
102
- },
103
- };
76
+ grantType: 'refresh_token',
77
+ issuesRefreshToken: true,
78
+ authenticate:
79
+ jest.fn<
80
+ NonNullable<WhookOAuth2RefreshTokenGranterService['authenticate']>
81
+ >(),
82
+ } satisfies WhookOAuth2RefreshTokenGranterService;
104
83
  const oAuth2Granters = [
105
84
  codeGranter,
106
85
  tokenGranter,
107
86
  passwordGranter,
108
87
  refreshTokenGranter,
109
88
  clientCredentialsGranter,
110
- ] as unknown as OAuth2GranterService[];
89
+ ] as unknown as WhookOAuth2GranterService<WhookOAuth2GranterDefinitions>[];
111
90
 
112
91
  beforeEach(() => {
113
92
  log.mockReset();
@@ -116,17 +95,17 @@ describe('postOAuth2Token', () => {
116
95
  oAuth2AccessToken.check.mockReset();
117
96
  oAuth2RefreshToken.create.mockReset();
118
97
  oAuth2RefreshToken.check.mockReset();
119
- codeGranter.authorizer.authorize.mockReset();
120
- codeGranter.acknowledger.acknowledge.mockReset();
121
- codeGranter.authenticator.authenticate.mockReset();
122
- tokenGranter.authorizer.authorize.mockReset();
123
- tokenGranter.acknowledger.acknowledge.mockReset();
124
- passwordGranter.authenticator.authenticate.mockReset();
125
- refreshTokenGranter.authenticator.authenticate.mockReset();
126
- clientCredentialsGranter.authenticator.authenticate.mockReset();
98
+ codeGranter.authorize.mockReset();
99
+ codeGranter.acknowledge.mockReset();
100
+ codeGranter.authenticate.mockReset();
101
+ tokenGranter.authorize.mockReset();
102
+ tokenGranter.acknowledge.mockReset();
103
+ passwordGranter.authenticate.mockReset();
104
+ refreshTokenGranter.authenticate.mockReset();
105
+ clientCredentialsGranter.authenticate.mockReset();
127
106
  });
128
107
 
129
- test('should create a token', async () => {
108
+ test('should create a token with the code flow', async () => {
130
109
  time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
131
110
  oAuth2AccessToken.create.mockResolvedValueOnce({
132
111
  token: 'an_access_token',
@@ -136,13 +115,14 @@ describe('postOAuth2Token', () => {
136
115
  token: 'a_refresh_token',
137
116
  expiresAt: Date.parse('2180-03-06T00:00:00Z'),
138
117
  });
139
- codeGranter.authorizer.authorize.mockResolvedValueOnce({
140
- applicationId: '',
141
- redirectURI: 'http://lol',
142
- scope: 'user,admin',
118
+ codeGranter.authenticate.mockResolvedValueOnce({
119
+ clientId: 'authenticate_app_id',
120
+ userId: 'authenticate_user_id',
121
+ scopes: ['user', 'admin'],
143
122
  });
144
123
 
145
124
  const postOAuth2Token = await initPostOAuth2Token({
125
+ OAUTH2,
146
126
  oAuth2Granters,
147
127
  oAuth2AccessToken,
148
128
  oAuth2RefreshToken,
@@ -151,8 +131,8 @@ describe('postOAuth2Token', () => {
151
131
  });
152
132
  const response = await postOAuth2Token({
153
133
  authenticationData: {
154
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
155
- scope: 'user',
134
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
135
+ scopes: ['user'],
156
136
  } as WhookAuthenticationData,
157
137
  body: {
158
138
  grant_type: 'authorization_code',
@@ -164,13 +144,462 @@ describe('postOAuth2Token', () => {
164
144
  expect({
165
145
  response,
166
146
  oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
147
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
148
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
149
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
150
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
151
+ codeGranterAcknowledgerAcknowledgeCalls:
152
+ codeGranter.acknowledge.mock.calls,
153
+ codeGranterAuthenticatorAuthenticateCalls:
154
+ codeGranter.authenticate.mock.calls,
155
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
156
+ tokenGranterAcknowledgerAcknowledgeCalls:
157
+ tokenGranter.acknowledge.mock.calls,
158
+ passwordGranterAuthenticatorAuthenticateCalls:
159
+ passwordGranter.authenticate.mock.calls,
160
+ refreshTokenGranterAuthenticatorAuthenticateCalls:
161
+ refreshTokenGranter.authenticate.mock.calls,
162
+ clientCredentialsGranterAuthenticatorAuthenticateCalls:
163
+ clientCredentialsGranter.authenticate.mock.calls,
164
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
165
+ }).toMatchInlineSnapshot(`
166
+ {
167
+ "clientCredentialsGranterAuthenticatorAuthenticateCalls": [],
168
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
169
+ "codeGranterAuthenticatorAuthenticateCalls": [
170
+ [
171
+ {
172
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
173
+ "code": "007",
174
+ "demandedScopes": [],
175
+ "redirectURI": "http://lol",
176
+ },
177
+ {
178
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
179
+ "scopes": [
180
+ "user",
181
+ ],
182
+ },
183
+ ],
184
+ ],
185
+ "codeGranterAuthorizerAuthorizeCalls": [],
186
+ "logCalls": [],
187
+ "oAuth2AccessTokenCheckCalls": [],
188
+ "oAuth2AccessTokenCreateCalls": [
189
+ [
190
+ {
191
+ "clientId": "authenticate_app_id",
192
+ "scopes": [
193
+ "user",
194
+ "admin",
195
+ ],
196
+ "userId": "authenticate_user_id",
197
+ },
198
+ ],
199
+ ],
200
+ "oAuth2RefreshTokenCheckCalls": [],
201
+ "oAuth2RefreshTokenCreateCalls": [
202
+ [
203
+ {
204
+ "clientId": "authenticate_app_id",
205
+ "scopes": [
206
+ "user",
207
+ "admin",
208
+ ],
209
+ "userId": "authenticate_user_id",
210
+ },
211
+ ],
212
+ ],
213
+ "passwordGranterAuthenticatorAuthenticateCalls": [],
214
+ "refreshTokenGranterAuthenticatorAuthenticateCalls": [],
215
+ "response": {
216
+ "body": {
217
+ "access_token": "an_access_token",
218
+ "expiration_date": "2010-03-07T00:00:00.000Z",
219
+ "expires_in": 86400,
220
+ "refresh_token": "a_refresh_token",
221
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
222
+ "refresh_token_expires_in": 5364748800,
223
+ "scope": "user admin",
224
+ "token_type": "bearer",
225
+ },
226
+ "headers": {
227
+ "Cache-Control": "no-store",
228
+ "Pragma": "no-cache",
229
+ },
230
+ "status": 200,
231
+ },
232
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
233
+ "tokenGranterAuthorizerAuthorizeCalls": [],
234
+ }
235
+ `);
236
+ });
237
+
238
+ test('should create a token with the client_credentials flow', async () => {
239
+ time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
240
+ oAuth2AccessToken.create.mockResolvedValueOnce({
241
+ token: 'an_access_token',
242
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
243
+ });
244
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
245
+ token: 'a_refresh_token',
246
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
247
+ });
248
+ clientCredentialsGranter.authenticate.mockResolvedValueOnce({
249
+ clientId: 'authenticate_app_id',
250
+ scopes: ['user', 'admin'],
251
+ userId: 'a_user_id',
252
+ });
253
+
254
+ const postOAuth2Token = await initPostOAuth2Token({
255
+ OAUTH2,
256
+ oAuth2Granters,
257
+ oAuth2AccessToken,
258
+ oAuth2RefreshToken,
259
+ time,
260
+ log,
261
+ });
262
+ const response = await postOAuth2Token({
263
+ authenticationData: {
264
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
265
+ scopes: ['user'],
266
+ } as WhookAuthenticationData,
267
+ body: {
268
+ grant_type: 'client_credentials',
269
+ scope: 'user',
270
+ },
271
+ });
272
+
273
+ expect({
274
+ response,
275
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
276
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
277
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
278
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
279
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
280
+ codeGranterAcknowledgerAcknowledgeCalls:
281
+ codeGranter.acknowledge.mock.calls,
282
+ codeGranterAuthenticatorAuthenticateCalls:
283
+ codeGranter.authenticate.mock.calls,
284
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
285
+ tokenGranterAcknowledgerAcknowledgeCalls:
286
+ tokenGranter.acknowledge.mock.calls,
287
+ passwordGranterAuthenticatorAuthenticateCalls:
288
+ passwordGranter.authenticate.mock.calls,
289
+ refreshTokenGranterAuthenticatorAuthenticateCalls:
290
+ refreshTokenGranter.authenticate.mock.calls,
291
+ clientCredentialsGranterAuthenticatorAuthenticateCalls:
292
+ clientCredentialsGranter.authenticate.mock.calls,
293
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
294
+ }).toMatchInlineSnapshot(`
295
+ {
296
+ "clientCredentialsGranterAuthenticatorAuthenticateCalls": [
297
+ [
298
+ {
299
+ "demandedScopes": [
300
+ "user",
301
+ ],
302
+ },
303
+ {
304
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
305
+ "scopes": [
306
+ "user",
307
+ ],
308
+ },
309
+ ],
310
+ ],
311
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
312
+ "codeGranterAuthenticatorAuthenticateCalls": [],
313
+ "codeGranterAuthorizerAuthorizeCalls": [],
314
+ "logCalls": [],
315
+ "oAuth2AccessTokenCheckCalls": [],
316
+ "oAuth2AccessTokenCreateCalls": [
317
+ [
318
+ {
319
+ "clientId": "authenticate_app_id",
320
+ "scopes": [
321
+ "user",
322
+ "admin",
323
+ ],
324
+ "userId": "a_user_id",
325
+ },
326
+ ],
327
+ ],
328
+ "oAuth2RefreshTokenCheckCalls": [],
329
+ "oAuth2RefreshTokenCreateCalls": [],
330
+ "passwordGranterAuthenticatorAuthenticateCalls": [],
331
+ "refreshTokenGranterAuthenticatorAuthenticateCalls": [],
332
+ "response": {
333
+ "body": {
334
+ "access_token": "an_access_token",
335
+ "expiration_date": "2010-03-07T00:00:00.000Z",
336
+ "expires_in": 86400,
337
+ "scope": "user admin",
338
+ "token_type": "bearer",
339
+ },
340
+ "headers": {
341
+ "Cache-Control": "no-store",
342
+ "Pragma": "no-cache",
343
+ },
344
+ "status": 200,
345
+ },
346
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
347
+ "tokenGranterAuthorizerAuthorizeCalls": [],
348
+ }
349
+ `);
350
+ });
351
+
352
+ test('should create a token with the refresh_token flow', async () => {
353
+ time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
354
+ oAuth2AccessToken.create.mockResolvedValueOnce({
355
+ token: 'an_access_token',
356
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
357
+ });
358
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
359
+ token: 'a_refresh_token',
360
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
361
+ });
362
+ refreshTokenGranter.authenticate.mockResolvedValueOnce({
363
+ clientId: 'authenticate_app_id',
364
+ userId: 'authenticate_user_id',
365
+ scopes: ['user', 'admin'],
366
+ });
367
+
368
+ const postOAuth2Token = await initPostOAuth2Token({
369
+ OAUTH2,
370
+ oAuth2Granters,
371
+ oAuth2AccessToken,
372
+ oAuth2RefreshToken,
373
+ time,
374
+ log,
375
+ });
376
+ const response = await postOAuth2Token({
377
+ authenticationData: {
378
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
379
+ scopes: ['user'],
380
+ } as WhookAuthenticationData,
381
+ body: {
382
+ grant_type: 'refresh_token',
383
+ refresh_token: 'a_refresh_token',
384
+ scope: 'user',
385
+ },
386
+ });
387
+
388
+ expect({
389
+ response,
390
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
391
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
392
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
393
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
394
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
395
+ codeGranterAcknowledgerAcknowledgeCalls:
396
+ codeGranter.acknowledge.mock.calls,
397
+ codeGranterAuthenticatorAuthenticateCalls:
398
+ codeGranter.authenticate.mock.calls,
399
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
400
+ tokenGranterAcknowledgerAcknowledgeCalls:
401
+ tokenGranter.acknowledge.mock.calls,
402
+ passwordGranterAuthenticatorAuthenticateCalls:
403
+ passwordGranter.authenticate.mock.calls,
404
+ refreshTokenGranterAuthenticatorAuthenticateCalls:
405
+ refreshTokenGranter.authenticate.mock.calls,
406
+ clientCredentialsGranterAuthenticatorAuthenticateCalls:
407
+ clientCredentialsGranter.authenticate.mock.calls,
408
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
409
+ }).toMatchInlineSnapshot(`
410
+ {
411
+ "clientCredentialsGranterAuthenticatorAuthenticateCalls": [],
412
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
413
+ "codeGranterAuthenticatorAuthenticateCalls": [],
414
+ "codeGranterAuthorizerAuthorizeCalls": [],
415
+ "logCalls": [],
416
+ "oAuth2AccessTokenCheckCalls": [],
417
+ "oAuth2AccessTokenCreateCalls": [
418
+ [
419
+ {
420
+ "clientId": "authenticate_app_id",
421
+ "scopes": [
422
+ "user",
423
+ "admin",
424
+ ],
425
+ "userId": "authenticate_user_id",
426
+ },
427
+ ],
428
+ ],
429
+ "oAuth2RefreshTokenCheckCalls": [],
430
+ "oAuth2RefreshTokenCreateCalls": [
431
+ [
432
+ {
433
+ "clientId": "authenticate_app_id",
434
+ "scopes": [
435
+ "user",
436
+ "admin",
437
+ ],
438
+ "userId": "authenticate_user_id",
439
+ },
440
+ ],
441
+ ],
442
+ "passwordGranterAuthenticatorAuthenticateCalls": [],
443
+ "refreshTokenGranterAuthenticatorAuthenticateCalls": [
444
+ [
445
+ {
446
+ "demandedScopes": [
447
+ "user",
448
+ ],
449
+ "refreshToken": "a_refresh_token",
450
+ },
451
+ {
452
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
453
+ "scopes": [
454
+ "user",
455
+ ],
456
+ },
457
+ ],
458
+ ],
459
+ "response": {
460
+ "body": {
461
+ "access_token": "an_access_token",
462
+ "expiration_date": "2010-03-07T00:00:00.000Z",
463
+ "expires_in": 86400,
464
+ "refresh_token": "a_refresh_token",
465
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
466
+ "refresh_token_expires_in": 5364748800,
467
+ "scope": "user admin",
468
+ "token_type": "bearer",
469
+ },
470
+ "headers": {
471
+ "Cache-Control": "no-store",
472
+ "Pragma": "no-cache",
473
+ },
474
+ "status": 200,
475
+ },
476
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
477
+ "tokenGranterAuthorizerAuthorizeCalls": [],
478
+ }
479
+ `);
480
+ });
481
+
482
+ test('should create a token with the password flow', async () => {
483
+ time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
484
+ oAuth2AccessToken.create.mockResolvedValueOnce({
485
+ token: 'an_access_token',
486
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
487
+ });
488
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
489
+ token: 'a_refresh_token',
490
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
491
+ });
492
+ passwordGranter.authenticate.mockResolvedValueOnce({
493
+ clientId: 'authenticate_app_id',
494
+ userId: 'authenticate_user_id',
495
+ scopes: ['user', 'admin'],
496
+ });
497
+
498
+ const postOAuth2Token = await initPostOAuth2Token({
499
+ OAUTH2,
500
+ oAuth2Granters,
501
+ oAuth2AccessToken,
502
+ oAuth2RefreshToken,
503
+ time,
504
+ log,
505
+ });
506
+ const response = await postOAuth2Token({
507
+ authenticationData: {
508
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
509
+ scopes: ['user'],
510
+ } as WhookAuthenticationData,
511
+ body: {
512
+ grant_type: 'password',
513
+ username: 'a_username',
514
+ password: 'a_password',
515
+ scope: 'user',
516
+ },
517
+ });
518
+
519
+ expect({
520
+ response,
521
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
522
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
167
523
  oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
524
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
525
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
526
+ codeGranterAcknowledgerAcknowledgeCalls:
527
+ codeGranter.acknowledge.mock.calls,
528
+ codeGranterAuthenticatorAuthenticateCalls:
529
+ codeGranter.authenticate.mock.calls,
530
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
531
+ tokenGranterAcknowledgerAcknowledgeCalls:
532
+ tokenGranter.acknowledge.mock.calls,
533
+ passwordGranterAuthenticatorAuthenticateCalls:
534
+ passwordGranter.authenticate.mock.calls,
535
+ refreshTokenGranterAuthenticatorAuthenticateCalls:
536
+ refreshTokenGranter.authenticate.mock.calls,
537
+ clientCredentialsGranterAuthenticatorAuthenticateCalls:
538
+ clientCredentialsGranter.authenticate.mock.calls,
168
539
  logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
169
- }).toMatchSnapshot();
540
+ }).toMatchInlineSnapshot(`
541
+ {
542
+ "clientCredentialsGranterAuthenticatorAuthenticateCalls": [],
543
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
544
+ "codeGranterAuthenticatorAuthenticateCalls": [],
545
+ "codeGranterAuthorizerAuthorizeCalls": [],
546
+ "logCalls": [],
547
+ "oAuth2AccessTokenCheckCalls": [],
548
+ "oAuth2AccessTokenCreateCalls": [
549
+ [
550
+ {
551
+ "clientId": "authenticate_app_id",
552
+ "scopes": [
553
+ "user",
554
+ "admin",
555
+ ],
556
+ "userId": "authenticate_user_id",
557
+ },
558
+ ],
559
+ ],
560
+ "oAuth2RefreshTokenCheckCalls": [],
561
+ "oAuth2RefreshTokenCreateCalls": [],
562
+ "passwordGranterAuthenticatorAuthenticateCalls": [
563
+ [
564
+ {
565
+ "demandedScopes": [
566
+ "user",
567
+ ],
568
+ "password": "a_password",
569
+ "username": "a_username",
570
+ },
571
+ {
572
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
573
+ "scopes": [
574
+ "user",
575
+ ],
576
+ },
577
+ ],
578
+ ],
579
+ "refreshTokenGranterAuthenticatorAuthenticateCalls": [],
580
+ "response": {
581
+ "body": {
582
+ "access_token": "an_access_token",
583
+ "expiration_date": "2010-03-07T00:00:00.000Z",
584
+ "expires_in": 86400,
585
+ "scope": "user admin",
586
+ "token_type": "bearer",
587
+ },
588
+ "headers": {
589
+ "Cache-Control": "no-store",
590
+ "Pragma": "no-cache",
591
+ },
592
+ "status": 200,
593
+ },
594
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
595
+ "tokenGranterAuthorizerAuthorizeCalls": [],
596
+ }
597
+ `);
170
598
  });
171
599
 
172
600
  test('should fail with a bad grant type', async () => {
173
601
  const postOAuth2Token = await initPostOAuth2Token({
602
+ OAUTH2,
174
603
  oAuth2Granters,
175
604
  oAuth2AccessToken,
176
605
  oAuth2RefreshToken,
@@ -181,8 +610,8 @@ describe('postOAuth2Token', () => {
181
610
  try {
182
611
  await postOAuth2Token({
183
612
  authenticationData: {
184
- applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
185
- scope: 'user',
613
+ clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
614
+ scopes: ['user'],
186
615
  } as WhookAuthenticationData,
187
616
  body: {
188
617
  grant_type: 'yolo',
@@ -196,19 +625,52 @@ describe('postOAuth2Token', () => {
196
625
  expect({
197
626
  errorCode: (err as YError).code,
198
627
  errorDebug: (err as YError).debug,
628
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
629
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
630
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
631
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
632
+ codeGranterAuthorizerAuthorizeCalls: codeGranter.authorize.mock.calls,
633
+ codeGranterAcknowledgerAcknowledgeCalls:
634
+ codeGranter.acknowledge.mock.calls,
635
+ codeGranterAuthenticatorAuthenticateCalls:
636
+ codeGranter.authenticate.mock.calls,
637
+ tokenGranterAuthorizerAuthorizeCalls: tokenGranter.authorize.mock.calls,
638
+ tokenGranterAcknowledgerAcknowledgeCalls:
639
+ tokenGranter.acknowledge.mock.calls,
640
+ passwordGranterAuthenticatorAuthenticateCalls:
641
+ passwordGranter.authenticate.mock.calls,
642
+ refreshTokenGranterAuthenticatorAuthenticateCalls:
643
+ refreshTokenGranter.authenticate.mock.calls,
644
+ clientCredentialsGranterAuthenticatorAuthenticateCalls:
645
+ clientCredentialsGranter.authenticate.mock.calls,
646
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
199
647
  }).toMatchInlineSnapshot(`
200
648
  {
201
- "errorCode": "E_UNKNOWN_AUTHENTICATOR_TYPE",
649
+ "clientCredentialsGranterAuthenticatorAuthenticateCalls": [],
650
+ "codeGranterAcknowledgerAcknowledgeCalls": [],
651
+ "codeGranterAuthenticatorAuthenticateCalls": [],
652
+ "codeGranterAuthorizerAuthorizeCalls": [],
653
+ "errorCode": "E_OAUTH2_UNKNOWN_GRANT_TYPE",
202
654
  "errorDebug": [
203
655
  "yolo",
204
656
  ],
657
+ "logCalls": [
658
+ [
659
+ "debug",
660
+ "👫 - OAuth2 token issuing error",
661
+ "E_OAUTH2_UNKNOWN_GRANT_TYPE",
662
+ ],
663
+ ],
664
+ "oAuth2AccessTokenCheckCalls": [],
665
+ "oAuth2AccessTokenCreateCalls": [],
666
+ "oAuth2RefreshTokenCheckCalls": [],
667
+ "oAuth2RefreshTokenCreateCalls": [],
668
+ "passwordGranterAuthenticatorAuthenticateCalls": [],
669
+ "refreshTokenGranterAuthenticatorAuthenticateCalls": [],
670
+ "tokenGranterAcknowledgerAcknowledgeCalls": [],
671
+ "tokenGranterAuthorizerAuthorizeCalls": [],
205
672
  }
206
673
  `);
207
- expect({
208
- oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
209
- oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
210
- logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
211
- }).toMatchSnapshot();
212
674
  }
213
675
  });
214
676
  });