diff --git a/__init__.py b/__init__.py index 862e609..2b53970 100644 --- a/__init__.py +++ b/__init__.py @@ -1,9 +1,14 @@ -from flask import Flask, render_template, flash, redirect, url_for, session, logging, request -from flask_sqlalchemy import SQLAlchemy +from flask import render_template, request +from db_routing import app +import db_routing +from flask_httpauth import HTTPBasicAuth +import os +import hashlib -app = Flask(__name__, static_folder="static", template_folder="templates") -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' -db = SQLAlchemy(app) + +if not os.path.exists('./data.db'): + db_routing.db.create_all() +auth = HTTPBasicAuth() @app.route('/', methods=['GET']) @@ -11,28 +16,44 @@ def index(): return render_template('index.html') -@app.route("/registration", methods=["GET", "POST"]) +@app.route('/registration', methods=['GET', 'POST']) def register(): - if request.method == "POST": - - UserLogin = request.form['UserLogin'] - UserPass = request.form['UserPass'] - - #db.session.add(UserLogin, UserPass) - #db.session.commit() - - #return redirect(url_for("")) - - print(UserLogin, ' ', UserPass) - # return страница с контентом - - return render_template("registration.html") + if request.method == 'POST': + userLogin = request.form['UserLogin'] + userPassw = request.form['UserPassw'] + db_routing.add_user(userLogin, passw_hash(userPassw)) + return render_template('registration.html') -# @app.route('/login', methods=['GET']) -# def login(): -# return render_template('login.html') +@auth.verify_password +def verify_password(user_login, user_passw): + user = db_routing.find_user(user_login) + if user: + userSalt = user[2][:32] + if passw_hash(user_passw, userSalt) == user[2]: + return True + else: + return False -if __name__ == "__main__": - app.run() +@app.route('/content', methods=['GET']) +@auth.login_required +def content(): + return render_template('content.html') + + +# @app.errorhandler(404) +# def not_found(error): +# return render_template('404.html'), 404 + + +def passw_hash(user_passw, salt=os.urandom(32)): + key = hashlib.pbkdf2_hmac('sha256', user_passw.encode('utf-8'), salt, 100000) + storage = salt + key + # salt_from_storage = storage[:32] # 32 длина соли + # key_from_storage = storage[32:] + return storage + + +if __name__ == '__main__': + db_routing.app.run() diff --git a/data.db b/data.db deleted file mode 100644 index 2c17d26..0000000 Binary files a/data.db and /dev/null differ diff --git a/db_routing.py b/db_routing.py new file mode 100644 index 0000000..0a6a560 --- /dev/null +++ b/db_routing.py @@ -0,0 +1,27 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + + +app = Flask(__name__, static_folder='static', template_folder='templates') +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' +db = SQLAlchemy(app) + + +class User(db.Model): + __tablename__ = 'users' + UserID = db.Column(db.Integer, primary_key=True) + UserName = db.Column(db.String(80), unique=True, nullable=False) + UserPassw = db.Column(db.String(120), nullable=False) + + +def add_user(user_name, passw_hash): + if not find_user(user_name): + new_user = User(UserName=user_name, UserPassw=passw_hash) + db.session.add(new_user) + db.session.commit() + else: + print('Логин занят') + + +def find_user(user_name): + return User.query.filter_by(UserName=user_name).first() diff --git a/requirements.txt b/requirements.txt index 8f8c054..74fd393 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ click==7.1.1 Flask==1.1.1 +Flask-HTTPAuth==3.3.0 Flask-SQLAlchemy==2.4.1 itsdangerous==1.1.0 Jinja2==2.11.1 diff --git a/templates/content.html b/templates/content.html index eb6aa68..0b8a642 100644 --- a/templates/content.html +++ b/templates/content.html @@ -29,7 +29,7 @@
- Лист событий + Настройки
@@ -38,9 +38,9 @@