2022-02-08 11:26:20 +00:00
|
|
|
from app import db
|
2021-09-14 10:52:23 +00:00
|
|
|
from flask_restx import Namespace, Resource
|
|
|
|
from .auth import basic_auth, token_auth
|
|
|
|
|
|
|
|
|
2021-09-14 11:01:19 +00:00
|
|
|
ns = Namespace('tokens', description='Token operations')
|
2021-09-14 10:52:23 +00:00
|
|
|
|
|
|
|
|
2021-09-16 12:20:18 +00:00
|
|
|
@ns.route('')
|
2021-09-14 14:17:26 +00:00
|
|
|
class API_Tokens(Resource):
|
2021-09-14 10:52:23 +00:00
|
|
|
'''Get or revoke a user authentication token'''
|
|
|
|
|
|
|
|
@ns.doc(security='basicAuth')
|
|
|
|
@basic_auth.login_required
|
|
|
|
def post(self):
|
2021-09-14 14:17:26 +00:00
|
|
|
'''Get a user token'''
|
2021-09-14 10:52:23 +00:00
|
|
|
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):
|
2021-09-14 14:17:26 +00:00
|
|
|
'''Revoke a user token'''
|
2021-09-14 10:52:23 +00:00
|
|
|
token_auth.current_user().revoke_token()
|
|
|
|
db.session.commit()
|
|
|
|
return '', 204
|