Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import urlparse
  2. import sys
  3. import base64
  4. import hashlib
  5. # Third party
  6. import oauth2
  7. from M2Crypto import RSA
  8.  
  9. """
  10. A signature method class for RSA signing, for use with oauth2.
  11. Requires M2Crypto
  12. """
  13.  
  14. class SignatureMethod_RSA(oauth2.SignatureMethod):
  15.     """ RSA signature not implemented by oauth2."""
  16.     name = "RSA-SHA1"
  17.  
  18.     def __init__(self, key_path):
  19.         super(oauth2.SignatureMethod, self).__init__()
  20.         self.key_path = key_path
  21.         self.RSA = RSA.load_key(key_path)
  22.  
  23.     def signing_base(self, request):
  24.         """Calculates the string that needs to be signed."""
  25.         sig = (
  26.             oauth2.escape(request.method),
  27.             oauth2.escape(request.normalized_url),
  28.             oauth2.escape(request.get_normalized_parameters()),
  29.         )
  30.         raw = '&'.join(sig)
  31.         print raw
  32.         return raw
  33.  
  34.     def sign(self, request, consumer, token):
  35.         """Returns the signature for the given request.
  36.        Note: consumer and token are not used, but are there to fit in with
  37.        call in oauth2 module.
  38.        """
  39.         raw = self.signing_base(request)
  40.         digest = hashlib.sha1(raw).digest()
  41.         signature = self.RSA.sign(digest, algo="sha1")
  42.         print signature
  43.         encoded = base64.b64encode(signature)
  44.         return encoded
  45.  
  46. class XeroPrivateClient(oauth2.Client):
  47.     def __init__(self, consumer_key, consumer_secret, rsa_key_path):
  48.         consumer = oauth2.Consumer(consumer_key, consumer_secret)
  49.         # For private applications, the consumer key and secret are used as the
  50.         # access token and access secret.
  51.         token = oauth2.Token(consumer_key, consumer_secret)
  52.         oauth2.Client.__init__(self, consumer, token)
  53.         self.set_signature_method(SignatureMethod_RSA(rsa_key_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement