Add issued handlers to auth_ref and fixtures

issued_at is a standard part of V2 and V3 tokens so add it to
AccessInfo in a similar way to expiry. Also it should be included when
generating tokens so include it in fixtures.

Change-Id: I0d62d8ce6472466886751e10e98046b8e398e079
This commit is contained in:
Jamie Lennox 2014-06-18 11:19:36 +10:00
parent bbf4c3ab5f
commit 158dadc028
3 changed files with 71 additions and 6 deletions

View File

@ -120,6 +120,14 @@ class AccessInfo(dict):
"""
raise NotImplementedError()
@property
def issued(self):
"""Returns the token issue time (as datetime object)
:returns: datetime
"""
raise NotImplementedError()
@property
def username(self):
"""Returns the username associated with the authentication request.
@ -363,6 +371,10 @@ class AccessInfoV2(AccessInfo):
def expires(self):
return timeutils.parse_isotime(self['token']['expires'])
@property
def issued(self):
return timeutils.parse_isotime(self['token']['issued_at'])
@property
def username(self):
return self['user'].get('name', self['user'].get('username'))
@ -529,6 +541,10 @@ class AccessInfoV3(AccessInfo):
def expires(self):
return timeutils.parse_isotime(self['expires_at'])
@property
def issued(self):
return timeutils.parse_isotime(self['issued_at'])
@property
def user_id(self):
return self['user']['id']

View File

@ -40,8 +40,8 @@ class Token(dict):
that matter to them and not copy and paste sample.
"""
def __init__(self, token_id=None,
expires=None, tenant_id=None, tenant_name=None, user_id=None,
def __init__(self, token_id=None, expires=None, issued=None,
tenant_id=None, tenant_name=None, user_id=None,
user_name=None):
super(Token, self).__init__()
@ -49,8 +49,16 @@ class Token(dict):
self.user_id = user_id or uuid.uuid4().hex
self.user_name = user_name or uuid.uuid4().hex
if not issued:
issued = timeutils.utcnow() - datetime.timedelta(minutes=2)
if not expires:
expires = timeutils.utcnow() + datetime.timedelta(hours=1)
expires = issued + datetime.timedelta(hours=1)
try:
self.issued = issued
except (TypeError, AttributeError):
# issued should be able to be passed as a string so ignore
self.issued_str = issued
try:
self.expires = expires
@ -93,6 +101,22 @@ class Token(dict):
def expires(self, value):
self.expires_str = timeutils.isotime(value)
@property
def issued_str(self):
return self._token['issued_at']
@issued_str.setter
def issued_str(self, value):
self._token['issued_at'] = value
@property
def issued(self):
return timeutils.parse_isotime(self.issued_str)
@issued.setter
def issued(self, value):
self.issued_str = timeutils.isotime(value)
@property
def _user(self):
return self.root.setdefault('user', {})

View File

@ -54,7 +54,7 @@ class Token(dict):
that matter to them and not copy and paste sample.
"""
def __init__(self, expires=None, user_id=None, user_name=None,
def __init__(self, expires=None, issued=None, user_id=None, user_name=None,
user_domain_id=None, user_domain_name=None, methods=None,
project_id=None, project_name=None, project_domain_id=None,
project_domain_name=None, domain_id=None, domain_name=None,
@ -71,8 +71,17 @@ class Token(dict):
methods = ['password']
self.methods.extend(methods)
if not issued:
issued = timeutils.utcnow() - datetime.timedelta(minutes=2)
try:
self.issued = issued
except (TypeError, AttributeError):
# issued should be able to be passed as a string so ignore
self.issued_str = issued
if not expires:
expires = timeutils.utcnow() + datetime.timedelta(hours=1)
expires = self.issued + datetime.timedelta(hours=1)
try:
self.expires = expires
@ -115,7 +124,23 @@ class Token(dict):
@expires.setter
def expires(self, value):
self.expires_str = timeutils.isotime(value)
self.expires_str = timeutils.isotime(value, subsecond=True)
@property
def issued_str(self):
return self.root.get('issued_at')
@issued_str.setter
def issued_str(self, value):
self.root['issued_at'] = value
@property
def issued(self):
return timeutils.parse_isotime(self.issued_str)
@issued.setter
def issued(self, value):
self.issued_str = timeutils.isotime(value, subsecond=True)
@property
def _user(self):