def register_new_user(self, request):
        register_form = request.json if request.is_json else request.form
        register_dict = dict(register_form)
        form_register_fields = ["firstName", 'lastName', 'email', 'password', 'sectorId']
        form_valid_res = form_is_full(register_dict, form_register_fields)

        if form_valid_res.isSuccess:
            firstName = register_dict["firstName"]
            lastName = register_dict['lastName']
            email = register_dict['email']
            password = register_dict['password']
            sectorId = register_dict['sectorId']

            user = User(firstName=firstName, lastName=lastName, email=email, password=password, sectorId=sectorId)

            if user_is_valid(user).isSuccess:
                if password_is_valid(user.password).isSuccess:
                    if sector_id_is_valid(user.sectorId).isSuccess:
                        password_encrypt = PasswordEncryption.hash_salt(password=password, salt=None)
                        user.password = password_encrypt
                        if DAL.save_new_user_to_db(user) and DAL.save_user_password_history_to_db(user):
                            return AuthorizationResult(isSuccess=True, Message="User created successfully. ")
                    else:
                        return sector_id_is_valid(user.sectorId)
                else:
                    return password_is_valid(user.password)
            else:
                return AuthorizationResult(isSuccess=False, Message="User created failed. ")
        else:
            return form_valid_res
Example #2
0
def user_is_valid(user):
    if valid_email(user.email):
        return AuthorizationResult(isSuccess=True,
                                   Message=Config.EMAIL_IS_VALID)
    else:
        return AuthorizationResult(isSuccess=False,
                                   Message=Config.EMAIL_IS_NOT_VALID)
Example #3
0
def form_is_full(form, field):
    form_is_not_full_res = AuthorizationResult(
        isSuccess=False, Message="All form fields must be filled in")
    for x in (True if field in form.keys() else False for field in field):
        if x == False:
            return form_is_not_full_res
    if "" in form.values():
        return form_is_not_full_res
    if '""' in form.values():
        return form_is_not_full_res
    return AuthorizationResult(isSuccess=True,
                               Message="All form fields are filled ")
Example #4
0
def start_password_recovery_process(email):
    if valid_email(email):
        user = UserProvider.get_user_from_db_by_email(email)
        if user:
            header = Config.TITLE_MSG_EMAIL_PASSWORD_RECOVERY
            body = UserTokenEncryptinoService.hash_email_with_date(email)
            EmailService.send(email=email, body=body, header=header)
            return AuthorizationResult(isSuccess=True, Message=Config.USER_NOT_FOUND)
        else:
            return AuthorizationResult(isSuccess=False, Message=Config.USER_NOT_FOUND)
    else:
        return AuthorizationResult(isSuccess=False, Message=Config.EMAIL_IS_NOT_VALID)
Example #5
0
def start_verify_password_and_token(verify_password_dict):
    email = verify_password_dict.get("email")
    entered_token = verify_password_dict.get("token")
    if valid_email(email):
        user = UserProvider.get_user_from_db_by_email(email)
        if user:
            if UserTokenEncryptinoService.verify_hash_email_with_date(email, entered_token):
                return user, AuthorizationResult(isSuccess=True, Message=Config.VERIFY_HASH_EMAIL_WITH_DATE_SUCCESS)
            else:
                return None, AuthorizationResult(isSuccess=False, Message=Config.VERIFY_HASH_EMAIL_WITH_DATE_FAILED)
        return None, AuthorizationResult(isSuccess=False, Message=Config.BAD_USER_NAME_OR_PASSWORD)
    else:
        return None, AuthorizationResult(isSuccess=False, Message=Config.EMAIL_IS_NOT_VALID)
Example #6
0
def password_is_valid(password):
    if len(password) != Config.LENGTH_OF_THE_PASSWORD:
        return AuthorizationResult(isSuccess=False,
                                   Message="Password should be exactly " +
                                   str(Config.LENGTH_OF_THE_PASSWORD) +
                                   " characters")
    for value, msg in Config.PASSWORD_VALIDATION_STRUCTURE.items():
        if re.search(value, password) is None:
            return AuthorizationResult(isSuccess=False, Message=msg)
    if Config.DICTIONARY_ATTACK:
        if dictionary_attack(password):
            return AuthorizationResult(isSuccess=False,
                                       Message="Password is not safe! ")
    return AuthorizationResult(isSuccess=True, Message="Password is ok! ")
Example #7
0
def start_login_process(email, enteredPassword):
    user = UserProvider.get_user_from_db_by_email(email)
    if user:
        if verify_user_password(user, enteredPassword):
            user.invalidLoginAttempt = 0
            return user, AuthorizationResult(isSuccess=verify_user_password(user, enteredPassword), Message="User Login!")
        else:
            #if hasattr(user, 'lockEndTime'):
            if user.lockEndTime != None and user.lockEndTime > datetime.now():
                return None, AuthorizationResult(isSuccess=False,
                                                     Message=Config.USER_IS_LOCKED_UNTIL + str(user.lockEndTime))
            else:
                user.invalidLoginAttempt = user.invalidLoginAttempt + 1
                if user.invalidLoginAttempt >= Config.LOGIN_LIMIT_TRYING:
                    user.lockEndTime = (datetime.now() + timedelta(minutes=15))  # .strftime("%B %d, %Y %I:%M%p")
                    user.invalidLoginAttempt = 0
                DAL.save_new_user_to_db(user)
    return None, AuthorizationResult(isSuccess=False, Message=Config.BAD_USER_NAME_OR_PASSWORD)
Example #8
0
def start_change_password(user_id,change_password_dict):
    enteredPassword = change_password_dict['password']
    user = UserProvider.get_user_from_db_by_id(user_id)
    occurrences = Config.HISTORY_OF_THE_PASSWORDS
    res_password_is_valid = password_is_valid(enteredPassword)
    if res_password_is_valid.isSuccess:
        print(was_password_used_in_the_last_given_occurrences(user, enteredPassword, occurrences))
        if was_password_used_in_the_last_given_occurrences(user, enteredPassword, occurrences):
            return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_WAS_USED_IN_THE_LAST_GIVEN_OCCURRENCES)
        else:
            password_encrypt = PasswordEncryption.hash_salt(password=enteredPassword, salt=None)
            user.password = password_encrypt
            if DAL.save_new_user_to_db(user) and DAL.save_user_password_history_to_db(user):
                return AuthorizationResult(isSuccess=True, Message=Config.PASSWORD_CHANGE_SUCCESS)
            else:
                return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_CHANGE_FAILED)
            # else:
            # return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_CHANGE_FAILED)
    else:
        return res_password_is_valid
Example #9
0
def sector_id_is_valid(sectorId):
    if SectorProivder.get_sector_by_id(sectorId) is None:
        return AuthorizationResult(isSuccess=False,
                                   Message="Sector is not exist")
    return AuthorizationResult(isSuccess=True, Message="Sector is exist")