diff --git a/__init__.py b/__init__.py index 862e609..2bc514c 100644 --- a/__init__.py +++ b/__init__.py @@ -1,9 +1,16 @@ -from flask import Flask, render_template, flash, redirect, url_for, session, logging, request -from flask_sqlalchemy import SQLAlchemy +from flask import Flask, render_template, request +import db_routing +from flask_httpauth import HTTPBasicAuth +import os +import hashlib -app = Flask(__name__, static_folder="static", template_folder="templates") + +if not os.path.exists('./data.db'): + db_routing.db.create_all() + +app = Flask('Jarvis', static_folder='static', template_folder='templates') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' -db = SQLAlchemy(app) +auth = HTTPBasicAuth() @app.route('/', methods=['GET']) @@ -11,28 +18,45 @@ 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'] + if db_routing.add_user(userLogin, passw_hash(userPassw)): + return render_template('content.html') + 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.UserPassw[:32] + if passw_hash(user_passw, userSalt) == User.UserPassw: + return True + else: + return False -if __name__ == "__main__": +@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__': 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..7862aab --- /dev/null +++ b/db_routing.py @@ -0,0 +1,27 @@ +from flask_sqlalchemy import SQLAlchemy +from __init__ import app + + +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() + return True + else: + print('Логин занят') + return False + + +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/registration.html b/templates/registration.html index 2559e90..dc10158 100644 --- a/templates/registration.html +++ b/templates/registration.html @@ -79,7 +79,7 @@