mirror of
https://github.com/Llloooggg/Jarvis.git
synced 2026-03-06 03:56:23 +03:00
Переработана и протестирована регистрация
This commit is contained in:
57
__init__.py
57
__init__.py
@@ -1,13 +1,14 @@
|
|||||||
from flask import Flask, render_template, request
|
from flask import render_template, request
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from db_routing import app
|
||||||
from flask_simplelogin import SimpleLogin
|
import db_routing
|
||||||
|
from flask_httpauth import HTTPBasicAuth
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
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 = SQLAlchemy(app)
|
db_routing.db.create_all()
|
||||||
|
auth = HTTPBasicAuth()
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
@@ -18,35 +19,37 @@ def index():
|
|||||||
@app.route('/registration', methods=['GET', 'POST'])
|
@app.route('/registration', methods=['GET', 'POST'])
|
||||||
def register():
|
def register():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
UserLogin = request.form['UserLogin']
|
userLogin = request.form['UserLogin']
|
||||||
UserPass = request.form['UserPass']
|
userPassw = request.form['UserPassw']
|
||||||
if not find_user_copy(UserLogin):
|
db_routing.add_user(userLogin, passw_hash(userPassw))
|
||||||
db.session.add(UserLogin, passw_hash(UserPass))
|
else:
|
||||||
db.session.commit()
|
print('Логин занят')
|
||||||
# return render_template('your_page.html')
|
|
||||||
return render_template('registration.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]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@app.route('/content', methods=['GET'])
|
@app.route('/content', methods=['GET'])
|
||||||
|
@auth.login_required
|
||||||
def content():
|
def content():
|
||||||
return render_template('content.html')
|
return render_template('content.html')
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
# @app.errorhandler(404)
|
||||||
def not_found(error):
|
# def not_found(error):
|
||||||
return render_template('404.html'), 404
|
# return render_template('404.html'), 404
|
||||||
|
|
||||||
|
|
||||||
def find_user_copy(user_login):
|
def passw_hash(user_passw, salt=os.urandom(32)):
|
||||||
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)
|
key = hashlib.pbkdf2_hmac('sha256', user_passw.encode('utf-8'), salt, 100000)
|
||||||
storage = salt + key
|
storage = salt + key
|
||||||
# salt_from_storage = storage[:32] # 32 длина соли
|
# salt_from_storage = storage[:32] # 32 длина соли
|
||||||
@@ -55,4 +58,4 @@ def passw_hash(user_passw):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
db_routing.app.run()
|
||||||
|
|||||||
25
db_routing.py
Normal file
25
db_routing.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
def find_user(user_name):
|
||||||
|
return User.query.filter_by(UserName=user_name).first()
|
||||||
@@ -72,7 +72,7 @@
|
|||||||
<div class="form__field-title" >
|
<div class="form__field-title" >
|
||||||
Введите пароль
|
Введите пароль
|
||||||
</div>
|
</div>
|
||||||
<input type="text" class="form__input-text" placeholder="Введите пароль" name = "UserPass">
|
<input type="text" class="form__input-text" placeholder="Введите пароль" name = "UserPassw">
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form__btns">
|
<div class="form__btns">
|
||||||
|
|||||||
Reference in New Issue
Block a user