|
| 1 | +import { sign as jwtSign } from 'jsonwebtoken'; |
| 2 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 3 | +import keypair from 'keypair'; |
| 4 | +import passport from 'passport'; |
| 5 | +import { DynamicStrategy } from '../src/lib/dynamicStrategy'; |
| 6 | +import { testServer } from '../src/test/TestServer'; |
| 7 | + |
| 8 | +const keys = keypair(); |
| 9 | + |
| 10 | +const user = { |
| 11 | + chain: 'ETH', |
| 12 | + environmentId: 'fb6dd9d1-09f5-43c3-8a8c-eab6e44c37f9', |
| 13 | + lists: [], |
| 14 | + userId: '382c1002-e9c1-4fc1-b17c-a887b693b940', |
| 15 | + wallet: 'metamask', |
| 16 | + walletPublicKey: '0x9249Ecdc1c83e5479289e0bDD9AB96738C51C9Da', |
| 17 | +}; |
| 18 | + |
| 19 | +const defaultOptions = { publicKey: keys.public }; |
| 20 | +const passportVerify = async (decodedToken: any, done: any) => { |
| 21 | + try { |
| 22 | + return done(null, decodedToken.payload); |
| 23 | + } catch (err) { |
| 24 | + return done(err, false); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const usePassport = ( |
| 29 | + options: any = defaultOptions, |
| 30 | + verifyCallback = passportVerify, |
| 31 | +) => { |
| 32 | + passport.use(new DynamicStrategy(options, verifyCallback)); |
| 33 | +}; |
| 34 | + |
| 35 | +describe('DynamicStrategy', () => { |
| 36 | + afterEach(() => { |
| 37 | + passport.unuse('dynamicStrategy'); |
| 38 | + }); |
| 39 | + |
| 40 | + const generateJWT = (alg = 'RS256') => { |
| 41 | + const header = { alg: alg }; |
| 42 | + const payload = user; |
| 43 | + const signedToken = jwtSign( |
| 44 | + { |
| 45 | + header: header, |
| 46 | + payload: payload, |
| 47 | + encoding: 'utf8', |
| 48 | + }, |
| 49 | + keys.private, |
| 50 | + { algorithm: 'RS256' }, |
| 51 | + ); |
| 52 | + |
| 53 | + return signedToken; |
| 54 | + }; |
| 55 | + |
| 56 | + it('throws an error if the strategy does not have a `publicKey`', async () => { |
| 57 | + expect(() => { |
| 58 | + usePassport({}); |
| 59 | + }).toThrowError( |
| 60 | + 'You must provide your Dynamic public key for verification.', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns 401 if no header is provided', async () => { |
| 65 | + usePassport(); |
| 66 | + |
| 67 | + const response = await (await testServer).app.get('/user'); |
| 68 | + |
| 69 | + expect(response.status).toEqual(401); |
| 70 | + expect(response.headers['www-authenticate']).toEqual('Missing JWT token'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns 401 if the Authorization header has no value', async () => { |
| 74 | + usePassport(); |
| 75 | + |
| 76 | + const response = await (await testServer).app |
| 77 | + .get('/user') |
| 78 | + .set('Authorization', ''); |
| 79 | + |
| 80 | + expect(response.status).toEqual(401); |
| 81 | + expect(response.headers['www-authenticate']).toEqual('Missing JWT token'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns 401 if the Authorization header does not match the Bearer format', async () => { |
| 85 | + usePassport(); |
| 86 | + |
| 87 | + const response = await (await testServer).app |
| 88 | + .get('/user') |
| 89 | + .set('Authorization', 'Bearer'); |
| 90 | + |
| 91 | + expect(response.status).toEqual(401); |
| 92 | + expect(response.headers['www-authenticate']).toEqual('Missing JWT token'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns 401 if the JWT is invalid', async () => { |
| 96 | + usePassport(); |
| 97 | + |
| 98 | + const signedToken = 'Token'; |
| 99 | + const response = await (await testServer).app |
| 100 | + .get('/user') |
| 101 | + .set('Authorization', `Bearer ${signedToken}`); |
| 102 | + |
| 103 | + expect(response.status).toEqual(401); |
| 104 | + expect(response.headers['www-authenticate']).toEqual('Invalid token'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns 500 if the verify callback throws an error', async () => { |
| 108 | + const errorVerify = (_payload: any, _done: any) => { |
| 109 | + throw new Error('Bad API'); |
| 110 | + }; |
| 111 | + |
| 112 | + usePassport(defaultOptions, errorVerify); |
| 113 | + |
| 114 | + const signedToken = generateJWT(); |
| 115 | + const response = await (await testServer).app |
| 116 | + .get('/user') |
| 117 | + .set('Authorization', `Bearer ${signedToken}`); |
| 118 | + |
| 119 | + expect(response.status).toEqual(500); |
| 120 | + expect(response.body.error).toEqual('Bad API'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns 500 if the the verify callback returns an error', async () => { |
| 124 | + const errorVerify = async (_payload: any, done: any) => { |
| 125 | + return done(new Error('Unexpected error'), false); |
| 126 | + }; |
| 127 | + |
| 128 | + usePassport(defaultOptions, errorVerify); |
| 129 | + |
| 130 | + const signedToken = generateJWT(); |
| 131 | + const response = await (await testServer).app |
| 132 | + .get('/user') |
| 133 | + .set('Authorization', `Bearer ${signedToken}`); |
| 134 | + |
| 135 | + expect(response.status).toEqual(500); |
| 136 | + expect(response.body.error).toEqual('Unexpected error'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns 401 if the verify callback does not return a user', async () => { |
| 140 | + const verifyNoUser = async (_payload: any, done: any) => { |
| 141 | + return done(null, false); |
| 142 | + }; |
| 143 | + |
| 144 | + usePassport(defaultOptions, verifyNoUser); |
| 145 | + |
| 146 | + const signedToken = generateJWT(); |
| 147 | + const response = await (await testServer).app |
| 148 | + .get('/user') |
| 149 | + .set('Authorization', `Bearer ${signedToken}`); |
| 150 | + |
| 151 | + expect(response.status).toEqual(401); |
| 152 | + expect(response.headers['www-authenticate']).toEqual('User not found'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns 200 if the user can be authenticated', async () => { |
| 156 | + usePassport(); |
| 157 | + |
| 158 | + const signedToken = generateJWT(); |
| 159 | + const response = await (await testServer).app |
| 160 | + .get('/user') |
| 161 | + .set('Authorization', `Bearer ${signedToken}`); |
| 162 | + |
| 163 | + expect(response.status).toEqual(200); |
| 164 | + expect(response.body).toEqual(user); |
| 165 | + }); |
| 166 | +}); |
0 commit comments