def test_unsupported_enc(self): with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({'alg': 'dir', 'enc': 'RSA'}) with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({'alg': 'dir', 'enc': 'A126'}) with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({'alg': 'dir', 'enc': 'Base64'})
def test_unsupported_enc(self): with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({"alg": "dir", "enc": "RSA"}) with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({"alg": "dir", "enc": "A126"}) with pytest.raises(jwe.exceptions.UnsupportedEncryption): validation.validate_header({"alg": "dir", "enc": "Base64"})
def test_unsupported_alg(self): with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({'alg': 'foo', 'enc': 'A256GCM'}) with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({'alg': 'bar', 'enc': 'A256GCM'}) with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({'alg': 'baz', 'enc': 'A256GCM'})
def test_unsupported_alg(self): with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({"alg": "foo", "enc": "A256GCM"}) with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({"alg": "bar", "enc": "A256GCM"}) with pytest.raises(jwe.exceptions.UnsupportedAlgorithm): validation.validate_header({"alg": "baz", "enc": "A256GCM"})
def decrypt(data, key): spl = data.split(b".") try: header, encrypted_key, iv, ciphertext, tag = [base64.b64decode(x) for x in spl] except ValueError: raise exceptions.MalformedData( "Recieved incorrected formatted data. Expected 5 segments, received {}".format(len(spl)) ) if encrypted_key: raise exceptions.UnsupportedOption("Key wrapping is currently not supported") try: validate_header(json.loads(header.decode("utf-8"))) except ValueError: raise exceptions.MalformedData("Header is not valid JSON") encryptor = Cipher(algorithms.AES(key), modes.GCM(iv, tag), backend=backend).decryptor() encryptor.authenticate_additional_data(spl[0]) return encryptor.update(ciphertext) + encryptor.finalize()
def test_correct_header(self): try: validation.validate_header({'alg': 'dir', 'enc': 'A256GCM'}) except Exception as e: pytest.fail(e)
def test_missing_enc(self): with pytest.raises(jwe.exceptions.MalformedHeader): validation.validate_header({'enc': 'A256GCM'})
def test_missing_alg(self): with pytest.raises(jwe.exceptions.MalformedHeader): validation.validate_header({'alg': 'dir'})
def test_missing_keys(self): with pytest.raises(jwe.exceptions.MalformedHeader): validation.validate_header({})
def test_correct_header(self): try: validation.validate_header({"alg": "dir", "enc": "A256GCM"}) except Exception as e: pytest.fail(e)
def test_missing_enc(self): with pytest.raises(jwe.exceptions.MalformedHeader): validation.validate_header({"enc": "A256GCM"})
def test_missing_alg(self): with pytest.raises(jwe.exceptions.MalformedHeader): validation.validate_header({"alg": "dir"})