-
Notifications
You must be signed in to change notification settings - Fork 39
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
Separated login and login_info routes #1140
Changes from 9 commits
55c029d
fc27f47
72f0bee
81c82a8
9dd45d4
6a741a3
531ff73
cecf8cc
b38bb14
34b1f83
1cf98d3
d27b122
bb5c56d
fe40504
17fa748
7bd7a66
dce890d
30c7a03
9268189
960470d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,23 +1,10 @@ | ||||||
import logging | ||||||
|
||||||
from flask import ( | ||||||
Blueprint, | ||||||
request, | ||||||
jsonify, | ||||||
make_response, | ||||||
redirect, | ||||||
session, | ||||||
) | ||||||
from flask import Blueprint, request, jsonify, make_response, session | ||||||
from mxcubeweb.core.util import networkutils | ||||||
from flask_login import current_user | ||||||
|
||||||
|
||||||
def deny_access(msg): | ||||||
resp = jsonify({"msg": msg}) | ||||||
resp.code = 409 | ||||||
return resp | ||||||
|
||||||
|
||||||
def init_route(app, server, url_prefix): | ||||||
bp = Blueprint("login", __name__, url_prefix=url_prefix) | ||||||
|
||||||
|
@@ -45,15 +32,17 @@ def login(): | |||||
password = params.get("password", "") | ||||||
|
||||||
try: | ||||||
res = jsonify(app.usermanager.login(login_id, password)) | ||||||
app.usermanager.login(login_id, password) | ||||||
except Exception as ex: | ||||||
msg = "[LOGIN] User %s could not login (%s)" % ( | ||||||
login_id, | ||||||
str(ex), | ||||||
) | ||||||
logging.getLogger("MX3.HWR").exception("") | ||||||
logging.getLogger("MX3.HWR").info(msg) | ||||||
res = deny_access("Could not authenticate") | ||||||
res = make_response(jsonify({"msg": "Could not authenticate"}), 200) | ||||||
else: | ||||||
res = make_response(jsonify({"msg": ""}), 200) | ||||||
fabcor-maxiv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
session.permanent = True | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I managed to get the tests working by adding back this line which I initially removed, as I thought that setting However it seems that the configuration value is not taken into account and we need to set it programmatically, have a look at our mockup server config Documentation even states that its true by default: https://flask-session.readthedocs.io/en/latest/config.html#configuration Does someone have an idea of what that could be ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recall it not being straightforward to understand. It took me a while to wrap my head around sessions and their expiration. I had written some doc, but obviously this doc is not good enough since reading it back now does not clarify much... Flask and Flask-session have different behavior, different defaults. Flask does not seem to know about the I made a quick search and I am starting to wonder if flask-session is used at all. I can not seem to find where it is used in the code. Am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed that explains it, so looking at the flask documentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably remove line 29 from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :), Actually I thought if flask does not deal with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a second thought, thats just unnecessary ... Ill remove the config option as we are actually relying on this feature, it does not make any sense to set it to false in our case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder now if it makes sense (or is needed) to declare the session permanent when authentication failed. It seems to work as it is so maybe it is fine to leave it like this, but curiosity... |
||||||
|
||||||
|
@@ -66,8 +55,7 @@ def signout(): | |||||
Signout from MXCuBE Web and reset the session | ||||||
""" | ||||||
app.usermanager.signout() | ||||||
|
||||||
return redirect("/login", code=302) | ||||||
return make_response(jsonify(""), 200) | ||||||
fabcor-maxiv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
@bp.route("/login_info", methods=["GET"]) | ||||||
def login_info(): | ||||||
|
@@ -88,16 +76,14 @@ def login_info(): | |||||
|
||||||
Status code set to: | ||||||
200: On success | ||||||
409: Error, could not log in | ||||||
401: Error, could not log in | ||||||
""" | ||||||
user, res = app.usermanager.login_info() | ||||||
|
||||||
# Redirect the user to login page if for some reason logged out | ||||||
# i.e. server restart | ||||||
if not user: | ||||||
response = redirect("/login", code=302) | ||||||
else: | ||||||
try: | ||||||
res = app.usermanager.login_info() | ||||||
response = jsonify(res) | ||||||
except Exception: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about having a more specific Exception type? See
Suggested change
|
||||||
response = make_response(jsonify(""), 401) | ||||||
|
||||||
return response | ||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe raise a more specific exception type? See
mxcubeweb/routes/login.py
line 85 where is is caught.