2017-08-16 10:57:44 +08:00
|
|
|
import os
|
|
|
|
|
2017-09-06 09:22:16 +08:00
|
|
|
import binascii
|
2017-09-17 11:21:46 +08:00
|
|
|
import base64
|
2017-08-14 07:43:33 +08:00
|
|
|
from flask import Flask, request, send_from_directory
|
|
|
|
from flask import render_template, redirect
|
|
|
|
from flask_cors import CORS
|
|
|
|
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user
|
2017-08-31 11:07:45 +08:00
|
|
|
|
|
|
|
from notes_api import notes_api
|
2017-09-13 09:06:09 +08:00
|
|
|
from sql import connect, getOption
|
2017-08-31 11:07:45 +08:00
|
|
|
from tree_api import tree_api
|
|
|
|
from notes_move_api import notes_move_api
|
2017-09-06 09:22:16 +08:00
|
|
|
from password_api import password_api
|
2017-09-13 11:04:17 +08:00
|
|
|
from settings_api import settings_api
|
2017-09-10 01:53:58 +08:00
|
|
|
import config_provider
|
2017-09-10 01:57:01 +08:00
|
|
|
import my_scrypt
|
2017-08-14 07:43:33 +08:00
|
|
|
|
2017-09-10 01:53:58 +08:00
|
|
|
config = config_provider.getConfig()
|
2017-08-16 10:57:44 +08:00
|
|
|
|
2017-09-13 09:06:09 +08:00
|
|
|
documentPath = config['Document']['documentPath']
|
|
|
|
connect(documentPath)
|
|
|
|
|
|
|
|
flask_secret_key = getOption("flask_secret_key")
|
|
|
|
|
|
|
|
if not flask_secret_key:
|
|
|
|
print("Application has not been setup yet. Run 'python setup.py' to finish setup.")
|
|
|
|
exit(1)
|
|
|
|
|
2017-08-14 07:43:33 +08:00
|
|
|
app = Flask(__name__)
|
2017-09-13 09:06:09 +08:00
|
|
|
app.secret_key = flask_secret_key
|
2017-08-31 11:07:45 +08:00
|
|
|
app.register_blueprint(tree_api)
|
|
|
|
app.register_blueprint(notes_api)
|
|
|
|
app.register_blueprint(notes_move_api)
|
2017-09-06 09:22:16 +08:00
|
|
|
app.register_blueprint(password_api)
|
2017-09-13 11:04:17 +08:00
|
|
|
app.register_blueprint(settings_api)
|
2017-08-14 07:43:33 +08:00
|
|
|
|
|
|
|
class User(UserMixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET'])
|
|
|
|
def login_form():
|
|
|
|
return render_template('login.html')
|
|
|
|
|
|
|
|
@app.route('/app', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def show_app():
|
|
|
|
return render_template('app.html')
|
|
|
|
|
|
|
|
@app.route('/logout', methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect('login')
|
|
|
|
|
|
|
|
user = User()
|
2017-09-13 09:06:09 +08:00
|
|
|
user.id = getOption('username')
|
2017-08-14 07:43:33 +08:00
|
|
|
|
2017-08-15 09:05:01 +08:00
|
|
|
port = config['Network']['port']
|
2017-08-17 08:48:40 +08:00
|
|
|
https = config['Network']['https']
|
2017-08-15 09:05:01 +08:00
|
|
|
certPath = config['Network']['certPath']
|
|
|
|
certKeyPath = config['Network']['certKeyPath']
|
|
|
|
|
2017-09-10 02:25:35 +08:00
|
|
|
def verify_password(guessed_password):
|
2017-09-17 11:21:46 +08:00
|
|
|
hashed_password = base64.b64decode(getOption('password_verification_hash'))
|
2017-09-06 09:22:16 +08:00
|
|
|
|
2017-09-10 02:21:57 +08:00
|
|
|
guess_hashed = my_scrypt.getVerificationHash(guessed_password)
|
2017-09-06 09:22:16 +08:00
|
|
|
|
2017-09-10 02:21:57 +08:00
|
|
|
return guess_hashed == hashed_password
|
2017-09-06 09:22:16 +08:00
|
|
|
|
2017-08-14 07:43:33 +08:00
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login_post():
|
2017-09-10 02:21:57 +08:00
|
|
|
guessedPassword = request.form['password'].encode('utf-8')
|
2017-08-14 07:43:33 +08:00
|
|
|
|
2017-09-10 02:25:35 +08:00
|
|
|
if request.form['username'] == user.id and verify_password(guessedPassword):
|
2017-08-14 07:43:33 +08:00
|
|
|
rememberMe = True if 'remember-me' in request.form else False
|
|
|
|
|
|
|
|
login_user(user, remember=rememberMe)
|
|
|
|
|
|
|
|
return redirect('app')
|
|
|
|
else:
|
|
|
|
return render_template('login.html', failedAuth=True)
|
|
|
|
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
@app.route('/stat/<path:path>')
|
|
|
|
def send_stc(path):
|
|
|
|
return send_from_directory(os.path.join(os.getcwd(), 'static'), path)
|
|
|
|
|
|
|
|
login_manager = LoginManager()
|
|
|
|
login_manager.init_app(app)
|
|
|
|
login_manager.login_view = 'login_form'
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
if user_id == user.id:
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2017-08-15 09:05:01 +08:00
|
|
|
if __name__ == "__main__":
|
2017-08-17 08:48:40 +08:00
|
|
|
ssl_context = None
|
|
|
|
|
|
|
|
if https == "true":
|
|
|
|
ssl_context = (certPath, certKeyPath)
|
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=port, ssl_context = ssl_context)
|