2023-04-06 16:10:32 +02:00
|
|
|
from flask_json import as_json
|
2022-05-03 08:55:56 +02:00
|
|
|
from app import db, log
|
2022-07-27 16:03:14 +02:00
|
|
|
from app.api import api_bp as bp
|
|
|
|
from app.auth.logic import basic_auth, token_auth
|
2021-09-09 12:49:23 +02:00
|
|
|
|
2024-07-24 17:34:30 +02:00
|
|
|
"""
|
|
|
|
CATEGORY
|
|
|
|
--------
|
|
|
|
Authentification API
|
|
|
|
"""
|
|
|
|
|
2021-09-09 12:49:23 +02:00
|
|
|
|
|
|
|
@bp.route("/tokens", methods=["POST"])
|
|
|
|
@basic_auth.login_required
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-17 14:58:49 +02:00
|
|
|
def token_get():
|
2024-07-24 17:34:30 +02:00
|
|
|
"Renvoie un jeton jwt pour l'utilisateur courant."
|
2021-09-09 12:49:23 +02:00
|
|
|
token = basic_auth.current_user().get_token()
|
2022-05-03 08:55:56 +02:00
|
|
|
log(f"API: giving token to {basic_auth.current_user()}")
|
2021-09-09 12:49:23 +02:00
|
|
|
db.session.commit()
|
2023-04-06 16:10:32 +02:00
|
|
|
return {"token": token}
|
2021-09-09 12:49:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/tokens", methods=["DELETE"])
|
|
|
|
@token_auth.login_required
|
2024-07-17 14:58:49 +02:00
|
|
|
def token_revoke():
|
2024-07-24 17:34:30 +02:00
|
|
|
"Révoque le jeton de l'utilisateur courant."
|
2023-03-09 14:24:12 +01:00
|
|
|
user = token_auth.current_user()
|
|
|
|
user.revoke_token()
|
2021-09-09 12:49:23 +02:00
|
|
|
db.session.commit()
|
2023-03-09 14:24:12 +01:00
|
|
|
log(f"API: revoking token for {user}")
|
2021-09-09 12:49:23 +02:00
|
|
|
return "", 204
|