21
21
class BasicApiAuthManager(object):
22
""" Implements a somewhat rudimentary version of Rackspace Auth"""
22
""" Implements a somewhat rudimentary version of OpenStack Auth"""
24
24
def __init__(self, host=None, db_driver=None):
29
db_driver = FLAGS.db_driver
29
db_driver = FLAGS.db_driver
30
30
self.db = utils.import_object(db_driver)
31
31
self.auth = auth.manager.AuthManager()
32
32
self.context = Context()
40
40
return faults.Fault(webob.exc.HTTPUnauthorized())
43
username, key = req.headers['X-Auth-User'], \
44
req.headers['X-Auth-Key']
43
username = req.headers['X-Auth-User']
44
key = req.headers['X-Auth-Key']
46
46
return faults.Fault(webob.exc.HTTPUnauthorized())
48
username, key = req.headers['X-Auth-User'], req.headers['X-Auth-Key']
49
48
token, user = self._authorize_user(username, key)
51
50
res = webob.Response()
52
res.headers['X-Auth-Token'] = token['token_hash']
51
res.headers['X-Auth-Token'] = token.token_hash
53
52
res.headers['X-Server-Management-Url'] = \
54
token['server_management_url']
55
res.headers['X-Storage-Url'] = token['storage_url']
56
res.headers['X-CDN-Management-Url'] = token['cdn_management_url']
53
token.server_management_url
54
res.headers['X-Storage-Url'] = token.storage_url
55
res.headers['X-CDN-Management-Url'] = token.cdn_management_url
57
56
res.content_type = 'text/plain'
66
65
If the token has expired, returns None
67
66
If the token is not found, returns None
68
Otherwise returns the token
67
Otherwise returns dict(id=(the authorized user's id))
70
69
This method will also remove the token if the timestamp is older than
73
72
token = self.db.auth_get_token(self.context, token_hash)
75
delta = datetime.datetime.now() - token['created_at']
74
delta = datetime.datetime.now() - token.created_at
76
75
if delta.days >= 2:
77
76
self.db.auth_destroy_token(self.context, token)
79
user = self.auth.get_user(token['user_id'])
80
return { 'id':user['uid'] }
78
#TODO(gundlach): Why not just return dict(id=token.user_id)?
79
user = self.auth.get_user(token.user_id)
80
return {'id': user.id}
83
83
def _authorize_user(self, username, key):
84
84
""" Generates a new token and assigns it to a user """
85
85
user = self.auth.get_user_from_access_key(key)
86
if user and user['name'] == username:
86
if user and user.name == username:
87
87
token_hash = hashlib.sha1('%s%s%f' % (username, key,
88
88
time.time())).hexdigest()
90
token['token_hash'] = token_hash
91
token['cdn_management_url'] = ''
92
token['server_management_url'] = self._get_server_mgmt_url()
93
token['storage_url'] = ''
94
token['user_id'] = user['uid']
95
self.db.auth_create_token(self.context, token)
90
token_dict['token_hash'] = token_hash
91
token_dict['cdn_management_url'] = ''
92
token_dict['server_management_url'] = self._get_server_mgmt_url()
93
token_dict['storage_url'] = ''
94
token_dict['user_id'] = user.id
95
token = self.db.auth_create_token(self.context, token_dict)