Skip to content

Commit

Permalink
Sync JWT and cookie expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Apr 22, 2024
1 parent 7201bfd commit 2e01b7d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/pypnusershub/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@

class ConfigurableBlueprint(Blueprint):
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"
)

# retro-compat set COOKIE_EXPIRATION in REMEMBER_COOKIE_DURATION
# (Flask Login parameter, default 1 year)
app.config["REMEMBER_COOKIE_DURATION"] = app.config.get(
"COOKIE_EXPIRATION", 31557600
)
parent = super(ConfigurableBlueprint, self)
parent.register(app, *args, **kwargs)

Expand Down Expand Up @@ -128,11 +131,13 @@ def login():
log.info(msg)
status_code = current_app.config.get("BAD_LOGIN_STATUS_CODE", 490)
return Response(msg, status=status_code)
login_user(user)
login_user(user, remember=True)
# Génération d'un token
token = encode_token(user_dict)
token_exp = datetime.datetime.now(datetime.timezone.utc)
token_exp += datetime.timedelta(seconds=current_app.config["COOKIE_EXPIRATION"])
token_exp += datetime.timedelta(
seconds=current_app.config["REMEMBER_COOKIE_DURATION"]
)
return jsonify(
{"user": user_dict, "expires": token_exp.isoformat(), "token": token.decode()}
)
Expand All @@ -155,7 +160,9 @@ def public_login():
# Génération d'un token
token = encode_token(user_dict)
token_exp = datetime.datetime.now(datetime.timezone.utc)
token_exp += datetime.timedelta(seconds=current_app.config["COOKIE_EXPIRATION"])
token_exp += datetime.timedelta(
seconds=current_app.config["REMEMBER_COOKIE_DURATION"]
)

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

0 comments on commit 2e01b7d

Please sign in to comment.