|
| 1 | +import { |
| 2 | + decryptEarResponse, |
| 3 | + generateEarKey, |
| 4 | +} from "../../src/crypto/BrowserCrypto.js"; |
| 5 | +import { base64Decode } from "../../src/encode/Base64Decode.js"; |
| 6 | +import { base64Encode, urlEncodeArr } from "../../src/encode/Base64Encode.js"; |
| 7 | +import { BrowserAuthError, BrowserAuthErrorCodes } from "../../src/index.js"; |
| 8 | +import { |
| 9 | + generateValidEarJWE, |
| 10 | + TEST_TOKEN_RESPONSE, |
| 11 | + validEarJWE, |
| 12 | + validEarJWK, |
| 13 | +} from "../utils/StringConstants.js"; |
| 14 | + |
| 15 | +describe("BrowserCrypto Tests", () => { |
| 16 | + describe("generateEarKey", () => { |
| 17 | + it("Returns Base64 encoded string of ear_jwk", async () => { |
| 18 | + const key = await window.crypto.subtle.generateKey( |
| 19 | + { name: "AES-GCM", length: 256 }, |
| 20 | + true, |
| 21 | + ["encrypt", "decrypt"] |
| 22 | + ); |
| 23 | + const rawKey = await window.crypto.subtle.exportKey("raw", key); |
| 24 | + const keyStr = urlEncodeArr(new Uint8Array(rawKey)); |
| 25 | + |
| 26 | + const genKeySpy = jest |
| 27 | + .spyOn(window.crypto.subtle, "generateKey") |
| 28 | + .mockResolvedValue(key); |
| 29 | + const exportKeySpy = jest |
| 30 | + .spyOn(window.crypto.subtle, "exportKey") |
| 31 | + .mockResolvedValue(rawKey); |
| 32 | + const encodedJwk = await generateEarKey(); |
| 33 | + expect(genKeySpy).toHaveBeenCalledWith( |
| 34 | + { name: "AES-GCM", length: 256 }, |
| 35 | + true, |
| 36 | + ["encrypt", "decrypt"] |
| 37 | + ); |
| 38 | + expect(exportKeySpy).toHaveBeenCalledWith("raw", key); |
| 39 | + |
| 40 | + const decodedJwk = base64Decode(encodedJwk); |
| 41 | + const jwk = JSON.parse(decodedJwk); |
| 42 | + |
| 43 | + expect(jwk.alg).toEqual("dir"); |
| 44 | + expect(jwk.kty).toEqual("oct"); |
| 45 | + expect(jwk.k).toEqual(keyStr); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("decryptEarResponse", () => { |
| 50 | + it("Throws if ear_jwe has fewer than 5 parts", (done) => { |
| 51 | + decryptEarResponse(validEarJWK, "header.iv.ciphertext.tag").catch( |
| 52 | + (e) => { |
| 53 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 54 | + expect(e.errorCode).toBe( |
| 55 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 56 | + ); |
| 57 | + expect(e.subError).toBe("jwe_length"); |
| 58 | + done(); |
| 59 | + } |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Throws if ear_jwe has more than 5 parts", (done) => { |
| 64 | + decryptEarResponse( |
| 65 | + validEarJWK, |
| 66 | + "header..iv..ciphertext..tag" |
| 67 | + ).catch((e) => { |
| 68 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 69 | + expect(e.errorCode).toBe( |
| 70 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 71 | + ); |
| 72 | + expect(e.subError).toBe("jwe_length"); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("Throws if earJwk does not have a 'k' property", (done) => { |
| 78 | + const encodedJwk = base64Encode( |
| 79 | + JSON.stringify({ alg: "dir", kty: "oct", key: "testKey" }) |
| 80 | + ); |
| 81 | + decryptEarResponse(encodedJwk, validEarJWE).catch((e) => { |
| 82 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 83 | + expect(e.errorCode).toBe( |
| 84 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 85 | + ); |
| 86 | + expect(e.subError).toBe("import_key"); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("Throws if earJwk cannot be B64 decoded", (done) => { |
| 92 | + decryptEarResponse( |
| 93 | + JSON.stringify({ alg: "dir", kty: "oct", k: "testKey" }), |
| 94 | + validEarJWE |
| 95 | + ).catch((e) => { |
| 96 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 97 | + expect(e.errorCode).toBe( |
| 98 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 99 | + ); |
| 100 | + expect(e.subError).toBe("import_key"); |
| 101 | + done(); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("Throws if earJwk is not a JSON object", async () => { |
| 106 | + decryptEarResponse("notJSON", validEarJWE).catch((e) => { |
| 107 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 108 | + expect(e.errorCode).toBe( |
| 109 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 110 | + ); |
| 111 | + expect(e.subError).toBe("import_key"); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("Throws if earJwk 'k' property is not a raw encryption key", (done) => { |
| 116 | + decryptEarResponse( |
| 117 | + base64Encode( |
| 118 | + JSON.stringify({ alg: "dir", kty: "oct", k: "testKey" }) |
| 119 | + ), |
| 120 | + validEarJWE |
| 121 | + ).catch((e) => { |
| 122 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 123 | + expect(e.errorCode).toBe( |
| 124 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 125 | + ); |
| 126 | + expect(e.subError).toBe("import_key"); |
| 127 | + done(); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it("Throws if ear_jwe cannot be decrypted with the provided key", (done) => { |
| 132 | + generateEarKey().then((jwk: string) => { |
| 133 | + decryptEarResponse(jwk, validEarJWE).catch((e) => { |
| 134 | + expect(e).toBeInstanceOf(BrowserAuthError); |
| 135 | + expect(e.errorCode).toBe( |
| 136 | + BrowserAuthErrorCodes.failedToDecryptEarResponse |
| 137 | + ); |
| 138 | + expect(e.subError).toBe("decrypt"); |
| 139 | + done(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("Successfully decrypts ear_jwe with given earJwk", async () => { |
| 145 | + const jwe = await generateValidEarJWE( |
| 146 | + JSON.stringify(TEST_TOKEN_RESPONSE.body), |
| 147 | + validEarJWK |
| 148 | + ); |
| 149 | + const decryptedString = await decryptEarResponse(validEarJWK, jwe); |
| 150 | + expect(JSON.parse(decryptedString)).toEqual( |
| 151 | + TEST_TOKEN_RESPONSE.body |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments