mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-25 18:26:34 +00:00
When returning the list of tokens, indicate which one is caller's
This commit is contained in:
parent
2ec9c8a441
commit
98e60abe74
|
@ -11,7 +11,7 @@ from selfprivacy_api.utils.auth import (
|
||||||
is_token_name_exists,
|
is_token_name_exists,
|
||||||
is_token_name_pair_valid,
|
is_token_name_pair_valid,
|
||||||
refresh_token,
|
refresh_token,
|
||||||
is_token_valid,
|
get_token_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,7 +36,18 @@ class Tokens(Resource):
|
||||||
400:
|
400:
|
||||||
description: Bad request
|
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):
|
def delete(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -86,6 +86,15 @@ def is_token_name_pair_valid(token_name, token):
|
||||||
return False
|
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():
|
def get_tokens_info():
|
||||||
"""Get all tokens info without tokens themselves"""
|
"""Get all tokens info without tokens themselves"""
|
||||||
with ReadUserData(UserDataFiles.TOKENS) as tokens:
|
with ReadUserData(UserDataFiles.TOKENS) as tokens:
|
||||||
|
|
|
@ -36,8 +36,12 @@ def test_get_tokens_info(authorized_client, tokens_file):
|
||||||
response = authorized_client.get("/auth/tokens")
|
response = authorized_client.get("/auth/tokens")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json == [
|
assert response.json == [
|
||||||
{"name": "test_token", "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"},
|
{
|
||||||
|
"name": "test_token2",
|
||||||
|
"date": "2022-01-14 08:31:10.789314",
|
||||||
|
"is_caller": False,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue