|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { AuthCodeChallenge, AuthCodePkceChallenge, IdTokenChallenge, OtpChallenge } from '../src/challenge.js' |
| 3 | +import { IdentityType } from '../src/index.js' |
| 4 | + |
| 5 | +describe('IdTokenChallenge', () => { |
| 6 | + const idToken = |
| 7 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaXNzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbSIsImF1ZCI6ImF1ZGllbmNlIiwiaWF0IjoxNzE2MjM5MDIyLCJleHAiOjE4MTYyMzkwMjJ9.vo-hzFNUd8uzKmMVEj04eIiqeXfOQahZu9ZWGnJPE74' |
| 8 | + |
| 9 | + it('returns correct commit params', () => { |
| 10 | + const challenge = new IdTokenChallenge('https://example.com', 'audience', idToken) |
| 11 | + const params = challenge.getCommitParams() |
| 12 | + expect(params).toBeDefined() |
| 13 | + expect(params.authMode).toBe('IDToken') |
| 14 | + expect(params.identityType).toBe('OIDC') |
| 15 | + expect(params.handle).toBe('0x800fa2a1ca87f4a37d7f0a2e1858d36cd622cc2970d886e7e8a00f82edca3455') |
| 16 | + expect(params.metadata).toBeDefined() |
| 17 | + expect(params.metadata.iss).toBe('https://example.com') |
| 18 | + expect(params.metadata.aud).toBe('audience') |
| 19 | + expect(params.metadata.exp).toBe('1816239022') |
| 20 | + }) |
| 21 | + |
| 22 | + it('returns correct complete params', () => { |
| 23 | + const challenge = new IdTokenChallenge('https://example.com', 'audience', idToken) |
| 24 | + const params = challenge.getCompleteParams() |
| 25 | + expect(params).toBeDefined() |
| 26 | + expect(params.authMode).toBe('IDToken') |
| 27 | + expect(params.identityType).toBe('OIDC') |
| 28 | + expect(params.verifier).toBe('0x800fa2a1ca87f4a37d7f0a2e1858d36cd622cc2970d886e7e8a00f82edca3455') |
| 29 | + expect(params.answer).toBe(idToken) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('AuthCodeChallenge', () => { |
| 34 | + const authCode = '1234567890' |
| 35 | + const signer = '0x26F5B2b3Feed8f02051c0b1c5b40cc088107935e' |
| 36 | + |
| 37 | + it('returns correct commit params', () => { |
| 38 | + const challenge = new AuthCodeChallenge('https://example.com', 'audience', 'https://dapp.com/redirect', authCode) |
| 39 | + const params = challenge.getCommitParams() |
| 40 | + expect(params).toBeDefined() |
| 41 | + expect(params.authMode).toBe('AuthCode') |
| 42 | + expect(params.identityType).toBe('OIDC') |
| 43 | + expect(params.handle).toBe('0x38301fb0b5fcf3aaa4b97c4771bb6c75546e313b4ce7057c51a8cc6a3ace9d7e') |
| 44 | + expect(params.signer).toBeUndefined() |
| 45 | + expect(params.metadata).toBeDefined() |
| 46 | + expect(params.metadata.iss).toBe('https://example.com') |
| 47 | + expect(params.metadata.aud).toBe('audience') |
| 48 | + expect(params.metadata.redirect_uri).toBe('https://dapp.com/redirect') |
| 49 | + }) |
| 50 | + |
| 51 | + it('returns correct commit params with signer', () => { |
| 52 | + const challenge = new AuthCodeChallenge('https://example.com', 'audience', 'https://dapp.com/redirect', authCode) |
| 53 | + const params = challenge.withSigner(signer).getCommitParams() |
| 54 | + expect(params).toBeDefined() |
| 55 | + expect(params.authMode).toBe('AuthCode') |
| 56 | + expect(params.identityType).toBe('OIDC') |
| 57 | + expect(params.signer).toBe(signer) |
| 58 | + expect(params.handle).toBe('0x38301fb0b5fcf3aaa4b97c4771bb6c75546e313b4ce7057c51a8cc6a3ace9d7e') |
| 59 | + expect(params.metadata).toBeDefined() |
| 60 | + expect(params.metadata.iss).toBe('https://example.com') |
| 61 | + expect(params.metadata.aud).toBe('audience') |
| 62 | + expect(params.metadata.redirect_uri).toBe('https://dapp.com/redirect') |
| 63 | + }) |
| 64 | + |
| 65 | + it('returns correct complete params', () => { |
| 66 | + const challenge = new AuthCodeChallenge('https://example.com', 'audience', 'https://dapp.com/redirect', authCode) |
| 67 | + const params = challenge.getCompleteParams() |
| 68 | + expect(params).toBeDefined() |
| 69 | + expect(params.authMode).toBe('AuthCode') |
| 70 | + expect(params.identityType).toBe('OIDC') |
| 71 | + expect(params.verifier).toBe('0x38301fb0b5fcf3aaa4b97c4771bb6c75546e313b4ce7057c51a8cc6a3ace9d7e') |
| 72 | + expect(params.answer).toBe(authCode) |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +describe('AuthCodePkceChallenge', () => { |
| 77 | + const challenge = new AuthCodePkceChallenge('https://example.com', 'audience', 'https://dapp.com/redirect') |
| 78 | + const authCode = '1234567890' |
| 79 | + const verifier = 'verifier' |
| 80 | + const signer = '0x26F5B2b3Feed8f02051c0b1c5b40cc088107935e' |
| 81 | + |
| 82 | + it('returns correct commit params', () => { |
| 83 | + const params = challenge.getCommitParams() |
| 84 | + expect(params).toBeDefined() |
| 85 | + expect(params.authMode).toBe('AuthCodePKCE') |
| 86 | + expect(params.identityType).toBe('OIDC') |
| 87 | + expect(params.handle).toBeUndefined() |
| 88 | + expect(params.metadata).toBeDefined() |
| 89 | + expect(params.metadata.iss).toBe('https://example.com') |
| 90 | + expect(params.metadata.aud).toBe('audience') |
| 91 | + expect(params.metadata.redirect_uri).toBe('https://dapp.com/redirect') |
| 92 | + }) |
| 93 | + |
| 94 | + it('returns correct commit params with signer', () => { |
| 95 | + const params = challenge.withSigner(signer).getCommitParams() |
| 96 | + expect(params).toBeDefined() |
| 97 | + expect(params.authMode).toBe('AuthCodePKCE') |
| 98 | + expect(params.identityType).toBe('OIDC') |
| 99 | + expect(params.signer).toBe(signer) |
| 100 | + expect(params.handle).toBeUndefined() |
| 101 | + expect(params.metadata).toBeDefined() |
| 102 | + expect(params.metadata.iss).toBe('https://example.com') |
| 103 | + expect(params.metadata.aud).toBe('audience') |
| 104 | + expect(params.metadata.redirect_uri).toBe('https://dapp.com/redirect') |
| 105 | + }) |
| 106 | + |
| 107 | + it('returns correct complete params with answer and verifier', () => { |
| 108 | + const params = challenge.withAnswer(verifier, authCode).getCompleteParams() |
| 109 | + expect(params).toBeDefined() |
| 110 | + expect(params.authMode).toBe('AuthCodePKCE') |
| 111 | + expect(params.identityType).toBe('OIDC') |
| 112 | + expect(params.verifier).toBe(verifier) |
| 113 | + expect(params.answer).toBe(authCode) |
| 114 | + }) |
| 115 | + |
| 116 | + it('throws if answer and verifier are not provided', () => { |
| 117 | + expect(() => challenge.getCompleteParams()).toThrow() |
| 118 | + }) |
| 119 | + |
| 120 | + it('throws if answer is not provided', () => { |
| 121 | + expect(() => challenge.withAnswer(verifier, '').getCompleteParams()).toThrow() |
| 122 | + }) |
| 123 | + |
| 124 | + it('throws if verifier is not provided', () => { |
| 125 | + expect(() => challenge.withAnswer('', authCode).getCompleteParams()).toThrow() |
| 126 | + }) |
| 127 | +}) |
| 128 | + |
| 129 | +describe('OtpChallenge', () => { |
| 130 | + const otp = '123456' |
| 131 | + const codeChallenge = 'codeChallenge' |
| 132 | + |
| 133 | + // finalAnswer = keccak256(codeChallenge + otp) |
| 134 | + const finalAnswer = '0xab1b443dd7ae1f1dd51f81f8d346565c1a63e7d090a1c220e44ed578183b08f5' |
| 135 | + |
| 136 | + describe('fromRecipient', () => { |
| 137 | + const recipient = 'test@example.com' |
| 138 | + |
| 139 | + describe('getCommitParams', () => { |
| 140 | + it('returns correct commit params', () => { |
| 141 | + const challenge = OtpChallenge.fromRecipient(IdentityType.Email, recipient) |
| 142 | + const params = challenge.getCommitParams() |
| 143 | + expect(params).toBeDefined() |
| 144 | + expect(params.authMode).toBe('OTP') |
| 145 | + expect(params.identityType).toBe('Email') |
| 146 | + expect(params.handle).toBe(recipient) |
| 147 | + expect(params.signer).toBeUndefined() |
| 148 | + }) |
| 149 | + |
| 150 | + it('throws if recipient is not provided', () => { |
| 151 | + const challenge = OtpChallenge.fromRecipient(IdentityType.Email, '') |
| 152 | + expect(() => challenge.getCommitParams()).toThrow() |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + describe('getCompleteParams', () => { |
| 157 | + it('returns correct complete params', () => { |
| 158 | + const challenge = OtpChallenge.fromRecipient(IdentityType.Email, recipient) |
| 159 | + const params = challenge.withAnswer(codeChallenge, otp).getCompleteParams() |
| 160 | + expect(params).toBeDefined() |
| 161 | + expect(params.authMode).toBe('OTP') |
| 162 | + expect(params.identityType).toBe('Email') |
| 163 | + expect(params.verifier).toBe(recipient) |
| 164 | + expect(params.answer).toBe(finalAnswer) |
| 165 | + }) |
| 166 | + |
| 167 | + it('throws if answer is not provided', () => { |
| 168 | + const challenge = OtpChallenge.fromRecipient(IdentityType.Email, recipient) |
| 169 | + expect(() => challenge.getCompleteParams()).toThrow() |
| 170 | + }) |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + describe('fromSigner', () => { |
| 175 | + const signer = '0x26F5B2b3Feed8f02051c0b1c5b40cc088107935e' |
| 176 | + |
| 177 | + describe('getCommitParams', () => { |
| 178 | + it('returns correct commit params', () => { |
| 179 | + const challenge = OtpChallenge.fromSigner(IdentityType.Email, signer) |
| 180 | + const params = challenge.getCommitParams() |
| 181 | + expect(params).toBeDefined() |
| 182 | + expect(params.authMode).toBe('OTP') |
| 183 | + expect(params.identityType).toBe('Email') |
| 184 | + expect(params.handle).toBeUndefined() |
| 185 | + expect(params.signer).toBe(signer) |
| 186 | + }) |
| 187 | + |
| 188 | + it('throws if signer is not provided', () => { |
| 189 | + const challenge = OtpChallenge.fromSigner(IdentityType.Email, '') |
| 190 | + expect(() => challenge.getCommitParams()).toThrow() |
| 191 | + }) |
| 192 | + }) |
| 193 | + }) |
| 194 | +}) |
0 commit comments