Skip to content
Snippets Groups Projects
Commit 5ee6f353 authored by danielmyren's avatar danielmyren
Browse files

Utilized Flask Security for validating the username

parent ab1ac3a6
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@ from flask import Blueprint
from flask import send_from_directory, render_template, jsonify
from flask import current_app, request
from flask_security import hash_password
from email_validator import validate_email, EmailNotValidError
from models_shared import db, User
from sqlalchemy import select
......@@ -35,21 +34,11 @@ def register():
_validated_username = None,
_validated_password = None
"""
# Validate email
try:
email_info = validate_email(data["email"], check_deliverability=False, test_environment=current_app.debug) # TODO DEPLOYMENT: Investigate check_deliverability and make sure test_environment (use @test) is set to False.
_validated_email = email_info.normalized # Use the normalized version of the string
"""
try:
# Validate the given email. If valid, the normalized version is returned.
_validated_email = current_app.security._mail_util.validate(data["email"])
except ValueError as e:
# TODO: What do we return to the user if not valid? Check the other stuff and return a response with everything that is wrong?
print("Invalid Email")
pass
......@@ -57,8 +46,11 @@ def register():
regex__name_pattern = "^[a-zA-Z0-9]{3,20}"
match_name = re.fullmatch(regex__name_pattern, data["username"]) # TODO DEPLOYMENT: Investigate normalization
if match_name:
_validated_username = match_name.string
ubad, unorm = current_app.security._username_util.validate(data["username"])
if ubad is None:
_validated_username = match_name.string
else:
pass
# Validate password
regex_passw_pattern = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9\w\W]{4,}$" # \W One character that is not a word character as defined by your engine's \w
......@@ -68,23 +60,13 @@ def register():
print(f"pbad={pbad} ---- pnorm={pnorm}")
if pbad is None:
_validated_password = pnorm
else:
data = {"info": "faults"}
if _validated_email is None:
data["fault_email"] = True
if _validated_username is None:
data["fault_username"] = True
if _validated_password is None:
data["fault_password"] = pbad
return jsonify(data)
if _validated_email and _validated_username and _validated_password:
# Create the user if everything is valid.
check_existing_email = current_app.security.datastore.find_user(email=_validated_email)
check_existing_username = current_app.security.datastore.find_user(username=_validated_username)
print(check_existing_email)
print(check_existing_username)
if check_existing_email is None and check_existing_username is None:
current_app.security.datastore.create_user(
......@@ -97,13 +79,24 @@ def register():
# TODO REDIRECT?
else:
# User already exists
data = {"info": "reg-faults"}
if check_existing_email is None:
if check_existing_email is not None:
data["fault_ext_email"] = True
if check_existing_username is None:
if check_existing_username is not None:
data["fault_ext_username"] = True
return jsonify(data)
else:
# Faults with the supplied information from the user
data = {"info": "faults"}
if _validated_email is None:
data["fault_email"] = True
if _validated_username is None:
data["fault_username"] = True
if _validated_password is None:
data["fault_password"] = pbad
return jsonify(data)
a=current_app.security.datastore.find_user(username=_validated_username)
print(a)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment