forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
64 lines (51 loc) · 1.86 KB
/
auth.py
File metadata and controls
64 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
SoftLayer.auth
~~~~~~~~~~~~~~
Module with the supported auth mechanisms for the SoftLayer API
:license: MIT, see LICENSE for more details.
"""
__all__ = ['BasicAuthentication', 'TokenAuthentication', 'AuthenticationBase']
class AuthenticationBase(object):
""" A base authentication class intended to be overridden """
def get_headers(self):
""" Return a dictionary of XML-RPC headers to be inserted for
authentication """
raise NotImplementedError
class TokenAuthentication(AuthenticationBase):
""" Token-based authentication class.
:param user_id int: a user's id
:param auth_token str: a user's auth token, attained through
User_Customer::getPortalLoginToken
"""
def __init__(self, user_id, auth_token):
self.user_id = user_id
self.auth_token = auth_token
def get_headers(self):
""" Returns token-based auth headers """
return {
'authenticate': {
'complexType': 'PortalLoginToken',
'userId': self.user_id,
'authToken': self.auth_token,
}
}
def __repr__(self):
return "<TokenAuthentication: %s %s>" % (self.user_id, self.auth_token)
class BasicAuthentication(AuthenticationBase):
""" Token-based authentication class.
:param username str: a user's username
:param api_key str: a user's API key
"""
def __init__(self, username, api_key):
self.username = username
self.api_key = api_key
def get_headers(self):
""" Returns token-based auth headers """
return {
'authenticate': {
'username': self.username,
'apiKey': self.api_key,
}
}
def __repr__(self):
return "<BasicAuthentication: %s>" % (self.username)