Skip to content

Commit

Permalink
Implemented an endpoint to list all active tokens for a user, see see p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattia D'Antonio committed Jun 8, 2016
1 parent a80e1a9 commit 5d2ffea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions restapi/resources/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ def get(self):
return self.response("", code=hcodes.HTTP_OK_NORESPONSE)


class Tokens(ExtendedApiResource):
""" List all active tokens for a user """

base_url = AUTH_URL

@auth.login_required
@decorate.apimethod
def get(self):
auth = self.global_get('custom_auth')
tokens = auth.list_all_tokens(auth._user)
return self.response(tokens)


class Profile(ExtendedApiResource):
""" Current user informations """

Expand Down
7 changes: 7 additions & 0 deletions restapi/resources/services/authentication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def get_user_object(self, username=None, payload=None):
"""
return

@abc.abstractmethod
def list_all_tokens(self, user):
"""
Return the list of all active tokens
"""
return

@abc.abstractmethod
def invalidate_all_tokens(self, user):
"""
Expand Down
19 changes: 19 additions & 0 deletions restapi/resources/services/authentication/graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ def verify_token_custom(self, token, user, payload):

return True

def list_all_tokens(self, user):
# TO FIX: TTL should be considered?

tokens = user.tokens.all()
list = []
for token in tokens:
t = {}

t["token"] = token.token
t["emitted"] = token.creation.strftime('%s')
t["last_access"] = token.last_access.strftime('%s')
if token.expiration is not None:
t["expiration"] = token.expiration.strftime('%s')
t["IP"] = token.IP
t["hostname"] = token.hostname
list.append(t)

return list

def invalidate_all_tokens(self, user):
user.uuid = self.getUUID()
user.save()
Expand Down

0 comments on commit 5d2ffea

Please sign in to comment.