Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync JWT and cookie expiration #94

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@jacquesfize jacquesfize Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

la durée par défaut est de 1 an ? ça fait pas beaucoup ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
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
Loading