2022-01-14 05:38:53 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""App tokens management module"""
|
|
|
|
from flask import request
|
|
|
|
from flask_restful import Resource, reqparse
|
|
|
|
|
|
|
|
from selfprivacy_api.resources.api_auth import api
|
|
|
|
from selfprivacy_api.utils.auth import (
|
|
|
|
delete_token,
|
|
|
|
get_tokens_info,
|
2022-01-17 11:28:17 +00:00
|
|
|
is_token_name_exists,
|
|
|
|
is_token_name_pair_valid,
|
2022-01-14 05:38:53 +00:00
|
|
|
refresh_token,
|
2022-02-16 06:40:31 +00:00
|
|
|
get_token_name,
|
2022-01-14 05:38:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Tokens(Resource):
|
|
|
|
"""Token management class
|
|
|
|
GET returns the list of active devices.
|
|
|
|
DELETE invalidates token unless it is the last one or the caller uses this token.
|
|
|
|
POST refreshes the token of the caller.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
"""
|
|
|
|
Get current device tokens
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- Tokens
|
|
|
|
security:
|
|
|
|
- bearerAuth: []
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: List of tokens
|
|
|
|
400:
|
|
|
|
description: Bad request
|
|
|
|
"""
|
2022-02-16 06:40:31 +00:00
|
|
|
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
|
|
|
|
]
|
2022-01-14 05:38:53 +00:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Delete token
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- Tokens
|
|
|
|
security:
|
|
|
|
- bearerAuth: []
|
|
|
|
parameters:
|
|
|
|
- in: body
|
2022-01-17 11:28:17 +00:00
|
|
|
name: token
|
|
|
|
required: true
|
|
|
|
description: Token's name to delete
|
|
|
|
schema:
|
|
|
|
type: object
|
|
|
|
properties:
|
2022-05-12 15:55:57 +00:00
|
|
|
token_name:
|
2022-01-17 11:28:17 +00:00
|
|
|
type: string
|
|
|
|
description: Token name to delete
|
2022-02-02 12:51:48 +00:00
|
|
|
required: true
|
2022-01-14 05:38:53 +00:00
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: Token deleted
|
|
|
|
400:
|
|
|
|
description: Bad request
|
|
|
|
404:
|
|
|
|
description: Token not found
|
|
|
|
"""
|
|
|
|
parser = reqparse.RequestParser()
|
2022-01-17 11:29:54 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"token_name", type=str, required=True, help="Token to delete"
|
|
|
|
)
|
2022-01-14 05:38:53 +00:00
|
|
|
args = parser.parse_args()
|
2022-01-18 15:20:47 +00:00
|
|
|
token_name = args["token_name"]
|
2022-01-17 11:29:54 +00:00
|
|
|
if is_token_name_pair_valid(
|
|
|
|
token_name, request.headers.get("Authorization").split(" ")[1]
|
|
|
|
):
|
2022-01-14 05:38:53 +00:00
|
|
|
return {"message": "Cannot delete caller's token"}, 400
|
2022-01-17 11:28:17 +00:00
|
|
|
if not is_token_name_exists(token_name):
|
2022-01-14 05:38:53 +00:00
|
|
|
return {"message": "Token not found"}, 404
|
2022-01-17 11:28:17 +00:00
|
|
|
delete_token(token_name)
|
2022-01-14 05:38:53 +00:00
|
|
|
return {"message": "Token deleted"}, 200
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
"""
|
|
|
|
Refresh token
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- Tokens
|
|
|
|
security:
|
|
|
|
- bearerAuth: []
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: Token refreshed
|
|
|
|
400:
|
|
|
|
description: Bad request
|
|
|
|
404:
|
|
|
|
description: Token not found
|
|
|
|
"""
|
|
|
|
# Get token from header
|
|
|
|
token = request.headers.get("Authorization").split(" ")[1]
|
2022-01-18 15:20:47 +00:00
|
|
|
new_token = refresh_token(token)
|
|
|
|
if new_token is None:
|
|
|
|
return {"message": "Token not found"}, 404
|
|
|
|
return {"token": new_token}, 200
|
2022-01-14 05:38:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(Tokens, "/tokens")
|