You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
71 lines
1.6 KiB
from functools import wraps
|
|
from typing import Any, Callable, NamedTuple
|
|
|
|
from flask import Blueprint, jsonify, Response, session
|
|
|
|
from atheneum.authentication import (
|
|
generate_token,
|
|
require_basic_auth,
|
|
require_token_auth
|
|
)
|
|
|
|
api_blueprint = Blueprint(name='api', import_name=__name__, url_prefix='/api')
|
|
|
|
|
|
class APIResponse(NamedTuple):
|
|
payload: Any
|
|
status: int
|
|
|
|
|
|
def return_json(func: Callable) -> Callable:
|
|
"""
|
|
If an Response object is not returned, jsonify the result and return it
|
|
:param func:
|
|
:return:
|
|
"""
|
|
|
|
@wraps(func)
|
|
def decorate(*args, **kwargs):
|
|
result = func(*args, **kwargs)
|
|
if isinstance(result, Response):
|
|
return result
|
|
if isinstance(result, APIResponse):
|
|
return jsonify(result.payload), result.status
|
|
return jsonify(result)
|
|
|
|
return decorate
|
|
|
|
|
|
@api_blueprint.route('/login', methods=['POST'])
|
|
@return_json
|
|
@require_basic_auth
|
|
def login() -> APIResponse:
|
|
"""
|
|
Get a token for continued authentication
|
|
:return: A login token for continued authentication
|
|
"""
|
|
token = generate_token()
|
|
return APIResponse({'token': token}, 200)
|
|
|
|
|
|
@api_blueprint.route('/login/bump', methods=['POST'])
|
|
@return_json
|
|
@require_token_auth
|
|
def login_bump() -> APIResponse:
|
|
"""
|
|
Update the user last seen timestamp
|
|
:return: A time stamp for the bumped login
|
|
"""
|
|
return APIResponse(None, 200)
|
|
|
|
|
|
@api_blueprint.route('/logout', methods=['POST'])
|
|
@return_json
|
|
@require_token_auth
|
|
def logout() -> APIResponse:
|
|
"""
|
|
logout and delete a token
|
|
:return:
|
|
"""
|
|
session.pop('user')
|
|
return APIResponse(None, 200)
|