When returning the list of tokens, indicate which one is caller's
continuous-integration/drone/push Build is passing Details

pull/9/head
Inex Code 2022-02-16 09:40:31 +03:00
parent 2ec9c8a441
commit 98e60abe74
3 changed files with 28 additions and 4 deletions

View File

@ -11,7 +11,7 @@ from selfprivacy_api.utils.auth import (
is_token_name_exists,
is_token_name_pair_valid,
refresh_token,
is_token_valid,
get_token_name,
)
@ -36,7 +36,18 @@ class Tokens(Resource):
400:
description: Bad request
"""
return get_tokens_info()
caller_name = get_token_name(request.headers.get("Authorization").split(" ")[1])
tokens = get_tokens_info()
# Retrun a list of tokens and if it is the caller's token
# it will be marked with a flag
return [
{
"name": token["name"],
"date": token["date"],
"is_caller": token["name"] == caller_name,
}
for token in tokens
]
def delete(self):
"""

View File

@ -86,6 +86,15 @@ def is_token_name_pair_valid(token_name, token):
return False
def get_token_name(token):
"""Return the name of the token provided"""
with ReadUserData(UserDataFiles.TOKENS) as tokens:
for t in tokens["tokens"]:
if t["token"] == token:
return t["name"]
return None
def get_tokens_info():
"""Get all tokens info without tokens themselves"""
with ReadUserData(UserDataFiles.TOKENS) as tokens:

View File

@ -36,8 +36,12 @@ def test_get_tokens_info(authorized_client, tokens_file):
response = authorized_client.get("/auth/tokens")
assert response.status_code == 200
assert response.json == [
{"name": "test_token", "date": "2022-01-14 08:31:10.789314"},
{"name": "test_token2", "date": "2022-01-14 08:31:10.789314"},
{"name": "test_token", "date": "2022-01-14 08:31:10.789314", "is_caller": True},
{
"name": "test_token2",
"date": "2022-01-14 08:31:10.789314",
"is_caller": False,
},
]