Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urlparse
- import sys
- import base64
- import hashlib
- # Third party
- import oauth2
- from M2Crypto import RSA
- """
- A signature method class for RSA signing, for use with oauth2.
- Requires M2Crypto
- """
- class SignatureMethod_RSA(oauth2.SignatureMethod):
- """ RSA signature not implemented by oauth2."""
- name = "RSA-SHA1"
- def __init__(self, key_path):
- super(oauth2.SignatureMethod, self).__init__()
- self.key_path = key_path
- self.RSA = RSA.load_key(key_path)
- def signing_base(self, request):
- """Calculates the string that needs to be signed."""
- sig = (
- oauth2.escape(request.method),
- oauth2.escape(request.normalized_url),
- oauth2.escape(request.get_normalized_parameters()),
- )
- raw = '&'.join(sig)
- print raw
- return raw
- def sign(self, request, consumer, token):
- """Returns the signature for the given request.
- Note: consumer and token are not used, but are there to fit in with
- call in oauth2 module.
- """
- raw = self.signing_base(request)
- digest = hashlib.sha1(raw).digest()
- signature = self.RSA.sign(digest, algo="sha1")
- print signature
- encoded = base64.b64encode(signature)
- return encoded
- class XeroPrivateClient(oauth2.Client):
- def __init__(self, consumer_key, consumer_secret, rsa_key_path):
- consumer = oauth2.Consumer(consumer_key, consumer_secret)
- # For private applications, the consumer key and secret are used as the
- # access token and access secret.
- token = oauth2.Token(consumer_key, consumer_secret)
- oauth2.Client.__init__(self, consumer, token)
- self.set_signature_method(SignatureMethod_RSA(rsa_key_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement