Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #3 from wehlutyk/master

Add possibility to sign and verify JSON strings directly
  • Loading branch information...
commit 9d4c9eda57654bf9816d7c205e0ecf7ceacd0c12 2 parents 0b103be + 6993c28
Brian J Brennan authored February 05, 2013

Showing 1 changed file with 16 additions and 13 deletions. Show diff stats Hide diff stats

  1. 29  jws/__init__.py
29  jws/__init__.py
... ...
@@ -1,6 +1,8 @@
  1
+import json
  2
+
1 3
 import utils
2 4
 
3  
-# local 
  5
+# local
4 6
 import algos
5 7
 import header
6 8
 from exceptions import *
@@ -8,11 +10,11 @@
8 10
 ##############
9 11
 # public api #
10 12
 ##############
11  
-def sign(head, payload, key=None):
  13
+def sign(head, payload, key=None, is_json=False):
12 14
     data = {
13 15
         'key': key,
14  
-        'header': head,
15  
-        'payload': payload,
  16
+        'header': json.loads(head) if is_json else head,
  17
+        'payload': json.loads(payload) if is_json else payload,
16 18
         'signer': None
17 19
     }
18 20
     # TODO: re-evaluate whether to pass ``data`` by reference, or to copy and reassign
@@ -22,15 +24,15 @@ def sign(head, payload, key=None):
22 24
     if not data['signer']:
23 25
         raise MissingSigner("Header was processed, but no algorithm was found to sign the message")
24 26
     signer = data['signer']
25  
-    signature = signer(_signing_input(head, payload), key)
  27
+    signature = signer(_signing_input(head, payload, is_json), key)
26 28
     return utils.to_base64(signature)
27  
-    
28 29
 
29  
-def verify(head, payload, encoded_signature, key=None):
  30
+
  31
+def verify(head, payload, encoded_signature, key=None, is_json=False):
30 32
     data = {
31 33
         'key': key,
32  
-        'header': head,
33  
-        'payload': payload,
  34
+        'header': json.loads(head) if is_json else head,
  35
+        'payload': json.loads(payload) if is_json else payload,
34 36
         'verifier': None
35 37
     }
36 38
     # 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):
41 43
         raise MissingVerifier("Header was processed, but no algorithm was found to sign the message")
42 44
     verifier = data['verifier']
43 45
     signature = utils.from_base64(encoded_signature)
44  
-    return verifier(_signing_input(head, payload), signature, key)
45  
-    
  46
+    return verifier(_signing_input(head, payload, is_json), signature, key)
  47
+
46 48
 ####################
47 49
 # semi-private api #
48 50
 ####################
49  
-def _signing_input(head, payload):
50  
-    head_input, payload_input = map(utils.encode, [head, payload])
  51
+def _signing_input(head, payload, is_json=False):
  52
+    enc = utils.to_base64 if is_json else utils.encode
  53
+    head_input, payload_input = map(enc, [head, payload])
51 54
     return "%s.%s" % (head_input, payload_input)

0 notes on commit 9d4c9ed

Please sign in to comment.
Something went wrong with that request. Please try again.