def verify(self, assertion, audience=None, now=None): """Verify the given VEP assertion. This method parses a VEP identity assertion, verifies the bundled chain of certificates and signatures, and returns the extracted email address and audience. If the 'audience' argument is given, it first verifies that the audience of the assertion matches the one given. This can help avoid doing lots of crypto for assertions that can't be valid. If you don't specify an audience, you *MUST* validate the audience value returned by this method. If the 'now' argument is given, it is used as the current time in milliseconds. This lets you verify expired assertions, e.g. for testing purposes. """ if now is None: now = int(time.time() * 1000) # This catches KeyError and turns it into ValueError. # It saves having to test for the existence of individual # items in the various assertion payloads. try: certificates, assertion = unbundle_certs_and_assertion(assertion) # Check that the assertion is usable and valid. # No point doing all that crypto if we're going to fail out anyway. assertion = jwt.parse(assertion, self.parser_cls) if audience is not None: if assertion.payload["aud"] != audience: raise AudienceMismatchError(assertion.payload["aud"]) if assertion.payload["exp"] < now: raise ExpiredSignatureError(assertion.payload["exp"]) # Parse out the list of certificates. certificates = [jwt.parse(c, self.parser_cls) for c in certificates] # Check that the root issuer is trusted. # No point doing all that crypto if we're going to fail out anyway. email = certificates[-1].payload["principal"]["email"] root_issuer = certificates[0].payload["iss"] if root_issuer not in self.trusted_secondaries: if not email.endswith("@" + root_issuer): msg = "untrusted root issuer: %s" % (root_issuer,) raise InvalidSignatureError(msg) # Verify the entire chain of certificates. cert = self.verify_certificate_chain(certificates, now=now) # Check the signature on the assertion. if not assertion.check_signature(cert.payload["public-key"]): raise InvalidSignatureError("invalid signature on assertion") except KeyError: raise ValueError("Malformed JWT") # Looks good! return { "status": "okay", "audience": assertion.payload["aud"], "email": email, "issuer": root_issuer, }
def verify(self, assertion, audience=None, now=None): """Verify the given VEP assertion. This method parses a VEP identity assertion, verifies the bundled chain of certificates and signatures, and returns the extracted email address and audience. If the 'audience' argument is given, it first verifies that the audience of the assertion matches the one given. This can help avoid doing lots of crypto for assertions that can't be valid. If you don't specify an audience, you *MUST* validate the audience value returned by this method. If the 'now' argument is given, it is used as the current time in milliseconds. This lets you verify expired assertions, e.g. for testing purposes. """ if now is None: now = int(time.time() * 1000) # This catches KeyError and turns it into ValueError. # It saves having to test for the existence of individual # items in the various assertion payloads. try: certificates, assertion = unbundle_certs_and_assertion(assertion) # Check that the assertion is usable and valid. # No point doing all that crypto if we're going to fail out anyway. assertion = jwt.parse(assertion) if audience is not None: if assertion.payload["aud"] != audience: raise AudienceMismatchError(assertion.payload["aud"]) if assertion.payload["exp"] < now: raise ExpiredSignatureError(assertion.payload["exp"]) # Parse out the list of certificates. certificates = [jwt.parse(c) for c in certificates] # Check that the root issuer is trusted. # No point doing all that crypto if we're going to fail out anyway. email = certificates[-1].payload["principal"]["email"] root_issuer = certificates[0].payload["iss"] if root_issuer not in self.trusted_secondaries: if not email.endswith("@" + root_issuer): msg = "untrusted root issuer: %s" % (root_issuer, ) raise InvalidSignatureError(msg) # Verify the entire chain of certificates. cert = self.verify_certificate_chain(certificates, now=now) # Check the signature on the assertion. if not assertion.check_signature(cert.payload["public-key"]): raise InvalidSignatureError("invalid signature on assertion") except KeyError: raise ValueError("Malformed JWT") # Looks good! return { "status": "okay", "audience": assertion.payload["aud"], "email": email, "issuer": root_issuer, }
def test_error_handling_in_verify_certificate_chain(self): self.assertRaises(ValueError, self.verifier.verify_certificate_chain, []) certs = decode_json_bytes(EXPIRED_ASSERTION)["certificates"] certs = [jwt.parse(cert) for cert in certs] self.assertRaises(ExpiredSignatureError, self.verifier.verify_certificate_chain, certs)
def parse_jwt(self, data): return jwt.parse(data, self.parser_cls)
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))