async def authenticate(request, *args, **kwargs): userdata = { 'username': request.form.get('username'), 'password': request.form.get('password'), 'confirm_password': request.form.get('confirm_password'), } if request.form.get('register_enabled'): register_schema.load(userdata) db_user = await User.get_user(userdata['username']) if db_user: raise Forbidden({ 'username': '******', }) engine = await get_engine() async with engine.acquire() as conn: await conn.execute(User.insert().values( username=userdata['username'], password=hash_password(userdata['password']))) return await User.get_user(userdata['username']) else: login_schema.load(userdata) db_user = await User.get_user(userdata['username']) if not db_user: raise NotFound({ 'username': '******', }) if hash_password(userdata['password']) != db_user['password']: raise ValidationError({ 'password': '******', }) return db_user
def register_user(): if request.method == 'POST': username = request.form['username'] password = hash.hash_password(request.form['password']) registry_date = date.today() data_manager.add_user(username, password, registry_date) return redirect(url_for('list_5_questions')) return render_template('register.html')
def create_root(root_passw): if not is_users_exist(): __create_users_table() db = __mysql_connect(cfg.DB) cursor = db.cursor() cursor.execute(""" INSERT INTO `users` (`user_id`, `user_name`, `user_pass`) VALUES (default, 'root', '%s'); """ % hash.hash_password(root_passw))
def changepassword(): os.chdir("/var/www/FlaskApp/FlaskApp") conn = sqlite3.connect("database/" + "users.db") error = None username = session.get('username') if request.method == 'POST': old = hash.hash_password(request.form['oldpassword']) print(old) new = hash.hash_password(request.form['newpassword']) print(new) passcheck = list(itertools.chain.from_iterable (conn.execute("SELECT Password from Users"))) print(passcheck[0]) if old != passcheck[0]: error = 'Invalid Password. Please try again.' else: c = conn.cursor() c.execute("UPDATE Users SET Password = ? WHERE Password = ?", (new, old)) conn.commit() return redirect(url_for('profile')) return render_template('changepassword.html', username=username, error=error)
def user_in_db(name, password): user = data_manager.select_query(table='users', clause='WHERE', condition=['name', '=', name]) print(user) if not user: data_manager.insert_record('users', { 'name': name, 'password': hash.hash_password(password) }) return {"registration_legit": True} return {"registration_legit": False}
def route_registration(): if request.method == "POST": registration_data = { 'id': data_manager.get_next_id('users'), 'username': request.form.get('username'), 'password': hash.hash_password(request.form.get('password')), 'email': request.form.get('email'), 'registration_date': datetime.now().strftime("%Y-%m-%d %H:%M:%S") } data_manager.insert_into_database('users', registration_data) return redirect('/list') else: return render_template('registration.html')
def create_user(cursor, username, password): pw_hash = hash.hash_password(password) date = datetime.now() try: cursor.execute( """ INSERT INTO users (username, pw_hash, creation_date) VALUES (%s,%s,%s) """, (username, pw_hash, date) ) return True except psycopg2.IntegrityError: return False
def submit_registration(): if request.method == 'POST': new_registration = request.form.to_dict() if new_registration["password_confirm"] == new_registration[ "password"]: hashed_password = hash.hash_password(new_registration["password"]) new_registration["password"] = hashed_password data_manager.add_new_user_to_database(new_registration) #data_manager.delete_token(new_registration["token"]) return redirect(url_for('login')) else: return redirect( url_for('registration', token=new_registration['token'])) else: return redirect(url_for('login'))
def add_user(self, email, password): self.connect_db() try: with self._conn: result = self._cursor.execute( """ SELECT * FROM user WHERE email=? """, (email, )) if result.fetchone() is None: new_password = hash_password(password) self._cursor.execute( """ INSERT INTO user (email, password) values (?, ?) """, ( email, new_password, )) self._conn.commit() data = { 'status': 'ok', 'message': "success", } return data data = { 'status': None, 'message': "Esse email já existe.", } return data data = { 'status': None, 'message': "Não foi possível estabelecer uma conexão com o servidor", } return data finally: self._conn.close()
def login(): conn = sqlite3.connect("database/" + "users.db") os.chdir("/var/www/FlaskApp/FlaskApp") error = None if request.method == 'POST': u = request.form['username'] print(u) p = hash.hash_password(request.form['password']) print(p) credentials = list(itertools.chain.from_iterable (conn.execute("SELECT * from Users"))) if u != credentials[0] or p != credentials[1]: error = 'Invalid Credentials. Please try again.' else: session['logged_in'] = True session['username'] = u return redirect(url_for('index')) return render_template('login.html', error=error)
def signup(): """ Register a user :return: None """ # Sign Up UI username = input("Username: "******"r") as f: data = json.load(f) if find_user(data['users'], username) != -1: system_notification("[The name is already taken]", (255, 0, 0)) username = input("Username: "******"Password: "******"Confirm Password: "******"[Doesn't Match] Confirm Password: "******"r") as f: data = json.load(f) except (FileNotFoundError, json.JSONDecodeError): data = DEFAULT_JSON_FORMAT info = { "name": username, "password": password, "balance": DEFAULT_BALANCE, "record": [] } data['users'].append(info) sort_name(data['users']) with open(DEFAULT_DATA_FILE, "w") as f: json.dump(data, f, indent=4) system_notification(f"Welcome to the Roulette World, {username}!", (66, 245, 212))
def new_user(): name = input("Name: ") email = input("Email: ") password = input("Password: "******"Algorithm: ") date = datetime.datetime.now() # Connection to the database connection = sqlite3.connect('database.db') # Cursor to navigate and execute in the database cursor = connection.cursor() # Hashing the password and choosing the algorithm password = hash.hash_password(password, algorithm) # Adding user to the db and closing connection cursor.execute("INSERT INTO users VALUES (?,?,?,?)", (name, email, date.strftime("%Y-%m-%d"), password )) connection.commit() connection.close()
import connect from hash import hash_password exists = True while exists == True: #Get the user to enter their details un = input("Username: "******"Password: "******"Year: ")) exists = connect.check_username_exists(un) #Hash the password hashed_pw = hash_password(pw) sql = "INSERT INTO users (username, password, year) VALUES (%s, %s, %s)" val = (un, hashed_pw, yr) connect.mycursor.execute(sql, val) connect.mydb.commit() print(connect.mycursor.rowcount, "record inserted.")
def __init__(self, username, password): self.username = username self.password = hash_password(password)