@verdaccio/auth 8.0.0-next-8.11 → 8.0.0-next-8.13

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 (39) hide show
  1. package/README.md +30 -24
  2. package/package.json +16 -12
  3. package/.babelrc +0 -3
  4. package/CHANGELOG.md +0 -1755
  5. package/src/auth.ts +0 -607
  6. package/src/index.ts +0 -3
  7. package/src/signature-legacy.ts +0 -66
  8. package/src/types.ts +0 -46
  9. package/src/utils.ts +0 -252
  10. package/test/auth-utils-middleware.spec.ts +0 -260
  11. package/test/auth-utils.spec.ts +0 -362
  12. package/test/auth.spec.ts +0 -722
  13. package/test/helper/plugin.ts +0 -43
  14. package/test/partials/config/yaml/security/security-basic.yaml +0 -12
  15. package/test/partials/config/yaml/security/security-empty.yaml +0 -1
  16. package/test/partials/config/yaml/security/security-jwt-legacy-enabled.yaml +0 -10
  17. package/test/partials/config/yaml/security/security-jwt.yaml +0 -6
  18. package/test/partials/config/yaml/security/security-legacy-disabled.yaml +0 -3
  19. package/test/partials/config/yaml/security/security-legacy.yaml +0 -3
  20. package/test/partials/config/yaml/security/security-missing.yaml +0 -0
  21. package/test/partials/config/yaml/security/security-no-legacy.yaml +0 -9
  22. package/test/partials/plugin/verdaccio-access-ok/access.js +0 -9
  23. package/test/partials/plugin/verdaccio-access-ok/package.json +0 -5
  24. package/test/partials/plugin/verdaccio-adduser/adduser.js +0 -25
  25. package/test/partials/plugin/verdaccio-adduser/package.json +0 -5
  26. package/test/partials/plugin/verdaccio-adduser-legacy/adduser.js +0 -25
  27. package/test/partials/plugin/verdaccio-adduser-legacy/package.json +0 -5
  28. package/test/partials/plugin/verdaccio-change-password/change.js +0 -32
  29. package/test/partials/plugin/verdaccio-change-password/package.json +0 -5
  30. package/test/partials/plugin/verdaccio-fail/fail.js +0 -14
  31. package/test/partials/plugin/verdaccio-fail/package.json +0 -5
  32. package/test/partials/plugin/verdaccio-fail-invalid-method/fail.js +0 -11
  33. package/test/partials/plugin/verdaccio-fail-invalid-method/package.json +0 -5
  34. package/test/partials/plugin/verdaccio-passthroug/package.json +0 -5
  35. package/test/partials/plugin/verdaccio-passthroug/passthroug.js +0 -9
  36. package/test/partials/plugin/verdaccio-success/package.json +0 -5
  37. package/test/partials/plugin/verdaccio-success/success.js +0 -9
  38. package/tsconfig.build.json +0 -9
  39. package/tsconfig.json +0 -32
package/test/auth.spec.ts DELETED
@@ -1,722 +0,0 @@
1
- import express from 'express';
2
- import path from 'path';
3
- import supertest from 'supertest';
4
- import { describe, expect, test, vi } from 'vitest';
5
-
6
- import { Config as AppConfig, ROLES, createRemoteUser, getDefaultConfig } from '@verdaccio/config';
7
- import { HEADERS, HTTP_STATUS, SUPPORT_ERRORS, TOKEN_BEARER, errorUtils } from '@verdaccio/core';
8
- import { logger, setup } from '@verdaccio/logger';
9
- import { errorReportingMiddleware, final, handleError } from '@verdaccio/middleware';
10
- import { Config } from '@verdaccio/types';
11
- import { buildToken } from '@verdaccio/utils';
12
-
13
- import { $RequestExtend, Auth } from '../src';
14
- import {
15
- authChangePasswordConf,
16
- authPluginFailureConf,
17
- authPluginPassThrougConf,
18
- authProfileConf,
19
- } from './helper/plugin';
20
-
21
- setup({});
22
-
23
- // to avoid flaky test generate same ramdom key
24
- vi.mock('@verdaccio/utils', async (importOriginal) => {
25
- return {
26
- ...(await importOriginal<typeof import('@verdaccio/utils')>()),
27
- // used by enhanced legacy aes signature (minimum 32 characters)
28
- generateRandomSecretKey: () => 'GCYW/3IJzQI6GvPmy9sbMkFoiL7QLVw',
29
- // used by legacy aes signature
30
- generateRandomHexString: () =>
31
- 'ff065fcf7a8330ae37d3ea116328852f387ad7aa6defbe47fb68b1ea25f97446',
32
- };
33
- });
34
-
35
- describe('AuthTest', () => {
36
- describe('default', () => {
37
- test('should init correctly', async () => {
38
- const config: Config = new AppConfig({ ...authProfileConf });
39
- config.checkSecretKey('12345');
40
-
41
- const auth: Auth = new Auth(config, logger);
42
- await auth.init();
43
- expect(auth).toBeDefined();
44
- });
45
-
46
- test('should load default auth plugin', async () => {
47
- const config: Config = new AppConfig({ ...authProfileConf, auth: undefined });
48
- config.checkSecretKey('12345');
49
-
50
- const auth: Auth = new Auth(config, logger);
51
- await auth.init();
52
- expect(auth).toBeDefined();
53
- });
54
- });
55
-
56
- describe('utils', () => {
57
- test('should load custom algorithm', async () => {
58
- const config: Config = new AppConfig({
59
- ...authProfileConf,
60
- auth: { htpasswd: { algorithm: 'sha1', file: './foo' } },
61
- });
62
- config.checkSecretKey('12345');
63
-
64
- const auth: Auth = new Auth(config, logger);
65
- await auth.init();
66
- expect(auth).toBeDefined();
67
- });
68
- });
69
-
70
- describe('authenticate', () => {
71
- describe('test authenticate states', () => {
72
- test('should be a success login', async () => {
73
- const config: Config = new AppConfig({ ...authProfileConf });
74
- config.checkSecretKey('12345');
75
- const auth: Auth = new Auth(config, logger);
76
- await auth.init();
77
- expect(auth).toBeDefined();
78
-
79
- const callback = vi.fn();
80
- const groups = ['test'];
81
-
82
- auth.authenticate('foo', 'bar', callback);
83
-
84
- expect(callback).toHaveBeenCalledTimes(1);
85
- expect(callback).toHaveBeenCalledWith(null, {
86
- groups: [
87
- 'test',
88
- ROLES.$ALL,
89
- ROLES.$AUTH,
90
- ROLES.DEPRECATED_ALL,
91
- ROLES.DEPRECATED_AUTH,
92
- ROLES.ALL,
93
- ],
94
- name: 'foo',
95
- real_groups: groups,
96
- });
97
- });
98
-
99
- test('should be a fail on login', async () => {
100
- const config: Config = new AppConfig(authPluginFailureConf);
101
- config.checkSecretKey('12345');
102
- const auth: Auth = new Auth(config, logger);
103
- await auth.init();
104
- expect(auth).toBeDefined();
105
-
106
- const callback = vi.fn();
107
-
108
- auth.authenticate('foo', 'bar', callback);
109
- expect(callback).toHaveBeenCalledTimes(1);
110
- expect(callback).toHaveBeenCalledWith(errorUtils.getInternalError());
111
- });
112
- });
113
-
114
- // plugins are free to send whatever they want, so, we need to test some scenarios
115
- // that might make break the request
116
- // the @ts-ignore below are intended
117
- describe('test authenticate out of control inputs from plugins', () => {
118
- test('should skip falsy values', async () => {
119
- const config: Config = new AppConfig({ ...authPluginPassThrougConf });
120
- config.checkSecretKey('12345');
121
- const auth: Auth = new Auth(config, logger);
122
- await auth.init();
123
- expect(auth).toBeDefined();
124
-
125
- const callback = vi.fn();
126
- let index = 0;
127
-
128
- // as defined by https://developer.mozilla.org/en-US/docs/Glossary/Falsy
129
- for (const value of [false, 0, '', null, undefined, NaN]) {
130
- // @ts-ignore
131
- auth.authenticate(null, value, callback);
132
- const call = callback.mock.calls[index++];
133
- expect(call[0]).toBeDefined();
134
- expect(call[1]).toBeUndefined();
135
- }
136
- });
137
-
138
- test('should error truthy non-array', async () => {
139
- const config: Config = new AppConfig({ ...authPluginPassThrougConf });
140
- config.checkSecretKey('12345');
141
- const auth: Auth = new Auth(config, logger);
142
- await auth.init();
143
- expect(auth).toBeDefined();
144
-
145
- const callback = vi.fn();
146
-
147
- for (const value of [true, 1, 'test', {}]) {
148
- expect(function () {
149
- // @ts-ignore
150
- auth.authenticate(null, value, callback);
151
- }).toThrow(TypeError);
152
- expect(callback).not.toHaveBeenCalled();
153
- }
154
- });
155
-
156
- test('should skip empty array', async () => {
157
- const config: Config = new AppConfig({ ...authPluginPassThrougConf });
158
- config.checkSecretKey('12345');
159
- const auth: Auth = new Auth(config, logger);
160
- await auth.init();
161
- expect(auth).toBeDefined();
162
-
163
- const callback = vi.fn();
164
- const value = [];
165
-
166
- // @ts-ignore
167
- auth.authenticate(null, value, callback);
168
- expect(callback.mock.calls).toHaveLength(1);
169
- expect(callback.mock.calls[0][0]).toBeDefined();
170
- expect(callback.mock.calls[0][1]).toBeUndefined();
171
- });
172
-
173
- test('should accept valid array', async () => {
174
- const config: Config = new AppConfig({ ...authPluginPassThrougConf });
175
- config.checkSecretKey('12345');
176
- const auth: Auth = new Auth(config, logger);
177
- await auth.init();
178
- expect(auth).toBeDefined();
179
-
180
- const callback = vi.fn();
181
- let index = 0;
182
-
183
- for (const value of [[''], ['1'], ['0'], ['000']]) {
184
- // @ts-ignore
185
- auth.authenticate(null, value, callback);
186
- const call = callback.mock.calls[index++];
187
- expect(call[0]).toBeNull();
188
- expect(call[1].real_groups).toBe(value);
189
- }
190
- });
191
- });
192
-
193
- describe('test multiple authenticate methods', () => {
194
- test('should skip falsy values', async () => {
195
- const config: Config = new AppConfig({
196
- ...getDefaultConfig(),
197
- plugins: path.join(__dirname, './partials/plugin'),
198
- auth: {
199
- success: {},
200
- 'fail-invalid-method': {},
201
- },
202
- });
203
- config.checkSecretKey('12345');
204
- const auth: Auth = new Auth(config, logger);
205
- await auth.init();
206
-
207
- return new Promise((resolve) => {
208
- auth.authenticate('foo', 'bar', (err, value) => {
209
- expect(value).toEqual({
210
- name: 'foo',
211
- groups: ['test', ROLES.$ALL, '$authenticated', '@all', '@authenticated', 'all'],
212
- real_groups: ['test'],
213
- });
214
- resolve(value);
215
- });
216
- });
217
- });
218
- });
219
- });
220
-
221
- describe('changePassword', () => {
222
- test('should fail if the plugin does not provide implementation', async () => {
223
- const config: Config = new AppConfig({ ...authProfileConf });
224
- config.checkSecretKey('12345');
225
- const auth: Auth = new Auth(config, logger);
226
- await auth.init();
227
- expect(auth).toBeDefined();
228
- const callback = vi.fn();
229
-
230
- auth.changePassword('foo', 'bar', 'newFoo', callback);
231
-
232
- expect(callback).toHaveBeenCalledTimes(1);
233
- expect(callback).toHaveBeenCalledWith(
234
- errorUtils.getInternalError(SUPPORT_ERRORS.PLUGIN_MISSING_INTERFACE)
235
- );
236
- });
237
- test('should handle plugin does provide implementation', async () => {
238
- const config: Config = new AppConfig({ ...authChangePasswordConf });
239
- config.checkSecretKey('12345');
240
- const auth: Auth = new Auth(config, logger);
241
- await auth.init();
242
- expect(auth).toBeDefined();
243
- const callback = vi.fn();
244
- auth.add_user('foo', 'bar', vi.fn());
245
- auth.changePassword('foo', 'bar', 'newFoo', callback);
246
- expect(callback).toHaveBeenCalledTimes(1);
247
- expect(callback).toHaveBeenCalledWith(null, true);
248
- });
249
- });
250
-
251
- describe('allow_access', () => {
252
- describe('no custom allow_access implementation provided', () => {
253
- // when allow_access is not implemented, the groups must match
254
- // exactly with the packages access group
255
- test('should fail if groups do not match exactly', async () => {
256
- const config: Config = new AppConfig({ ...authProfileConf });
257
- config.checkSecretKey('12345');
258
- const auth: Auth = new Auth(config, logger);
259
- await auth.init();
260
- expect(auth).toBeDefined();
261
-
262
- const callback = vi.fn();
263
- const groups = ['test'];
264
-
265
- auth.allow_access(
266
- { packageName: 'foo' },
267
- { name: 'foo', groups, real_groups: groups },
268
- callback
269
- );
270
-
271
- expect(callback).toHaveBeenCalledTimes(1);
272
- expect(callback).toHaveBeenCalledWith(
273
- errorUtils.getForbidden('user foo is not allowed to access package foo')
274
- );
275
- });
276
-
277
- test('should success if groups do not match exactly', async () => {
278
- const config: Config = new AppConfig({ ...authProfileConf });
279
- config.checkSecretKey('12345');
280
- const auth: Auth = new Auth(config, logger);
281
- await auth.init();
282
- expect(auth).toBeDefined();
283
-
284
- const callback = vi.fn();
285
- // $all comes from configuration file
286
- const groups = [ROLES.$ALL];
287
-
288
- auth.allow_access(
289
- { packageName: 'foo' },
290
- { name: 'foo', groups, real_groups: groups },
291
- callback
292
- );
293
-
294
- expect(callback).toHaveBeenCalledTimes(1);
295
- expect(callback).toHaveBeenCalledWith(null, true);
296
- });
297
- });
298
- });
299
-
300
- describe('allow_publish', () => {
301
- describe('no custom allow_publish implementation provided', () => {
302
- // when allow_access is not implemented, the groups must match
303
- // exactly with the packages access group
304
- test('should fail if groups do not match exactly', async () => {
305
- const config: Config = new AppConfig({ ...authProfileConf });
306
- config.checkSecretKey('12345');
307
- const auth: Auth = new Auth(config, logger);
308
- await auth.init();
309
- expect(auth).toBeDefined();
310
-
311
- const callback = vi.fn();
312
- const groups = ['test'];
313
-
314
- auth.allow_publish(
315
- { packageName: 'foo' },
316
- { name: 'foo', groups, real_groups: groups },
317
- callback
318
- );
319
-
320
- expect(callback).toHaveBeenCalledTimes(1);
321
- expect(callback).toHaveBeenCalledWith(
322
- errorUtils.getForbidden('user foo is not allowed to publish package foo')
323
- );
324
- });
325
-
326
- test('should success if groups do match exactly', async () => {
327
- const config: Config = new AppConfig({ ...authProfileConf });
328
- config.checkSecretKey('12345');
329
- const auth: Auth = new Auth(config, logger);
330
- await auth.init();
331
- expect(auth).toBeDefined();
332
-
333
- const callback = vi.fn();
334
- // $all comes from configuration file
335
- const groups = [ROLES.$AUTH];
336
-
337
- auth.allow_publish(
338
- { packageName: 'foo' },
339
- { name: 'foo', groups, real_groups: groups },
340
- callback
341
- );
342
-
343
- expect(callback).toHaveBeenCalledTimes(1);
344
- expect(callback).toHaveBeenCalledWith(null, true);
345
- });
346
- });
347
- });
348
- describe('allow_unpublish', () => {
349
- describe('no custom allow_unpublish implementation provided', () => {
350
- test('should fail if groups do not match exactly', async () => {
351
- const config: Config = new AppConfig({ ...authProfileConf });
352
- config.checkSecretKey('12345');
353
-
354
- const auth: Auth = new Auth(config, logger);
355
- await auth.init();
356
- expect(auth).toBeDefined();
357
-
358
- const callback = vi.fn();
359
- const groups = ['test'];
360
-
361
- auth.allow_unpublish(
362
- { packageName: 'foo' },
363
- { name: 'foo', groups, real_groups: groups },
364
- callback
365
- );
366
-
367
- expect(callback).toHaveBeenCalledTimes(1);
368
- expect(callback).toHaveBeenCalledWith(
369
- errorUtils.getForbidden('user foo is not allowed to unpublish package foo')
370
- );
371
- });
372
-
373
- test('should handle missing unpublish method (special case to handle legacy configurations)', async () => {
374
- const config: Config = new AppConfig({
375
- ...authProfileConf,
376
- packages: {
377
- ...authProfileConf.packages,
378
- '**': {
379
- access: ['$all'],
380
- publish: ['$authenticated'],
381
- // it forces publish handle the access
382
- unpublish: undefined,
383
- proxy: ['npmjs'],
384
- },
385
- },
386
- });
387
- config.checkSecretKey('12345');
388
- const auth: Auth = new Auth(config, logger);
389
- await auth.init();
390
- expect(auth).toBeDefined();
391
-
392
- const callback = vi.fn();
393
- const groups = ['test'];
394
-
395
- auth.allow_unpublish(
396
- { packageName: 'foo' },
397
- { name: 'foo', groups, real_groups: groups },
398
- callback
399
- );
400
-
401
- expect(callback).toHaveBeenCalledTimes(1);
402
- expect(callback).toHaveBeenCalledWith(
403
- errorUtils.getForbidden('user foo is not allowed to publish package foo')
404
- );
405
- });
406
-
407
- test('should success if groups do match exactly', async () => {
408
- const config: Config = new AppConfig({ ...authProfileConf });
409
-
410
- config.checkSecretKey('12345');
411
- const auth: Auth = new Auth(config, logger);
412
- await auth.init();
413
- expect(auth).toBeDefined();
414
-
415
- const callback = vi.fn();
416
- // $all comes from configuration file
417
- const groups = [ROLES.$AUTH];
418
-
419
- auth.allow_unpublish(
420
- { packageName: 'foo' },
421
- { name: 'foo', groups, real_groups: groups },
422
- callback
423
- );
424
-
425
- expect(callback).toHaveBeenCalledTimes(1);
426
- expect(callback).toHaveBeenCalledWith(null, true);
427
- });
428
- });
429
- });
430
-
431
- describe('add_user', () => {
432
- describe('error handling', () => {
433
- // when allow_access is not implemented, the groups must match
434
- // exactly with the packages access group
435
- test('should not fail if adduser is not implemented', async () => {
436
- const config: Config = new AppConfig({ ...authProfileConf });
437
- config.checkSecretKey('12345');
438
- const auth: Auth = new Auth(config, logger);
439
- await auth.init();
440
- expect(auth).toBeDefined();
441
-
442
- const callback = vi.fn();
443
-
444
- auth.add_user('juan', 'password', callback);
445
-
446
- expect(callback).toHaveBeenCalledTimes(1);
447
- expect(callback).toHaveBeenCalledWith(null, {
448
- groups: [
449
- 'test',
450
- ROLES.$ALL,
451
- ROLES.$AUTH,
452
- ROLES.DEPRECATED_ALL,
453
- ROLES.DEPRECATED_AUTH,
454
- ROLES.ALL,
455
- ],
456
- name: 'juan',
457
- real_groups: ['test'],
458
- });
459
- });
460
-
461
- test('should fail if adduser fails internally (exception)', async () => {
462
- const config: Config = new AppConfig({
463
- ...getDefaultConfig(),
464
- plugins: path.join(__dirname, './partials/plugin'),
465
- auth: {
466
- adduser: {},
467
- },
468
- });
469
- config.checkSecretKey('12345');
470
- const auth: Auth = new Auth(config, logger);
471
- await auth.init();
472
- expect(auth).toBeDefined();
473
-
474
- const callback = vi.fn();
475
-
476
- // note: username to make plugin fail
477
- auth.add_user('fail', 'password', callback);
478
-
479
- expect(callback).toHaveBeenCalledTimes(1);
480
- expect(callback).toHaveBeenCalledWith(new Error('bad username'));
481
- });
482
-
483
- test('should skip to the next plugin and fail', async () => {
484
- const config: Config = new AppConfig({
485
- ...getDefaultConfig(),
486
- plugins: path.join(__dirname, './partials/plugin'),
487
- auth: {
488
- adduser: {},
489
- // plugin implement adduser with fail auth
490
- fail: {},
491
- },
492
- });
493
- config.checkSecretKey('12345');
494
- const auth: Auth = new Auth(config, logger);
495
- await auth.init();
496
- expect(auth).toBeDefined();
497
-
498
- const callback = vi.fn();
499
-
500
- // note: username to make plugin fail
501
- auth.add_user('skip', 'password', callback);
502
-
503
- expect(callback).toHaveBeenCalledTimes(1);
504
- expect(callback).toHaveBeenCalledWith(null, {
505
- groups: [
506
- 'test',
507
- ROLES.$ALL,
508
- ROLES.$AUTH,
509
- ROLES.DEPRECATED_ALL,
510
- ROLES.DEPRECATED_AUTH,
511
- ROLES.ALL,
512
- ],
513
- name: 'skip',
514
- real_groups: ['test'],
515
- });
516
- });
517
- });
518
- test('should success if adduser', async () => {
519
- const config: Config = new AppConfig({
520
- ...getDefaultConfig(),
521
- plugins: path.join(__dirname, './partials/plugin'),
522
- auth: {
523
- adduser: {},
524
- },
525
- });
526
- config.checkSecretKey('12345');
527
- const auth: Auth = new Auth(config, logger);
528
- await auth.init();
529
- expect(auth).toBeDefined();
530
-
531
- const callback = vi.fn();
532
-
533
- auth.add_user('something', 'password', callback);
534
-
535
- expect(callback).toHaveBeenCalledTimes(1);
536
- expect(callback).toHaveBeenCalledWith(null, {
537
- groups: ['test', '$all', '$authenticated', '@all', '@authenticated', 'all'],
538
- name: 'something',
539
- real_groups: ['test'],
540
- });
541
- });
542
- test('should handle legacy add_user method', async () => {
543
- const config: Config = new AppConfig({
544
- ...getDefaultConfig(),
545
- plugins: path.join(__dirname, './partials/plugin'),
546
- auth: {
547
- 'adduser-legacy': {},
548
- },
549
- });
550
- config.checkSecretKey('12345');
551
- const auth: Auth = new Auth(config, logger);
552
- await auth.init();
553
- expect(auth).toBeDefined();
554
-
555
- const callback = vi.fn();
556
-
557
- auth.add_user('something', 'password', callback);
558
-
559
- expect(callback).toHaveBeenCalledTimes(1);
560
- expect(callback).toHaveBeenCalledWith(null, {
561
- groups: ['test', '$all', '$authenticated', '@all', '@authenticated', 'all'],
562
- name: 'something',
563
- real_groups: ['test'],
564
- });
565
- });
566
- });
567
- describe('middlewares', () => {
568
- describe('apiJWTmiddleware', () => {
569
- const secret = '12345';
570
- const getServer = async function (auth) {
571
- const app = express();
572
- app.use(express.json({ strict: false, limit: '10mb' }));
573
-
574
- app.use(auth.apiJWTmiddleware());
575
- // @ts-expect-error
576
- app.use(errorReportingMiddleware(logger));
577
- app.get('/*', (req, res, next) => {
578
- if ((req as $RequestExtend).remote_user.error) {
579
- next(new Error((req as $RequestExtend).remote_user.error));
580
- } else {
581
- // @ts-expect-error
582
- next({ user: req?.remote_user });
583
- }
584
- });
585
- // @ts-expect-error
586
- app.use(handleError(logger));
587
- // @ts-expect-error
588
- app.use(final);
589
- return app;
590
- };
591
-
592
- describe('legacy signature', () => {
593
- describe('error cases', () => {
594
- test('should handle invalid auth token', async () => {
595
- const config: Config = new AppConfig({ ...authProfileConf });
596
- config.checkSecretKey(secret);
597
- const auth = new Auth(config, logger);
598
- await auth.init();
599
- const app = await getServer(auth);
600
- return supertest(app)
601
- .get(`/`)
602
- .set(HEADERS.AUTHORIZATION, 'Bearer foo')
603
- .expect(HTTP_STATUS.INTERNAL_ERROR);
604
- });
605
-
606
- test('should handle missing auth header', async () => {
607
- const config: Config = new AppConfig({ ...authProfileConf });
608
- config.checkSecretKey(secret);
609
- const auth = new Auth(config, logger);
610
- await auth.init();
611
- const app = await getServer(auth);
612
- return supertest(app).get(`/`).expect(HTTP_STATUS.OK);
613
- });
614
- });
615
-
616
- describe('deprecated legacy handling', () => {
617
- test('should handle valid auth token', async () => {
618
- const payload = 'juan:password';
619
- // const token = await signPayload(remoteUser, '12345');
620
- const config: Config = new AppConfig({ ...authProfileConf });
621
- // intended to force key generator (associated with mocks above)
622
- // 64 characters secret long
623
- config.checkSecretKey('35fabdd29b820d39125e76e6d85cc294');
624
- const auth = new Auth(config, logger);
625
- await auth.init();
626
- const token = auth.aesEncrypt(payload) as string;
627
- const app = await getServer(auth);
628
- const res = await supertest(app)
629
- .get(`/`)
630
- .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
631
- .expect(HTTP_STATUS.OK);
632
- expect(res.body.user.name).toEqual('juan');
633
- });
634
-
635
- test('should handle invalid auth token', async () => {
636
- const payload = 'juan:password';
637
- const config: Config = new AppConfig({ ...authPluginFailureConf });
638
- // intended to force key generator (associated with mocks above)
639
- config.checkSecretKey(undefined);
640
- const auth = new Auth(config, logger);
641
- await auth.init();
642
- const token = auth.aesEncrypt(payload) as string;
643
- const app = await getServer(auth);
644
- return await supertest(app)
645
- .get(`/`)
646
- .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
647
- .expect(HTTP_STATUS.INTERNAL_ERROR);
648
- });
649
- });
650
- });
651
- describe('jwt signature', () => {
652
- describe('error cases', () => {
653
- test('should handle invalid auth token and return anonymous', async () => {
654
- // @ts-expect-error
655
- const config: Config = new AppConfig({
656
- ...authProfileConf,
657
- ...{ security: { api: { jwt: { sign: { expiresIn: '29d' } } } } },
658
- });
659
- config.checkSecretKey(secret);
660
- const auth = new Auth(config, logger);
661
- await auth.init();
662
- const app = await getServer(auth);
663
- const res = await supertest(app)
664
- .get(`/`)
665
- .set(HEADERS.AUTHORIZATION, 'Bearer foo')
666
- .expect(HTTP_STATUS.OK);
667
- expect(res.body.user.groups).toEqual([
668
- ROLES.$ALL,
669
- ROLES.$ANONYMOUS,
670
- ROLES.DEPRECATED_ALL,
671
- ROLES.DEPRECATED_ANONYMOUS,
672
- ]);
673
- });
674
-
675
- test('should handle missing auth header', async () => {
676
- // @ts-expect-error
677
- const config: Config = new AppConfig({
678
- ...authProfileConf,
679
- ...{ security: { api: { jwt: { sign: { expiresIn: '29d' } } } } },
680
- });
681
- config.checkSecretKey(secret);
682
- const auth = new Auth(config, logger);
683
- await auth.init();
684
- const app = await getServer(auth);
685
- const res = await supertest(app).get(`/`).expect(HTTP_STATUS.OK);
686
- expect(res.body.user.groups).toEqual([
687
- ROLES.$ALL,
688
- ROLES.$ANONYMOUS,
689
- ROLES.DEPRECATED_ALL,
690
- ROLES.DEPRECATED_ANONYMOUS,
691
- ]);
692
- });
693
- });
694
- describe('valid signature handlers', () => {
695
- test('should handle valid auth token', async () => {
696
- const config: Config = new AppConfig(
697
- // @ts-expect-error
698
- {
699
- ...authProfileConf,
700
- ...{ security: { api: { jwt: { sign: { expiresIn: '29d' } } } } },
701
- }
702
- );
703
- // intended to force key generator (associated with mocks above)
704
- config.checkSecretKey(undefined);
705
- const auth = new Auth(config, logger);
706
- await auth.init();
707
- const token = (await auth.jwtEncrypt(
708
- createRemoteUser('jwt_user', [ROLES.ALL]),
709
- config.security.api.jwt.sign
710
- )) as string;
711
- const app = await getServer(auth);
712
- const res = await supertest(app)
713
- .get(`/`)
714
- .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
715
- .expect(HTTP_STATUS.OK);
716
- expect(res.body.user.name).toEqual('jwt_user');
717
- });
718
- });
719
- });
720
- });
721
- });
722
- });