Permalink
Browse files

Signatures and verifications can be done from json strings also, not

only from python jsonables
  • Loading branch information...
1 parent 0b103be commit 6993c289de2a3170db5355bed7b0c00023ed6c54 @wehlutyk wehlutyk committed Dec 21, 2012
Showing with 16 additions and 13 deletions.
  1. +16 −13 jws/__init__.py
View
@@ -1,18 +1,20 @@
+import json
+
import utils
-# local
+# local
import algos
import header
from exceptions import *
##############
# public api #
##############
-def sign(head, payload, key=None):
+def sign(head, payload, key=None, is_json=False):
data = {
'key': key,
- 'header': head,
- 'payload': payload,
+ 'header': json.loads(head) if is_json else head,
+ 'payload': json.loads(payload) if is_json else payload,
'signer': None
}
# TODO: re-evaluate whether to pass ``data`` by reference, or to copy and reassign
@@ -22,15 +24,15 @@ def sign(head, payload, key=None):
if not data['signer']:
raise MissingSigner("Header was processed, but no algorithm was found to sign the message")
signer = data['signer']
- signature = signer(_signing_input(head, payload), key)
+ signature = signer(_signing_input(head, payload, is_json), key)
return utils.to_base64(signature)
-
-def verify(head, payload, encoded_signature, key=None):
+
+def verify(head, payload, encoded_signature, key=None, is_json=False):
data = {
'key': key,
- 'header': head,
- 'payload': payload,
+ 'header': json.loads(head) if is_json else head,
+ 'payload': json.loads(payload) if is_json else payload,
'verifier': None
}
# TODO: re-evaluate whether to pass ``data`` by reference, or to copy and reassign
@@ -41,11 +43,12 @@ def verify(head, payload, encoded_signature, key=None):
raise MissingVerifier("Header was processed, but no algorithm was found to sign the message")
verifier = data['verifier']
signature = utils.from_base64(encoded_signature)
- return verifier(_signing_input(head, payload), signature, key)
-
+ return verifier(_signing_input(head, payload, is_json), signature, key)
+
####################
# semi-private api #
####################
-def _signing_input(head, payload):
- head_input, payload_input = map(utils.encode, [head, payload])
+def _signing_input(head, payload, is_json=False):
+ enc = utils.to_base64 if is_json else utils.encode
+ head_input, payload_input = map(enc, [head, payload])
return "%s.%s" % (head_input, payload_input)

0 comments on commit 6993c28

Please sign in to comment.