@whook/oauth2 8.3.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 (146) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +384 -0
  3. package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
  4. package/dist/handlers/getOAuth2Authorize.js +192 -0
  5. package/dist/handlers/getOAuth2Authorize.js.map +1 -0
  6. package/dist/handlers/getOAuth2Authorize.mjs +169 -0
  7. package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
  8. package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
  9. package/dist/handlers/getOAuth2Authorize.test.js +122 -0
  10. package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
  11. package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
  12. package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
  13. package/dist/handlers/postAuthLogin.d.ts +45 -0
  14. package/dist/handlers/postAuthLogin.js +125 -0
  15. package/dist/handlers/postAuthLogin.js.map +1 -0
  16. package/dist/handlers/postAuthLogin.mjs +108 -0
  17. package/dist/handlers/postAuthLogin.mjs.map +1 -0
  18. package/dist/handlers/postAuthLogin.test.d.ts +1 -0
  19. package/dist/handlers/postAuthLogin.test.js +90 -0
  20. package/dist/handlers/postAuthLogin.test.js.map +1 -0
  21. package/dist/handlers/postAuthLogin.test.mjs +85 -0
  22. package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
  23. package/dist/handlers/postAuthLogout.d.ts +12 -0
  24. package/dist/handlers/postAuthLogout.js +46 -0
  25. package/dist/handlers/postAuthLogout.js.map +1 -0
  26. package/dist/handlers/postAuthLogout.mjs +33 -0
  27. package/dist/handlers/postAuthLogout.mjs.map +1 -0
  28. package/dist/handlers/postAuthLogout.test.d.ts +1 -0
  29. package/dist/handlers/postAuthLogout.test.js +35 -0
  30. package/dist/handlers/postAuthLogout.test.js.map +1 -0
  31. package/dist/handlers/postAuthLogout.test.mjs +30 -0
  32. package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
  33. package/dist/handlers/postAuthRefresh.d.ts +45 -0
  34. package/dist/handlers/postAuthRefresh.js +140 -0
  35. package/dist/handlers/postAuthRefresh.js.map +1 -0
  36. package/dist/handlers/postAuthRefresh.mjs +122 -0
  37. package/dist/handlers/postAuthRefresh.mjs.map +1 -0
  38. package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
  39. package/dist/handlers/postAuthRefresh.test.js +99 -0
  40. package/dist/handlers/postAuthRefresh.test.js.map +1 -0
  41. package/dist/handlers/postAuthRefresh.test.mjs +94 -0
  42. package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
  43. package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
  44. package/dist/handlers/postOAuth2Acknowledge.js +165 -0
  45. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
  46. package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
  47. package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
  48. package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
  49. package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
  50. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
  51. package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
  52. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
  53. package/dist/handlers/postOAuth2Token.d.ts +36 -0
  54. package/dist/handlers/postOAuth2Token.js +263 -0
  55. package/dist/handlers/postOAuth2Token.js.map +1 -0
  56. package/dist/handlers/postOAuth2Token.mjs +242 -0
  57. package/dist/handlers/postOAuth2Token.mjs.map +1 -0
  58. package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
  59. package/dist/handlers/postOAuth2Token.test.js +166 -0
  60. package/dist/handlers/postOAuth2Token.test.js.map +1 -0
  61. package/dist/handlers/postOAuth2Token.test.mjs +160 -0
  62. package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
  63. package/dist/index.d.ts +17 -0
  64. package/dist/index.js +230 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/index.mjs +15 -0
  67. package/dist/index.mjs.map +1 -0
  68. package/dist/index.test.d.ts +1 -0
  69. package/dist/index.test.js +759 -0
  70. package/dist/index.test.js.map +1 -0
  71. package/dist/index.test.mjs +748 -0
  72. package/dist/index.test.mjs.map +1 -0
  73. package/dist/services/authCookies.d.ts +26 -0
  74. package/dist/services/authCookies.js +74 -0
  75. package/dist/services/authCookies.js.map +1 -0
  76. package/dist/services/authCookies.mjs +58 -0
  77. package/dist/services/authCookies.mjs.map +1 -0
  78. package/dist/services/authCookies.test.d.ts +1 -0
  79. package/dist/services/authCookies.test.js +82 -0
  80. package/dist/services/authCookies.test.js.map +1 -0
  81. package/dist/services/authCookies.test.mjs +77 -0
  82. package/dist/services/authCookies.test.mjs.map +1 -0
  83. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
  84. package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
  85. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
  86. package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
  88. package/dist/services/oAuth2CodeGranter.d.ts +17 -0
  89. package/dist/services/oAuth2CodeGranter.js +115 -0
  90. package/dist/services/oAuth2CodeGranter.js.map +1 -0
  91. package/dist/services/oAuth2CodeGranter.mjs +98 -0
  92. package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
  93. package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
  94. package/dist/services/oAuth2CodeGranter.test.js +86 -0
  95. package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
  96. package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
  97. package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
  98. package/dist/services/oAuth2Granters.d.ts +79 -0
  99. package/dist/services/oAuth2Granters.js +140 -0
  100. package/dist/services/oAuth2Granters.js.map +1 -0
  101. package/dist/services/oAuth2Granters.mjs +127 -0
  102. package/dist/services/oAuth2Granters.mjs.map +1 -0
  103. package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
  104. package/dist/services/oAuth2PasswordGranter.js +55 -0
  105. package/dist/services/oAuth2PasswordGranter.js.map +1 -0
  106. package/dist/services/oAuth2PasswordGranter.mjs +40 -0
  107. package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
  109. package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
  111. package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
  113. package/dist/services/oAuth2TokenGranter.d.ts +15 -0
  114. package/dist/services/oAuth2TokenGranter.js +92 -0
  115. package/dist/services/oAuth2TokenGranter.js.map +1 -0
  116. package/dist/services/oAuth2TokenGranter.mjs +78 -0
  117. package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
  118. package/package.json +205 -0
  119. package/src/__snapshots__/index.test.ts.snap +396 -0
  120. package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
  121. package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
  122. package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
  123. package/src/handlers/getOAuth2Authorize.test.ts +142 -0
  124. package/src/handlers/getOAuth2Authorize.ts +222 -0
  125. package/src/handlers/postAuthLogin.test.ts +88 -0
  126. package/src/handlers/postAuthLogin.ts +123 -0
  127. package/src/handlers/postAuthLogout.test.ts +33 -0
  128. package/src/handlers/postAuthLogout.ts +38 -0
  129. package/src/handlers/postAuthRefresh.test.ts +97 -0
  130. package/src/handlers/postAuthRefresh.ts +146 -0
  131. package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
  132. package/src/handlers/postOAuth2Acknowledge.ts +209 -0
  133. package/src/handlers/postOAuth2Token.test.ts +153 -0
  134. package/src/handlers/postOAuth2Token.ts +302 -0
  135. package/src/index.test.ts +936 -0
  136. package/src/index.ts +105 -0
  137. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
  138. package/src/services/authCookies.test.ts +93 -0
  139. package/src/services/authCookies.ts +82 -0
  140. package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
  141. package/src/services/oAuth2CodeGranter.test.ts +94 -0
  142. package/src/services/oAuth2CodeGranter.ts +133 -0
  143. package/src/services/oAuth2Granters.ts +274 -0
  144. package/src/services/oAuth2PasswordGranter.ts +75 -0
  145. package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
  146. package/src/services/oAuth2TokenGranter.ts +105 -0
@@ -0,0 +1,396 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`OAuth2 server with the client credentials flow should produce new tokens 2`] = `
4
+ Object {
5
+ "authenticationCheckCalls": Array [
6
+ Array [
7
+ "basic",
8
+ Object {
9
+ "hash": "YWxpOm9wZW5fc2VzYW1l",
10
+ "password": "open_sesame",
11
+ "username": "ali",
12
+ },
13
+ ],
14
+ ],
15
+ "checkApplicationCalls": Array [
16
+ Array [
17
+ Object {
18
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
19
+ "scope": "user",
20
+ "type": "client_credentials",
21
+ },
22
+ ],
23
+ ],
24
+ "oAuth2AccessTokenCheckCalls": Array [],
25
+ "oAuth2AccessTokenCreateCalls": Array [
26
+ Array [
27
+ Object {
28
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
29
+ "scope": "user,oauth",
30
+ },
31
+ Object {
32
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
33
+ "scope": "user",
34
+ },
35
+ ],
36
+ ],
37
+ "oAuth2ClientCredentialsCheckCalls": Array [],
38
+ "oAuth2CodeCheckCalls": Array [],
39
+ "oAuth2CodeCreateCalls": Array [],
40
+ "oAuth2PasswordCheckCalls": Array [],
41
+ "oAuth2RefreshTokenCheckCalls": Array [],
42
+ "oAuth2RefreshTokenCreateCalls": Array [
43
+ Array [
44
+ Object {
45
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
46
+ "scope": "user,oauth",
47
+ },
48
+ Object {
49
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
50
+ "scope": "user",
51
+ },
52
+ ],
53
+ ],
54
+ }
55
+ `;
56
+
57
+ exports[`OAuth2 server with the code flow should build the authorization redirection 2`] = `
58
+ Object {
59
+ "authenticationCheckCalls": Array [],
60
+ "checkApplicationCalls": Array [
61
+ Array [
62
+ Object {
63
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
64
+ "redirectURI": "https://example.com/oauth2/callback?a_param=a_param_value",
65
+ "scope": "user",
66
+ "type": "code",
67
+ },
68
+ ],
69
+ ],
70
+ "oAuth2AccessTokenCheckCalls": Array [],
71
+ "oAuth2AccessTokenCreateCalls": Array [],
72
+ "oAuth2ClientCredentialsCheckCalls": Array [],
73
+ "oAuth2CodeCheckCalls": Array [],
74
+ "oAuth2CodeCreateCalls": Array [],
75
+ "oAuth2PasswordCheckCalls": Array [],
76
+ "oAuth2RefreshTokenCheckCalls": Array [],
77
+ "oAuth2RefreshTokenCreateCalls": Array [],
78
+ }
79
+ `;
80
+
81
+ exports[`OAuth2 server with the code flow should produce new tokens 2`] = `
82
+ Object {
83
+ "authenticationCheckCalls": Array [
84
+ Array [
85
+ "basic",
86
+ Object {
87
+ "hash": "YWxpOm9wZW5fc2VzYW1l",
88
+ "password": "open_sesame",
89
+ "username": "ali",
90
+ },
91
+ ],
92
+ ],
93
+ "checkApplicationCalls": Array [
94
+ Array [
95
+ Object {
96
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
97
+ "scope": "",
98
+ "type": "code",
99
+ },
100
+ ],
101
+ ],
102
+ "oAuth2AccessTokenCheckCalls": Array [],
103
+ "oAuth2AccessTokenCreateCalls": Array [
104
+ Array [
105
+ Object {
106
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
107
+ "scope": "user,oauth",
108
+ },
109
+ Object {
110
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
111
+ "scope": "user,auth",
112
+ "userId": 1,
113
+ },
114
+ ],
115
+ ],
116
+ "oAuth2ClientCredentialsCheckCalls": Array [],
117
+ "oAuth2CodeCheckCalls": Array [
118
+ Array [
119
+ Object {
120
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
121
+ "scope": "user,oauth",
122
+ },
123
+ "a_grant_code",
124
+ "http://redirect.example.com/yolo",
125
+ ],
126
+ ],
127
+ "oAuth2CodeCreateCalls": Array [],
128
+ "oAuth2PasswordCheckCalls": Array [],
129
+ "oAuth2RefreshTokenCheckCalls": Array [],
130
+ "oAuth2RefreshTokenCreateCalls": Array [
131
+ Array [
132
+ Object {
133
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
134
+ "scope": "user,oauth",
135
+ },
136
+ Object {
137
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
138
+ "scope": "user,auth",
139
+ "userId": 1,
140
+ },
141
+ ],
142
+ ],
143
+ }
144
+ `;
145
+
146
+ exports[`OAuth2 server with the code flow should redirect with a code 2`] = `
147
+ Object {
148
+ "authenticationCheckCalls": Array [
149
+ Array [
150
+ "bearer",
151
+ Object {
152
+ "hash": "yolo",
153
+ },
154
+ ],
155
+ ],
156
+ "checkApplicationCalls": Array [
157
+ Array [
158
+ Object {
159
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
160
+ "scope": "",
161
+ "type": "root",
162
+ },
163
+ ],
164
+ ],
165
+ "oAuth2AccessTokenCheckCalls": Array [],
166
+ "oAuth2AccessTokenCreateCalls": Array [],
167
+ "oAuth2ClientCredentialsCheckCalls": Array [],
168
+ "oAuth2CodeCheckCalls": Array [],
169
+ "oAuth2CodeCreateCalls": Array [
170
+ Array [
171
+ Object {
172
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
173
+ "scope": "user",
174
+ },
175
+ "http://redirect.example.com/yolo?a_param=a_value",
176
+ Object {},
177
+ ],
178
+ ],
179
+ "oAuth2PasswordCheckCalls": Array [],
180
+ "oAuth2RefreshTokenCheckCalls": Array [],
181
+ "oAuth2RefreshTokenCreateCalls": Array [],
182
+ }
183
+ `;
184
+
185
+ exports[`OAuth2 server with the implicit flow should build the authorization redirection 2`] = `
186
+ Object {
187
+ "authenticationCheckCalls": Array [],
188
+ "checkApplicationCalls": Array [
189
+ Array [
190
+ Object {
191
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
192
+ "redirectURI": "http://redirect.example.com/yolo",
193
+ "scope": "user",
194
+ "type": "token",
195
+ },
196
+ ],
197
+ ],
198
+ "oAuth2AccessTokenCheckCalls": Array [],
199
+ "oAuth2AccessTokenCreateCalls": Array [],
200
+ "oAuth2ClientCredentialsCheckCalls": Array [],
201
+ "oAuth2CodeCheckCalls": Array [],
202
+ "oAuth2CodeCreateCalls": Array [],
203
+ "oAuth2PasswordCheckCalls": Array [],
204
+ "oAuth2RefreshTokenCheckCalls": Array [],
205
+ "oAuth2RefreshTokenCreateCalls": Array [],
206
+ }
207
+ `;
208
+
209
+ exports[`OAuth2 server with the implicit flow should redirect with a token 2`] = `
210
+ Object {
211
+ "authenticationCheckCalls": Array [
212
+ Array [
213
+ "bearer",
214
+ Object {
215
+ "hash": "yolo",
216
+ },
217
+ ],
218
+ ],
219
+ "checkApplicationCalls": Array [
220
+ Array [
221
+ Object {
222
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
223
+ "scope": "",
224
+ "type": "root",
225
+ },
226
+ ],
227
+ Array [
228
+ Object {
229
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
230
+ "redirectURI": "http://redirect.example.com/yolo?a_param=a_value",
231
+ "scope": "user",
232
+ "type": "token",
233
+ },
234
+ ],
235
+ ],
236
+ "oAuth2AccessTokenCheckCalls": Array [],
237
+ "oAuth2AccessTokenCreateCalls": Array [
238
+ Array [
239
+ Object {
240
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
241
+ "scope": "user,oauth",
242
+ },
243
+ Object {
244
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
245
+ "scope": "user",
246
+ },
247
+ Object {},
248
+ ],
249
+ ],
250
+ "oAuth2ClientCredentialsCheckCalls": Array [],
251
+ "oAuth2CodeCheckCalls": Array [],
252
+ "oAuth2CodeCreateCalls": Array [],
253
+ "oAuth2PasswordCheckCalls": Array [],
254
+ "oAuth2RefreshTokenCheckCalls": Array [],
255
+ "oAuth2RefreshTokenCreateCalls": Array [],
256
+ }
257
+ `;
258
+
259
+ exports[`OAuth2 server with the password flow should produce new tokens 2`] = `
260
+ Object {
261
+ "authenticationCheckCalls": Array [
262
+ Array [
263
+ "basic",
264
+ Object {
265
+ "hash": "YWxpOm9wZW5fc2VzYW1l",
266
+ "password": "open_sesame",
267
+ "username": "ali",
268
+ },
269
+ ],
270
+ ],
271
+ "checkApplicationCalls": Array [
272
+ Array [
273
+ Object {
274
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
275
+ "scope": "user",
276
+ "type": "password",
277
+ },
278
+ ],
279
+ ],
280
+ "oAuth2AccessTokenCheckCalls": Array [],
281
+ "oAuth2AccessTokenCreateCalls": Array [
282
+ Array [
283
+ Object {
284
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
285
+ "scope": "user,oauth",
286
+ },
287
+ Object {
288
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
289
+ "authentication": Object {
290
+ "userId": 1,
291
+ },
292
+ "scope": "user,auth",
293
+ },
294
+ ],
295
+ ],
296
+ "oAuth2ClientCredentialsCheckCalls": Array [],
297
+ "oAuth2CodeCheckCalls": Array [],
298
+ "oAuth2CodeCreateCalls": Array [],
299
+ "oAuth2PasswordCheckCalls": Array [
300
+ Array [
301
+ Object {
302
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
303
+ "scope": "user,oauth",
304
+ },
305
+ "me@example.com",
306
+ "udelawli",
307
+ "user",
308
+ ],
309
+ ],
310
+ "oAuth2RefreshTokenCheckCalls": Array [],
311
+ "oAuth2RefreshTokenCreateCalls": Array [
312
+ Array [
313
+ Object {
314
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
315
+ "scope": "user,oauth",
316
+ },
317
+ Object {
318
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
319
+ "authentication": Object {
320
+ "userId": 1,
321
+ },
322
+ "scope": "user,auth",
323
+ },
324
+ ],
325
+ ],
326
+ }
327
+ `;
328
+
329
+ exports[`OAuth2 server with the refresh token flow should produce new tokens 2`] = `
330
+ Object {
331
+ "authenticationCheckCalls": Array [
332
+ Array [
333
+ "basic",
334
+ Object {
335
+ "hash": "YWxpOm9wZW5fc2VzYW1l",
336
+ "password": "open_sesame",
337
+ "username": "ali",
338
+ },
339
+ ],
340
+ ],
341
+ "checkApplicationCalls": Array [
342
+ Array [
343
+ Object {
344
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
345
+ "scope": "user",
346
+ "type": "refresh",
347
+ },
348
+ ],
349
+ ],
350
+ "oAuth2AccessTokenCheckCalls": Array [],
351
+ "oAuth2AccessTokenCreateCalls": Array [
352
+ Array [
353
+ Object {
354
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
355
+ "scope": "user,oauth",
356
+ },
357
+ Object {
358
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
359
+ "authentication": Object {
360
+ "userId": 1,
361
+ },
362
+ "scope": "user,auth",
363
+ },
364
+ ],
365
+ ],
366
+ "oAuth2ClientCredentialsCheckCalls": Array [],
367
+ "oAuth2CodeCheckCalls": Array [],
368
+ "oAuth2CodeCreateCalls": Array [],
369
+ "oAuth2PasswordCheckCalls": Array [],
370
+ "oAuth2RefreshTokenCheckCalls": Array [
371
+ Array [
372
+ Object {
373
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
374
+ "scope": "user,oauth",
375
+ },
376
+ "a_refresh_token",
377
+ "user",
378
+ ],
379
+ ],
380
+ "oAuth2RefreshTokenCreateCalls": Array [
381
+ Array [
382
+ Object {
383
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
384
+ "scope": "user,oauth",
385
+ },
386
+ Object {
387
+ "applicationId": "acdc41ce-acdc-41ce-acdc-41ceacdc41ce",
388
+ "authentication": Object {
389
+ "userId": 1,
390
+ },
391
+ "scope": "user,auth",
392
+ },
393
+ ],
394
+ ],
395
+ }
396
+ `;
@@ -0,0 +1,38 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`getOAuth2Authorize should redirect 2`] = `
4
+ Object {
5
+ "codeGranterAcknowledgerAcknowledgeCalls": Array [],
6
+ "codeGranterAuthenticatorAuthenticateCalls": Array [],
7
+ "codeGranterAuthorizerAuthorizeCalls": Array [
8
+ Array [
9
+ Object {
10
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
11
+ "redirectURI": "https://www.example.com",
12
+ "scope": "user",
13
+ },
14
+ Object {},
15
+ ],
16
+ ],
17
+ "logCalls": Array [],
18
+ "tokenGranterAcknowledgerAcknowledgeCalls": Array [],
19
+ "tokenGranterAuthorizerAuthorizeCalls": Array [],
20
+ }
21
+ `;
22
+
23
+ exports[`getOAuth2Authorize should redirect with an error when some 2`] = `
24
+ Object {
25
+ "codeGranterAcknowledgerAcknowledgeCalls": Array [],
26
+ "codeGranterAuthenticatorAuthenticateCalls": Array [],
27
+ "codeGranterAuthorizerAuthorizeCalls": Array [],
28
+ "logCalls": Array [
29
+ Array [
30
+ "debug",
31
+ "👫 - OAuth2 initialization error",
32
+ "E_UNKNOWN_AUTHORIZER_TYPE",
33
+ ],
34
+ ],
35
+ "tokenGranterAcknowledgerAcknowledgeCalls": Array [],
36
+ "tokenGranterAuthorizerAuthorizeCalls": Array [],
37
+ }
38
+ `;
@@ -0,0 +1,61 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`postOAuth2Acknowledge should redirect 2`] = `
4
+ Object {
5
+ "checkApplicationCalls": Array [
6
+ Array [
7
+ Object {
8
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
9
+ "scope": "",
10
+ "type": "root",
11
+ },
12
+ ],
13
+ ],
14
+ "codeGranterAcknowledgerAcknowledgeCalls": Array [
15
+ Array [
16
+ Object {
17
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
18
+ "scope": "auth",
19
+ "userId": 1,
20
+ },
21
+ Object {
22
+ "clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
23
+ "redirectURI": "https://www.example.com",
24
+ "scope": "user",
25
+ },
26
+ Object {},
27
+ ],
28
+ ],
29
+ "codeGranterAuthenticatorAuthenticateCalls": Array [],
30
+ "codeGranterAuthorizerAuthorizeCalls": Array [],
31
+ "logCalls": Array [],
32
+ "tokenGranterAcknowledgerAcknowledgeCalls": Array [],
33
+ "tokenGranterAuthorizerAuthorizeCalls": Array [],
34
+ }
35
+ `;
36
+
37
+ exports[`postOAuth2Acknowledge should redirect errors too 2`] = `
38
+ Object {
39
+ "checkApplicationCalls": Array [
40
+ Array [
41
+ Object {
42
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
43
+ "scope": "",
44
+ "type": "root",
45
+ },
46
+ ],
47
+ ],
48
+ "codeGranterAcknowledgerAcknowledgeCalls": Array [],
49
+ "codeGranterAuthenticatorAuthenticateCalls": Array [],
50
+ "codeGranterAuthorizerAuthorizeCalls": Array [],
51
+ "logCalls": Array [
52
+ Array [
53
+ "debug",
54
+ "👫 - OAuth2 acknowledge error",
55
+ "E_UNKNOWN_ACKNOWLEDGOR_TYPE",
56
+ ],
57
+ ],
58
+ "tokenGranterAcknowledgerAcknowledgeCalls": Array [],
59
+ "tokenGranterAuthorizerAuthorizeCalls": Array [],
60
+ }
61
+ `;
@@ -0,0 +1,52 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`postOAuth2Token should create a token 1`] = `
4
+ Object {
5
+ "logCalls": Array [],
6
+ "oAuth2AccessTokenCreateCalls": Array [
7
+ Array [
8
+ Object {
9
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
10
+ "scope": "user",
11
+ },
12
+ undefined,
13
+ ],
14
+ ],
15
+ "oAuth2RefreshTokenCreateCalls": Array [
16
+ Array [
17
+ Object {
18
+ "applicationId": "abbacaca-abba-caca-abba-cacaabbacaca",
19
+ "scope": "user",
20
+ },
21
+ undefined,
22
+ ],
23
+ ],
24
+ "response": Object {
25
+ "body": Object {
26
+ "access_token": "an_access_token",
27
+ "expiration_date": "2010-03-07T00:00:00.000Z",
28
+ "expires_in": 86400,
29
+ "refresh_token": "a_refresh_token",
30
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
31
+ "refresh_token_expires_in": 5364748800,
32
+ "token_type": "bearer",
33
+ },
34
+ "headers": Object {},
35
+ "status": 200,
36
+ },
37
+ }
38
+ `;
39
+
40
+ exports[`postOAuth2Token should fail with a bad grant type 2`] = `
41
+ Object {
42
+ "logCalls": Array [
43
+ Array [
44
+ "debug",
45
+ "👫 - OAuth2 token issuing error",
46
+ "E_UNKNOWN_AUTHENTICATOR_TYPE",
47
+ ],
48
+ ],
49
+ "oAuth2AccessTokenCreateCalls": Array [],
50
+ "oAuth2RefreshTokenCreateCalls": Array [],
51
+ }
52
+ `;
@@ -0,0 +1,142 @@
1
+ import initGetOAuth2Authorize from './getOAuth2Authorize';
2
+ import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
3
+ import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
4
+ import YError from 'yerror';
5
+ import type { OAuth2Options, OAuth2GranterService } from '..';
6
+
7
+ describe('getOAuth2Authorize', () => {
8
+ const OAUTH2: OAuth2Options = {
9
+ authenticateURL: 'https://auth.example.com/sign_in',
10
+ };
11
+ const ERRORS_DESCRIPTORS = {
12
+ ...DEFAULT_ERRORS_DESCRIPTORS,
13
+ ...OAUTH2_ERRORS_DESCRIPTORS,
14
+ };
15
+ const log = jest.fn();
16
+ const codeGranter = {
17
+ type: 'code',
18
+ authorizer: { responseType: 'code', authorize: jest.fn() },
19
+ acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },
20
+ authenticator: {
21
+ grantType: 'authorization_code',
22
+ authenticate: jest.fn(),
23
+ },
24
+ };
25
+ const tokenGranter = {
26
+ type: 'token',
27
+ authorizer: { responseType: 'token', authorize: jest.fn() },
28
+ acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },
29
+ };
30
+ const oAuth2Granters: OAuth2GranterService[] = [codeGranter, tokenGranter];
31
+
32
+ beforeEach(() => {
33
+ log.mockReset();
34
+ [
35
+ codeGranter.authorizer.authorize,
36
+ codeGranter.acknowledger.acknowledge,
37
+ codeGranter.authenticator.authenticate,
38
+ tokenGranter.authorizer.authorize,
39
+ tokenGranter.acknowledger.acknowledge,
40
+ ].forEach((mock) => mock.mockReset());
41
+ });
42
+
43
+ test('should redirect', async () => {
44
+ [
45
+ codeGranter.acknowledger.acknowledge,
46
+ codeGranter.authenticator.authenticate,
47
+ tokenGranter.authorizer.authorize,
48
+ tokenGranter.acknowledger.acknowledge,
49
+ ].forEach((mock) =>
50
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
51
+ );
52
+ codeGranter.authorizer.authorize.mockResolvedValueOnce({
53
+ applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
54
+ redirectURI: 'http://lol',
55
+ scope: 'user',
56
+ });
57
+
58
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
59
+ OAUTH2,
60
+ ERRORS_DESCRIPTORS,
61
+ oAuth2Granters,
62
+ log,
63
+ });
64
+ const response = await getOAuth2Authorize({
65
+ response_type: 'code',
66
+ client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
67
+ redirect_uri: 'https://www.example.com',
68
+ scope: 'user',
69
+ state: 'bancal',
70
+ });
71
+
72
+ expect(response).toMatchInlineSnapshot(`
73
+ Object {
74
+ "headers": Object {
75
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=http%3A%2F%2Flol&scope=user&client_id=abbacaca-abba-caca-abba-cacaabbacaca&state=bancal",
76
+ },
77
+ "status": 302,
78
+ }
79
+ `);
80
+ expect({
81
+ logCalls: log.mock.calls.filter((args) => args[0].endsWith('stack')),
82
+ codeGranterAuthorizerAuthorizeCalls:
83
+ codeGranter.authorizer.authorize.mock.calls,
84
+ codeGranterAcknowledgerAcknowledgeCalls:
85
+ codeGranter.acknowledger.acknowledge.mock.calls,
86
+ codeGranterAuthenticatorAuthenticateCalls:
87
+ codeGranter.authenticator.authenticate.mock.calls,
88
+ tokenGranterAuthorizerAuthorizeCalls:
89
+ tokenGranter.authorizer.authorize.mock.calls,
90
+ tokenGranterAcknowledgerAcknowledgeCalls:
91
+ tokenGranter.acknowledger.acknowledge.mock.calls,
92
+ }).toMatchSnapshot();
93
+ });
94
+
95
+ test('should redirect with an error when some', async () => {
96
+ [
97
+ codeGranter.authorizer.authorize,
98
+ codeGranter.acknowledger.acknowledge,
99
+ codeGranter.authenticator.authenticate,
100
+ tokenGranter.authorizer.authorize,
101
+ tokenGranter.acknowledger.acknowledge,
102
+ ].forEach((mock) =>
103
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
104
+ );
105
+
106
+ const getOAuth2Authorize = await initGetOAuth2Authorize({
107
+ OAUTH2,
108
+ ERRORS_DESCRIPTORS,
109
+ oAuth2Granters,
110
+ log,
111
+ });
112
+ const response = await getOAuth2Authorize({
113
+ response_type: 'yolo',
114
+ client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
115
+ redirect_uri: 'https://www.example.com',
116
+ scope: 'user',
117
+ state: 'bancal',
118
+ });
119
+
120
+ expect(response).toMatchInlineSnapshot(`
121
+ Object {
122
+ "headers": Object {
123
+ "location": "https://auth.example.com/sign_in?redirect_uri=https%3A%2F%2Fwww.example.com&error=unsupported_response_type&error_decription=The+type+%22yolo%22+is+not+supported.&state=bancal",
124
+ },
125
+ "status": 302,
126
+ }
127
+ `);
128
+ expect({
129
+ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
130
+ codeGranterAuthorizerAuthorizeCalls:
131
+ codeGranter.authorizer.authorize.mock.calls,
132
+ codeGranterAcknowledgerAcknowledgeCalls:
133
+ codeGranter.acknowledger.acknowledge.mock.calls,
134
+ codeGranterAuthenticatorAuthenticateCalls:
135
+ codeGranter.authenticator.authenticate.mock.calls,
136
+ tokenGranterAuthorizerAuthorizeCalls:
137
+ tokenGranter.authorizer.authorize.mock.calls,
138
+ tokenGranterAcknowledgerAcknowledgeCalls:
139
+ tokenGranter.acknowledger.acknowledge.mock.calls,
140
+ }).toMatchSnapshot();
141
+ });
142
+ });