From 2cddf6fa102ddfa4861fff5f3ca1fba5b2301c84 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 12:03:26 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2=D1=83?= =?UTF-8?q?=D1=8E=D1=89=D0=B5=D0=B3=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 44 +++++++++++++++++++++++++++++++----------- templates/content.html | 6 +++--- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/__init__.py b/__init__.py index 862e609..3cb2be1 100644 --- a/__init__.py +++ b/__init__.py @@ -1,7 +1,10 @@ from flask import Flask, render_template, flash, redirect, url_for, session, logging, request from flask_sqlalchemy import SQLAlchemy +import os +import hashlib +import sqlite3 -app = Flask(__name__, static_folder="static", template_folder="templates") +app = Flask(__name__, static_folder='static', template_folder='templates') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' db = SQLAlchemy(app) @@ -11,28 +14,47 @@ def index(): return render_template('index.html') -@app.route("/registration", methods=["GET", "POST"]) +@app.route('/registraion', methods=['GET', 'POST']) def register(): - if request.method == "POST": - + if request.method == 'POST': UserLogin = request.form['UserLogin'] UserPass = request.form['UserPass'] + if not find_user_copy(UserLogin): + db.session.add(UserLogin, passw_hash(UserPass)) + db.session.commit() + return render_template('registration.html') - #db.session.add(UserLogin, UserPass) - #db.session.commit() - #return redirect(url_for("")) +@app.route('/content', methods=['GET']) +def content(): + return render_template('content.html') - print(UserLogin, ' ', UserPass) - # return страница с контентом - return render_template("registration.html") +@app.errorhandler(404) +def not_found(error): + return render_template('404.html'), 404 +def find_user_copy(user_login): + con = sqlite3.connect('data.db') + with con: + cur = con.cursor() + exist = cur.execute('SELECT EXISTS ( SELECT UserLogin FROM Users Where UserLogin = ' + user_login + ' LIMIT 1') + return exist + + +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 + # @app.route('/login', methods=['GET']) # def login(): # return render_template('login.html') -if __name__ == "__main__": +if __name__ == '__main__': app.run() 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 @@
From 3ed9925fd33610833a0ad6aafa68df607b09d932 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 14:42:32 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=BD=D0=B0=20=D0=B8=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 2 -- db_routing.py | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/__init__.py b/__init__.py index 70a5a77..2b53970 100644 --- a/__init__.py +++ b/__init__.py @@ -22,8 +22,6 @@ def register(): userLogin = request.form['UserLogin'] userPassw = request.form['UserPassw'] db_routing.add_user(userLogin, passw_hash(userPassw)) - else: - print('Логин занят') return render_template('registration.html') diff --git a/db_routing.py b/db_routing.py index 3465184..0a6a560 100644 --- a/db_routing.py +++ b/db_routing.py @@ -19,6 +19,8 @@ def add_user(user_name, passw_hash): new_user = User(UserName=user_name, UserPassw=passw_hash) db.session.add(new_user) db.session.commit() + else: + print('Логин занят') def find_user(user_name): From c2bbcf13c077171b1f416e2fb867b8d7deb2a3f8 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 14:50:33 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From b8c734530b0309ca97723ba6e062058c2f02cb7d Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 15:55:58 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 20 ++++++++++++-------- db_routing.py | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/__init__.py b/__init__.py index 2b53970..a0eb807 100644 --- a/__init__.py +++ b/__init__.py @@ -1,11 +1,14 @@ -from flask import render_template, request -from db_routing import app +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') +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' + + if not os.path.exists('./data.db'): db_routing.db.create_all() auth = HTTPBasicAuth() @@ -21,16 +24,17 @@ def register(): if request.method == 'POST': userLogin = request.form['UserLogin'] userPassw = request.form['UserPassw'] - db_routing.add_user(userLogin, passw_hash(userPassw)) + if db_routing.add_user(userLogin, passw_hash(userPassw)): + return render_template('content.html') return render_template('registration.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]: + 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 @@ -56,4 +60,4 @@ def passw_hash(user_passw, salt=os.urandom(32)): if __name__ == '__main__': - db_routing.app.run() + app.run() diff --git a/db_routing.py b/db_routing.py index 0a6a560..7862aab 100644 --- a/db_routing.py +++ b/db_routing.py @@ -1,9 +1,7 @@ -from flask import Flask from flask_sqlalchemy import SQLAlchemy +from __init__ import app -app = Flask(__name__, static_folder='static', template_folder='templates') -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' db = SQLAlchemy(app) @@ -19,8 +17,10 @@ def add_user(user_name, passw_hash): 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): From 45cf0ca0eec827b7dee17e0466105f630654d6c0 Mon Sep 17 00:00:00 2001 From: burzuf Date: Sun, 15 Mar 2020 15:57:13 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/__init__.py b/__init__.py index a0eb807..2bc514c 100644 --- a/__init__.py +++ b/__init__.py @@ -5,12 +5,11 @@ import os import hashlib -app = Flask(__name__, static_folder='static', template_folder='templates') -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' - - 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' auth = HTTPBasicAuth()