def make_assertion(cls, email, audience, issuer=None, exp=None, assertion_sig=None, certificate_sig=None, new_style=True): """Generate a new dummy assertion for the given email address. This method lets you generate VEP assertions using dummy private keys. Called with just an email and audience it will generate an assertion from browserid.org. By specifying the "exp", "assertion_sig" or "certificate_sig" arguments it is possible generate invalid assertions for testing purposes. """ if issuer is None: issuer = "browserid.org" if exp is None: exp = int((time.time() + 60) * 1000) # Get private key for the email address itself. email_pub, email_priv = cls._get_keypair(email) # Get private key for the hostname so we can sign it. iss_pub, iss_priv = cls._get_keypair(issuer) # Generate the assertion, signed with email's public key. assertion = { "exp": exp, "aud": audience, } assertion = jwt.generate(assertion, email_priv) if assertion_sig is not None: assertion = ".".join( assertion.split(".")[:-1] + [encode_bytes(assertion_sig)]) # Generate the certificate signing the email's public key # with the issuer's public key. certificate = { "iss": issuer, "exp": exp, "principal": { "email": email }, "public-key": email_pub, } certificate = jwt.generate(certificate, iss_priv) if certificate_sig is not None: certificate = ".".join( certificate.split(".")[:-1] + [encode_bytes(certificate_sig)]) # Combine them into a VEP bundled assertion. return bundle_certs_and_assertion([certificate], assertion, new_style)
def test_malformed_assertions(self): errors = (ValueError, TrustError) # This one doesn't actually contain an assertion assertion = encode_json_bytes({}) self.assertRaises(errors, self.verifier.verify, assertion) # This one has no certificates pub, priv = get_keypair("TEST") assertion = encode_json_bytes({"assertion": jwt.generate({"aud": "TEST"}, priv), "certificates": []}) self.assertRaises(errors, self.verifier.verify, assertion)
def make_assertion(email, audience, issuer=None, exp=None, assertion_sig=None, certificate_sig=None, new_style=True): """Generate a new dummy assertion for the given email address. This method lets you generate VEP assertions using dummy private keys. Called with just an email and audience it will generate an assertion from browserid.org. By specifying the "exp", "assertion_sig" or "certificate_sig" arguments it is possible generate invalid assertions for testing purposes. """ if issuer is None: issuer = "browserid.org" if exp is None: exp = int((time.time() + 60) * 1000) # Get private key for the email address itself. email_pub, email_priv = get_keypair(email) # Get private key for the hostname so we can sign it. iss_pub, iss_priv = get_keypair(issuer) # Generate the assertion, signed with email's public key. assertion = { "exp": exp, "aud": audience, } assertion = jwt.generate(assertion, email_priv) if assertion_sig is not None: assertion = ".".join(assertion.split(".")[:-1] + [encode_bytes(assertion_sig)]) # Generate the certificate signing the email's public key # with the issuer's public key. certificate = { "iss": issuer, "exp": exp, "principal": {"email": email}, "public-key": email_pub, } certificate = jwt.generate(certificate, iss_priv) if certificate_sig is not None: certificate = ".".join(certificate.split(".")[:-1] + [encode_bytes(certificate_sig)]) # Combine them into a VEP bundled assertion. return bundle_certs_and_assertion([certificate], assertion, new_style)
def test_malformed_assertions(self): errors = (ValueError, TrustError) # This one doesn't actually contain an assertion assertion = encode_json_bytes({}) self.assertRaises(errors, self.verifier.verify, assertion) # This one has no certificates pub, priv = DummyVerifier._get_keypair("TEST") assertion = encode_json_bytes({ "assertion": jwt.generate({"aud": "TEST"}, priv), "certificates": [] }) self.assertRaises(errors, self.verifier.verify, assertion)
def test_error_jwt_with_mismatched_algorithm(self): pub, priv = DummyVerifier._get_keypair("TEST") token = jwt.generate({}, priv) token = jwt.parse(token) pub["algorithm"] = "RS" self.assertFalse(token.check_signature(pub))