|
1 | 1 | import { createRequest, createResponse } from 'node-mocks-http'; |
2 | 2 | import { HttpValidator, NoTokenFoundError } from './http'; |
3 | | -import { CAT, MemoryCTIStore } from '..'; |
| 3 | +import { |
| 4 | + CAT, |
| 5 | + CommonAccessToken, |
| 6 | + ICTIStore, |
| 7 | + ITokenLogger, |
| 8 | + MemoryCTIStore |
| 9 | +} from '..'; |
4 | 10 | import { CommonAccessTokenRenewal } from '../catr'; |
5 | | -import { CloudFrontResponse } from 'aws-lambda'; |
6 | 11 |
|
7 | 12 | describe('HTTP Request CAT Validator', () => { |
8 | 13 | test('fail to validate token in CTA-Common-Access-Token header with wrong signature', async () => { |
@@ -626,6 +631,7 @@ describe('HTTP Request CAT Validator with store', () => { |
626 | 631 | expect(result.claims!.cti).toBe('0b71'); |
627 | 632 | expect(result.count).toBe(1); |
628 | 633 | const result2 = await httpValidator.validateHttpRequest(request, response); |
| 634 | + expect(result2.status).toBe(200); |
629 | 635 | expect(result2.count).toBe(2); |
630 | 636 |
|
631 | 637 | const cfResult = await httpValidator.validateCloudFrontRequest({ |
@@ -695,4 +701,136 @@ describe('HTTP Request CAT Validator with store', () => { |
695 | 701 | expect(result.claims!.cti).toBe('0b71'); |
696 | 702 | expect(result.count).toBeUndefined(); |
697 | 703 | }); |
| 704 | + |
| 705 | + test('pass if a token has a claim that allows replay and it has been used multiple times', async () => { |
| 706 | + const base64encoded = await generator.generateFromJson( |
| 707 | + { |
| 708 | + iss: 'eyevinn', |
| 709 | + catreplay: 0 |
| 710 | + }, |
| 711 | + { |
| 712 | + type: 'mac', |
| 713 | + alg: 'HS256', |
| 714 | + kid: 'Symmetric256', |
| 715 | + generateCwtId: true |
| 716 | + } |
| 717 | + ); |
| 718 | + const httpValidator = new HttpValidator({ |
| 719 | + keys: [ |
| 720 | + { |
| 721 | + kid: 'Symmetric256', |
| 722 | + key: Buffer.from( |
| 723 | + '403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388', |
| 724 | + 'hex' |
| 725 | + ) |
| 726 | + } |
| 727 | + ], |
| 728 | + issuer: 'eyevinn', |
| 729 | + store: new MemoryCTIStore() |
| 730 | + }); |
| 731 | + const request = createRequest({ |
| 732 | + method: 'GET', |
| 733 | + headers: { |
| 734 | + 'CTA-Common-Access-Token': base64encoded |
| 735 | + } |
| 736 | + }); |
| 737 | + const result = await httpValidator.validateHttpRequest(request); |
| 738 | + expect(result.status).toBe(200); |
| 739 | + const result2 = await httpValidator.validateHttpRequest(request); |
| 740 | + expect(result2.status).toBe(200); |
| 741 | + }); |
| 742 | + |
| 743 | + test('fail if a token has a claim that does not allow replay and it has been used multiple times', async () => { |
| 744 | + const base64encoded = await generator.generateFromJson( |
| 745 | + { |
| 746 | + iss: 'eyevinn', |
| 747 | + catreplay: 1 |
| 748 | + }, |
| 749 | + { |
| 750 | + type: 'mac', |
| 751 | + alg: 'HS256', |
| 752 | + kid: 'Symmetric256', |
| 753 | + generateCwtId: true |
| 754 | + } |
| 755 | + ); |
| 756 | + const httpValidator = new HttpValidator({ |
| 757 | + keys: [ |
| 758 | + { |
| 759 | + kid: 'Symmetric256', |
| 760 | + key: Buffer.from( |
| 761 | + '403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388', |
| 762 | + 'hex' |
| 763 | + ) |
| 764 | + } |
| 765 | + ], |
| 766 | + issuer: 'eyevinn', |
| 767 | + store: new MemoryCTIStore() |
| 768 | + }); |
| 769 | + const request = createRequest({ |
| 770 | + method: 'GET', |
| 771 | + headers: { |
| 772 | + 'CTA-Common-Access-Token': base64encoded |
| 773 | + } |
| 774 | + }); |
| 775 | + const result = await httpValidator.validateHttpRequest(request); |
| 776 | + expect(result.status).toBe(200); |
| 777 | + const result2 = await httpValidator.validateHttpRequest(request); |
| 778 | + expect(result2.status).toBe(401); |
| 779 | + }); |
| 780 | + |
| 781 | + test('can provide a simple reuse detection algorithm', async () => { |
| 782 | + const base64encoded = await generator.generateFromJson( |
| 783 | + { |
| 784 | + iss: 'eyevinn', |
| 785 | + catreplay: 2 |
| 786 | + }, |
| 787 | + { |
| 788 | + type: 'mac', |
| 789 | + alg: 'HS256', |
| 790 | + kid: 'Symmetric256', |
| 791 | + generateCwtId: true |
| 792 | + } |
| 793 | + ); |
| 794 | + |
| 795 | + const simpleReuseDetection = async ( |
| 796 | + cat: CommonAccessToken, |
| 797 | + store?: ICTIStore, |
| 798 | + logger?: ITokenLogger |
| 799 | + ) => { |
| 800 | + if (store) { |
| 801 | + const count = await store.getTokenCount(cat); |
| 802 | + // Consider reuse if same token has been used more than 2 times |
| 803 | + // (this is a very naive example) |
| 804 | + return count > 2; |
| 805 | + } |
| 806 | + return true; |
| 807 | + }; |
| 808 | + const httpValidator = new HttpValidator({ |
| 809 | + keys: [ |
| 810 | + { |
| 811 | + kid: 'Symmetric256', |
| 812 | + key: Buffer.from( |
| 813 | + '403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388', |
| 814 | + 'hex' |
| 815 | + ) |
| 816 | + } |
| 817 | + ], |
| 818 | + issuer: 'eyevinn', |
| 819 | + store: new MemoryCTIStore(), |
| 820 | + reuseDetection: simpleReuseDetection |
| 821 | + }); |
| 822 | + |
| 823 | + const request = createRequest({ |
| 824 | + method: 'GET', |
| 825 | + headers: { |
| 826 | + 'CTA-Common-Access-Token': base64encoded |
| 827 | + } |
| 828 | + }); |
| 829 | + const result = await httpValidator.validateHttpRequest(request); |
| 830 | + expect(result.status).toBe(200); |
| 831 | + const result2 = await httpValidator.validateHttpRequest(request); |
| 832 | + expect(result2.status).toBe(200); |
| 833 | + const result3 = await httpValidator.validateHttpRequest(request); |
| 834 | + expect(result3.status).toBe(401); |
| 835 | + }); |
698 | 836 | }); |
0 commit comments