2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
|
|
|
|
2021-06-05 04:49:45 +00:00
|
|
|
from voussoirkit import flasktools
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
import etiquette
|
|
|
|
|
2018-07-19 01:37:21 +00:00
|
|
|
from .. import common
|
2018-01-12 03:40:56 +00:00
|
|
|
from .. import decorators
|
|
|
|
from .. import sessions
|
|
|
|
|
|
|
|
site = common.site
|
|
|
|
session_manager = common.session_manager
|
|
|
|
|
|
|
|
# Individual users #################################################################################
|
|
|
|
|
2018-02-17 22:59:02 +00:00
|
|
|
@site.route('/user/<username>')
|
2018-01-12 03:40:56 +00:00
|
|
|
def get_user_html(username):
|
|
|
|
user = common.P_user(username, response_type='html')
|
2019-08-14 20:40:52 +00:00
|
|
|
return common.render_template(request, 'user.html', user=user)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-02-17 22:59:02 +00:00
|
|
|
@site.route('/user/<username>.json')
|
2018-01-12 03:40:56 +00:00
|
|
|
def get_user_json(username):
|
|
|
|
user = common.P_user(username, response_type='json')
|
2021-01-01 20:56:05 +00:00
|
|
|
user = user.jsonify()
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(user)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/userid/<user_id>')
|
|
|
|
@site.route('/userid/<user_id>.json')
|
|
|
|
def get_user_id_redirect(user_id):
|
2021-01-08 07:04:10 +00:00
|
|
|
if request.path.endswith('.json'):
|
2018-01-12 03:40:56 +00:00
|
|
|
user = common.P_user_id(user_id, response_type='json')
|
|
|
|
else:
|
|
|
|
user = common.P_user_id(user_id, response_type='html')
|
|
|
|
url_from = '/userid/' + user_id
|
|
|
|
url_to = '/user/' + user.username
|
|
|
|
url = request.url.replace(url_from, url_to)
|
|
|
|
return flask.redirect(url)
|
|
|
|
|
2021-01-02 03:46:31 +00:00
|
|
|
@site.route('/user/<username>/edit', methods=['POST'])
|
|
|
|
def post_user_edit(username):
|
|
|
|
session = session_manager.get(request)
|
2021-01-09 23:42:51 +00:00
|
|
|
|
2021-01-02 03:46:31 +00:00
|
|
|
if not session:
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
|
2021-01-02 03:46:31 +00:00
|
|
|
user = common.P_user(username, response_type='json')
|
|
|
|
if session.user != user:
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
|
2021-01-02 03:46:31 +00:00
|
|
|
|
|
|
|
display_name = request.form.get('display_name')
|
|
|
|
if display_name is not None:
|
|
|
|
user.set_display_name(display_name)
|
|
|
|
|
|
|
|
common.P.commit()
|
|
|
|
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(user.jsonify())
|
2021-01-02 03:46:31 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Login and logout #################################################################################
|
|
|
|
|
|
|
|
@site.route('/login', methods=['GET'])
|
|
|
|
def get_login():
|
2019-08-14 20:40:52 +00:00
|
|
|
response = common.render_template(
|
|
|
|
request,
|
2018-04-15 20:20:08 +00:00
|
|
|
'login.html',
|
2018-04-15 21:32:18 +00:00
|
|
|
min_username_length=common.P.config['user']['min_username_length'],
|
2018-04-15 20:20:08 +00:00
|
|
|
min_password_length=common.P.config['user']['min_password_length'],
|
|
|
|
)
|
|
|
|
return response
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/login', methods=['POST'])
|
|
|
|
@decorators.required_fields(['username', 'password'])
|
|
|
|
def post_login():
|
2018-01-16 02:41:21 +00:00
|
|
|
session = session_manager.get(request)
|
|
|
|
if session.user:
|
2018-01-12 03:40:56 +00:00
|
|
|
exc = etiquette.exceptions.AlreadySignedIn()
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(response, status=403)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
username = request.form['username']
|
|
|
|
password = request.form['password']
|
|
|
|
try:
|
|
|
|
# Consideration: Should the server hash the password to discourage
|
|
|
|
# information (user exists) leak via response time?
|
|
|
|
# Currently I think not, because they can check if the account
|
|
|
|
# page 404s anyway.
|
2020-09-18 04:02:55 +00:00
|
|
|
user = common.P.login(username=username, password=password)
|
2018-01-12 03:40:56 +00:00
|
|
|
except (etiquette.exceptions.NoSuchUser, etiquette.exceptions.WrongLogin):
|
|
|
|
exc = etiquette.exceptions.WrongLogin()
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(response, status=422)
|
2018-01-12 03:40:56 +00:00
|
|
|
except etiquette.exceptions.FeatureDisabled as exc:
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(response, status=400)
|
2018-01-12 03:40:56 +00:00
|
|
|
session = sessions.Session(request, user)
|
|
|
|
session_manager.add(session)
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response({})
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-08-18 05:23:40 +00:00
|
|
|
@site.route('/logout', methods=['POST'])
|
2018-01-12 03:40:56 +00:00
|
|
|
def logout():
|
|
|
|
session_manager.remove(request)
|
2021-06-05 04:49:45 +00:00
|
|
|
response = flasktools.make_json_response({})
|
2018-01-12 03:40:56 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
# User registration ################################################################################
|
|
|
|
|
|
|
|
@site.route('/register', methods=['GET'])
|
|
|
|
def get_register():
|
|
|
|
return flask.redirect('/login')
|
|
|
|
|
|
|
|
@site.route('/register', methods=['POST'])
|
|
|
|
@decorators.required_fields(['username', 'password_1', 'password_2'])
|
|
|
|
def post_register():
|
2018-01-16 02:41:21 +00:00
|
|
|
session = session_manager.get(request)
|
|
|
|
if session.user:
|
2018-01-12 03:40:56 +00:00
|
|
|
exc = etiquette.exceptions.AlreadySignedIn()
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(response, status=403)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
username = request.form['username']
|
2019-08-26 21:32:51 +00:00
|
|
|
display_name = request.form.get('display_name', None)
|
2018-01-12 03:40:56 +00:00
|
|
|
password_1 = request.form['password_1']
|
|
|
|
password_2 = request.form['password_2']
|
|
|
|
|
|
|
|
if password_1 != password_2:
|
|
|
|
response = {
|
|
|
|
'error_type': 'PASSWORDS_DONT_MATCH',
|
|
|
|
'error_message': 'Passwords do not match.',
|
|
|
|
}
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response(response, status=422)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2020-02-20 08:34:28 +00:00
|
|
|
user = common.P.new_user(username, password_1, display_name=display_name, commit=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
session = sessions.Session(request, user)
|
|
|
|
session_manager.add(session)
|
2021-06-05 04:49:45 +00:00
|
|
|
return flasktools.make_json_response({})
|