spaceport/authserver/server.py

83 lines
2.0 KiB
Python
Raw Normal View History

2020-11-23 07:27:46 +00:00
from log import logger
2020-09-16 21:41:55 +00:00
from flask import Flask, abort, request
app = Flask(__name__)
import auth_functions
import secrets
HTTP_UNAUTHORIZED = 401
def check_auth():
auth_header = request.headers.get('Authorization', '')
if auth_header != 'Token ' + secrets.AUTH_TOKEN:
2021-10-14 22:47:10 +00:00
logger.info('Bad auth token, aborting.')
2020-09-16 21:41:55 +00:00
abort(HTTP_UNAUTHORIZED)
@app.route('/')
def index():
2020-11-23 07:27:46 +00:00
logger.info('Index page requested')
2020-11-22 01:30:02 +00:00
return '<i>LIFE IS BUT A DREAM...</i>'
2020-09-16 21:41:55 +00:00
2020-11-23 07:27:46 +00:00
@app.route('/ping')
def ping():
return 'pong'
2021-09-04 06:19:57 +00:00
@app.route('/set-wiki-password', methods=['POST'])
2021-09-04 22:54:17 +00:00
def set_wiki_password():
2020-09-16 21:41:55 +00:00
check_auth()
username = request.form['username']
password = request.form['password']
auth_functions.set_wiki_password(username, password)
2020-09-16 21:41:55 +00:00
return ''
2021-09-04 06:19:57 +00:00
@app.route('/set-discourse-password', methods=['POST'])
2021-09-04 22:54:17 +00:00
def set_discourse_password():
2021-09-04 06:19:57 +00:00
check_auth()
username = request.form['username']
password = request.form['password']
first_name = request.form['first_name']
email = request.form['email']
auth_functions.set_discourse_password(username, password, first_name, email)
return ''
@app.route('/add-discourse-group-members', methods=['POST'])
def add_discourse_group_members():
check_auth()
data = request.get_json()
group_name = data['group_name']
usernames = data['usernames']
auth_functions.add_discourse_group_members(group_name, usernames)
return ''
@app.route('/remove-discourse-group-members', methods=['POST'])
def remove_discourse_group_members():
check_auth()
data = request.get_json()
group_name = data['group_name']
usernames = data['usernames']
auth_functions.remove_discourse_group_members(group_name, usernames)
return ''
2021-10-14 22:47:10 +00:00
@app.route('/change-discourse-username', methods=['POST'])
def change_discourse_username():
check_auth()
username = request.form['username']
new_username = request.form['new_username']
auth_functions.change_discourse_username(username, new_username)
return ''
2020-09-16 21:41:55 +00:00
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')