Commit b4712a01 authored by Simo Sorce's avatar Simo Sorce

Add a 'payload' property to both JWS and JWE

Use the common term 'payload' and make it a property to access the
token's payload message. Allow access only if th payload has been
successfully validated or decrypted, raise otherwise
Signed-off-by: default avatarSimo Sorce <simo@redhat.com>
parent 92466152
......@@ -688,3 +688,9 @@ class JWE(object):
if key:
self.decrypt(key)
@property
def payload(self):
if not self.plaintext:
raise InvalidJWEOperation("Plaintext not available")
return self.plaintext
......@@ -519,3 +519,11 @@ class JWS(object):
else:
raise InvalidJWSSignature("No available signature")
return json_encode(sig)
@property
def payload(self):
if 'payload' not in self.objects:
raise InvalidJWSOperation("Payload not available")
if not self.is_valid:
raise InvalidJWSOperation("Payload not verified")
return self.objects['payload']
......@@ -959,13 +959,13 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(rsa_key)
e = E.serialize()
E.deserialize(e, rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(JWE_compact_5_1_5, rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_1_5), rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_1_5), rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
def test_5_2_encryption(self):
plaintext = Payload_plaintext_5
......@@ -975,13 +975,13 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(rsa_key)
e = E.serialize()
E.deserialize(e, rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(JWE_compact_5_2_5, rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_2_5), rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_2_5), rsa_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
# 5.3 - PBES2-* not implemented yet
# def test_5_3_encryption(self):
......@@ -1000,11 +1000,11 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(JWE_compact_5_6_4, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_6_4), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
# 5.7 - AES-GCM key wrapping not implemented yet
# def test_5_7_encryption(self):
......@@ -1017,13 +1017,13 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(JWE_compact_5_8_5, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_8_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_8_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
def test_5_9_encryption(self):
plaintext = Payload_plaintext_5
......@@ -1033,13 +1033,13 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(JWE_compact_5_9_5, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_9_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_9_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
def test_5_10_encryption(self):
plaintext = Payload_plaintext_5
......@@ -1050,11 +1050,11 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_10_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_10_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
def test_5_11_encryption(self):
plaintext = Payload_plaintext_5
......@@ -1065,11 +1065,11 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_11_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_11_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
def test_5_12_encryption(self):
plaintext = Payload_plaintext_5
......@@ -1079,11 +1079,11 @@ class Cookbook08JWETests(unittest.TestCase):
E.add_recipient(aes_key)
e = E.serialize()
E.deserialize(e, aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_general_5_12_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
E.deserialize(json_encode(JWE_flattened_5_12_5), aes_key)
self.assertEqual(E.plaintext, plaintext)
self.assertEqual(E.payload, plaintext)
# 5.13 - AES-GCM key wrapping not implemented yet
# def test_5_13_encryption(self):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment