Skip to content

Commit

Permalink
fix(login): add function get_user_data to fix inpn cas login
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentCauchois committed Mar 26, 2024
1 parent 975a3eb commit 476620e
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/pypnusershub/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging

import datetime
from flask_login import login_user, logout_user, current_user
from flask_login import login_required, login_user, logout_user, current_user
from flask import (
Blueprint,
request,
Expand Down Expand Up @@ -68,9 +68,7 @@ def register(self, app, *args, **kwargs):
# set cookie autorenew
app.config["PASS_METHOD"] = app.config.get("PASS_METHOD", "hash")

app.config["REMEMBER_COOKIE_NAME"] = app.config.get(
"REMEMBER_COOKIE_NAME", "token"
)
app.config["REMEMBER_COOKIE_NAME"] = app.config.get("REMEMBER_COOKIE_NAME", "token")

parent = super(ConfigurableBlueprint, self)
parent.register(app, *args, **kwargs)
Expand All @@ -86,6 +84,23 @@ def load_current_user():
from pypnusershub.decorators import check_auth


@routes.route("/get_current_user")
@login_required
def get_user_data():
user_dict = UserSchema(exclude=["remarques"], only=["+max_level_profil"]).dump(g.current_user)

token_exp = datetime.datetime.now(datetime.timezone.utc)
token_exp += datetime.timedelta(seconds=current_app.config["COOKIE_EXPIRATION"])
data = {
"user": user_dict,
"token": encode_token(g.current_user.as_dict()).decode(),
"expires": token_exp.isoformat(),
}
print(data)

return jsonify(data)


@routes.route("/login", methods=["POST"])
def login():
user_data = request.json
Expand All @@ -106,9 +121,7 @@ def login():
.where(models.User.identifiant == login)
.where(models.User.filter_by_app())
).scalar_one()
user_dict = UserSchema(exclude=["remarques"], only=["+max_level_profil"]).dump(
user
)
user_dict = UserSchema(exclude=["remarques"], only=["+max_level_profil"]).dump(user)
except exc.NoResultFound as e:
msg = json.dumps(
{
Expand All @@ -133,9 +146,7 @@ def login():
token = encode_token(user_dict)
token_exp = datetime.datetime.now(datetime.timezone.utc)
token_exp += datetime.timedelta(seconds=current_app.config["COOKIE_EXPIRATION"])
return jsonify(
{"user": user_dict, "expires": token_exp.isoformat(), "token": token.decode()}
)
return jsonify({"user": user_dict, "expires": token_exp.isoformat(), "token": token.decode()})


@routes.route("/public_login", methods=["POST"])
Expand All @@ -157,9 +168,7 @@ def public_login():
token_exp = datetime.datetime.now(datetime.timezone.utc)
token_exp += datetime.timedelta(seconds=current_app.config["COOKIE_EXPIRATION"])

return jsonify(
{"user": user_dict, "expires": token_exp.isoformat(), "token": token.decode()}
)
return jsonify({"user": user_dict, "expires": token_exp.isoformat(), "token": token.decode()})


@routes.route("/logout", methods=["GET", "POST"])
Expand Down

0 comments on commit 476620e

Please sign in to comment.