nopaque/app/api/tokens.py

28 lines
712 B
Python
Raw Normal View History

from flask_restx import Namespace, Resource
from .auth import basic_auth, token_auth
from .. import db
2021-09-14 11:01:19 +00:00
ns = Namespace('tokens', description='Token operations')
@ns.route('/')
class Token(Resource):
'''Get or revoke a user authentication token'''
@ns.doc(security='basicAuth')
@basic_auth.login_required
def post(self):
'''Get user token'''
token = basic_auth.current_user().get_token()
db.session.commit()
return {'token': 'Bearer ' + token}
@ns.doc(security='apiKey')
@token_auth.login_required
def delete(self):
'''Revoke user token'''
token_auth.current_user().revoke_token()
db.session.commit()
return '', 204