def generate(payload, key): """Generate and sign a JWT for a dict payload.""" alg = key.__class__.__name__[:-3] algorithm = encode_json_bytes({"alg": alg}) payload = encode_json_bytes(payload) signature = encode_bytes(key.sign(".".join((algorithm, payload)))) return ".".join((algorithm, payload, signature))
def test_error_jwt_with_no_algorithm(self): token = ".".join(( encode_json_bytes({}), encode_json_bytes({}), encode_bytes("signature"), )) self.assertRaises(ValueError, jwt.parse, token)
def generate(cls, payload, key): """Generate and sign a JWT for a dict payload.""" alg = key.__class__.__name__[:-3] algorithm = encode_json_bytes({"alg": alg}) payload = encode_json_bytes(payload) signature = encode_bytes(key.sign(".".join((algorithm, payload)))) return ".".join((algorithm, payload, signature))
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 test_malformed_assertions(self): # This one doesn't actually contain an assertion assertion = encode_json_bytes({}) self.assertRaises(ValueError, 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(ValueError, self.verifier.verify, assertion)
def test_get_assertion_info(self): assertion = """ eyJjZXJ0aWZpY2F0ZXMiOlsiZXlKaGJHY2lPaUpTVXpFeU9DSjkuZXlKcGMzTWlPaUppY 205M2MyVnlhV1F1YjNKbklpd2laWGh3SWpveE16SXhPVFF4T1Rnek1EVXdMQ0p3ZFdKc2 FXTXRhMlY1SWpwN0ltRnNaMjl5YVhSb2JTSTZJbEpUSWl3aWJpSTZJamd4TmpreE5UQTB OVGswTkRVek5EVTFPREF4TlRreU5Ea3hNemsyTkRFNE56RTJNVFUwTkRNNE5EWXdPREl6 TXpBMU1USXlPRGN3TURRNE56TTFNREk1TURrek16a3lNRFkzTURFMU1qQTBORGd6TWpVM U56WXdOREE1TnpFeU9EYzNNVGswT1RVek1UQXdNVFEyTkRVek56TTJOakU0TlRVek5EY3 hNakkxT0RreU16TTFPRFV4TWpZNU1EQXdOREF5TVRrMk9ERTBNRGtpTENKbElqb2lOalU xTXpjaWZTd2ljSEpwYm1OcGNHRnNJanA3SW1WdFlXbHNJam9pY25saGJrQnlabXN1YVdR dVlYVWlmWDAua19oaEtYMFRCVnUyX2szbV9uRDVOVWJfTktwX19PLTY1MW1CRUl3S1NZZ GlOenQwQm9WRkNEVEVueEhQTWJCVjJaejk0WDgtLVRjVXJidEV0MWV1S1dWdjMtNTFUOU xBZnV6SEhfekNCUXJVbmxkMVpXSmpBM185ZEhQeTMwZzRMSU9YZTJWWmd0T1Nva3MyZFE 4ZDNvazlSUTJQME5ERzB1MDBnN3lGejE4Il0sImFzc2VydGlvbiI6ImV5SmhiR2NpT2lK U1V6WTBJbjAuZXlKbGVIQWlPakV6TWpFNU1qazBOelU0TWprc0ltRjFaQ0k2SW1oMGRIQ TZMeTl0ZVdaaGRtOXlhWFJsWW1WbGNpNXZjbWNpZlEuQWhnS2Q0eXM0S3FnSGJYcUNSS3 hHdlluVmFJOUwtb2hYSHk0SVBVWDltXzI0TWdfYlU2aGRIMTNTNnFnQy1vSHBpS3BfTGl 6cDRGRjlUclBjNjBTRXcifQ """.replace(" ", "").replace("\n", "").strip() data = get_assertion_info(assertion) self.assertEquals(data["principal"]["email"], "*****@*****.**") self.assertEquals(data["audience"], "http://myfavoritebeer.org") self.assertRaises(ValueError, get_assertion_info, "JUNK") self.assertRaises(ValueError, get_assertion_info, "X") self.assertRaises(ValueError, get_assertion_info, "\x00\x01\x02") bad_assertion = encode_json_bytes({"fake": "assertion"}) self.assertRaises(ValueError, get_assertion_info, bad_assertion)
def test_encode_decode_json_bytes(self): obj = {"hello": "world"} self.assertEquals(obj, decode_json_bytes(encode_json_bytes(obj))) self.assertRaises(ValueError, decode_json_bytes, encode_bytes("NOJSON4U")) self.assertRaises(ValueError, decode_json_bytes, encode_bytes("42")) self.assertRaises(ValueError, decode_json_bytes, encode_bytes("[1, 2, 3]")) self.assertRaises(ValueError, encode_json_bytes, 42) self.assertRaises(ValueError, encode_json_bytes, [1, 3, 3])