Reputation: 95
I've been reading through some example code for authentication using tokens, and there is a line that I do not understand at all.
client.set_signature_method(SignatureMethod_RSA_SHA1())
Specifically, I'm unsure what "SignatureMethod_RSA_SHA1()" does. Here's the SignatureMethod class, for reference:
class SignatureMethod_RSA_SHA1(oauth.SignatureMethod):
name = 'RSA-SHA1'
def signing_base(self, request, consumer, token):
if not hasattr(request, 'normalized_url') or request.normalized_url is None:
raise ValueError("Base URL for request is not set.")
sig = ( #Creates the request?
oauth.escape(request.method),
oauth.escape(request.normalized_url),
oauth.escape(request.get_normalized_parameters()),
)
key = '%s&' % oauth.escape(consumer.secret)#
if token:
key += oauth.escape(token.secret)
raw = '&'.join(sig)
return key, raw
def sign(self, request, consumer, token):
"""Builds the base signature string."""
key, raw = self.signing_base(request, consumer, token)
with open('../rsa.pem', 'r') as f:
data = f.read()
privateKeyString = data.strip()
privatekey = keyfactory.parsePrivateKey(privateKeyString)
signature = privatekey.hashAndSign(raw)
return base64.b64encode(signature)
The syntax suggests that the class is being called as a function, which I have never seen in my short time programming. Does anybody else what this means?
Upvotes: 0
Views: 130
Reputation: 11039
That is how you instantiate a class, it's not being used as a function (although technically it is calling the classes __init__()
method).
client.set_signature_method()
apparently takes an instance of of some sort of signature method class (not sure if SignatureMethod_RSA_SHA1()
is the only possible type) and most likely hangs it on the client
object making it accessible through client.signature_method
or some other property.
This is the same as doing:
sig_method = SignatureMethod_RSA_SHA1()
client.set_signature_method(sig_method)
It's just a shorter way of doing it if you won't need to access sig_method
again outside of the client
instance.
More familiar examples would be:
a_list = list()
a_dict = dict()
This way the SignatureMethod_RSA_SHA1()
and it's properties and methods are now available to the client
instance. For simpler example we can do:
>>> class MyClass:
def __init__(self, my_list):
self.my_list = my_list
def add_to_list(self, a_value):
self.my_list.append(a_value)
>>> foo = MyClass(list())
>>> foo.my_list
[]
So now the list is available to the class and so are it's methods:
>>> foo.my_list.extend([2,4,1,4,8,0,3,])
>>> foo.my_list
[2, 4, 1, 4, 8, 0, 3]
And more importantly the class can use the list objects methods internally like in our add_to_list()
method (which is probably how SignatureMethod_RSA_SHA1()
is being used):
>>> foo.add_to_list('a')
>>> foo.my_list
[2, 4, 1, 4, 8, 0, 3, 'a']
Upvotes: 2
Reputation: 64
SignatureMethod_RSA_SHA1()
is just class constructor. Its __init__
function is called.
In the other words - set_signature_method
is called with SignatureMethod_RSA_SHA1
instance.
Upvotes: 0