From 2eacbe943e4f448d7c5876b9b06dcce154d1d2c6 Mon Sep 17 00:00:00 2001 From: burzuf Date: Mon, 16 Mar 2020 22:24:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D1=83=D0=BD=D0=B8=D0=B2=D0=B5=D1=80=D1=81=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20=D1=85=D0=B5=D0=BD=D0=B4=D0=BB=D0=B5=D1=80?= =?UTF-8?q?=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20=D0=B8=20=D0=B2=D0=BE?= =?UTF-8?q?=D1=81=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BD=D0=B5=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2=D1=83=D1=8E=D1=89?= =?UTF-8?q?=D0=B5=D0=B3=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 38 +++++++++++++++++++++++++++++--------- db_routing.py | 6 +++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/__init__.py b/__init__.py index ba8acae..b461ab1 100644 --- a/__init__.py +++ b/__init__.py @@ -4,6 +4,7 @@ from flask_login import LoginManager, login_user, login_required, logout_user from db_routing import app, db import os import hashlib +from re import match login_manager = LoginManager(app) @@ -23,9 +24,10 @@ def register(): if request.method == 'POST': userName = request.form['RegUserLogin'] userPassw = request.form['RegUserPassw'] - if db_routing.add_user(userName, passw_hash(userPassw)): - login_user(db_routing.find_user(username=userName)) - return redirect(url_for('workshop')) + if string_check(userName) and string_check(userPassw): + if db_routing.add_user(userName, passw_hash(userPassw)): + login_user(db_routing.find_user(username=userName)) + return redirect(url_for('workshop')) return render_template('registration.html') @@ -33,10 +35,16 @@ def register(): def login(): userName = request.form['LogUserLogin'] userPassw = request.form['LogUserPassw'] - user = verify_password(userName, userPassw) - if user: - login_user(user) - return redirect(url_for('workshop')) + if string_check(userName) and string_check(userPassw): + user = verify_password(userName, userPassw) + if user: + login_user(user) + return redirect(url_for('workshop')) + else: + return redirect(url_for('register')) + + else: + return redirect(url_for('register')) @app.route('/logout') @@ -51,11 +59,22 @@ def workshop(): return render_template('workshop.html') -@app.errorhandler(404) -def not_found(error): +@app.errorhandler(Exception) +def universal_error(error): return render_template('error.html'), 404 +def string_check(string): + if 2 < len(string) < 7: + if match('^[0-9A-Za-z]*$', string) and not ('\\' in string): + return True + else: + print( + 'Некорректный ввод! Строка должно включать только английские буквы или цифры. Содержать не менее 3 и не ' + 'более 6 символов') + return False + + def passw_hash(user_passw, salt=os.urandom(32)): key = hashlib.pbkdf2_hmac('sha256', user_passw.encode('utf-8'), salt, 100000) storage = salt + key @@ -66,6 +85,7 @@ def passw_hash(user_passw, salt=os.urandom(32)): def verify_password(username, password): User = db_routing.find_user(username=username) + print(User) if User: userSalt = User.password[:32] if passw_hash(password, userSalt) == User.password: diff --git a/db_routing.py b/db_routing.py index 5bbfe2e..9dcb68b 100644 --- a/db_routing.py +++ b/db_routing.py @@ -14,7 +14,6 @@ class User(db.Model): username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) - def __init__(self, username, password): self.username = username self.password = password @@ -49,7 +48,7 @@ class Scenario(db.Model): owner_id = db.Column(db.Integer, ForeignKey('Users.id')) trigger_id = db.Column(db.Integer, ForeignKey('Triggers.id')) trigger_args = db.Column(db.String(200)) - action_id = db.Column(db.Integer, ForeignKey('Action.id')) + action_id = db.Column(db.Integer, ForeignKey('Actions.id')) action_args = db.Column(db.String(200)) @@ -70,4 +69,5 @@ def find_user(id=None, username=None): if username: return User.query.filter_by(username=username).first() -#def get_trigers(): + +# def get_trigers():