def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        # Successful POST, i.e. the username and password have passed validation checking.
        # Use the service layer to lookup the user.
        try:
            user = services.get_user(form.username.data, repo.repo_instance)

            # Authenticate user.
            services.authenticate_user(user['username'], form.password.data, repo.repo_instance)

            # Initialise session and redirect the user to the home page.
            session.clear()
            session['username'] = user['username']
            return redirect(url_for('movies_bp.login_home'))

        except services.UnknownUserException:
            # Username not known to the system, set a suitable error message.
            username_not_recognised = 'Username not recognised - please supply another'

        except services.AuthenticationException:
            # Authentication failed, set a suitable error message.
            password_does_not_match_username = '******'

    # For a GET or a failed POST, return the Login Web page.
    return render_template(
        'First/authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form=form
    )
예제 #2
0
def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        try:
            user = services.get_user(form.username.data, repo.repo_instance)

            # authenticate user
            services.authenticate_user(user['username'], form.password.data, repo.repo_instance)

            # initialise session and redirect the user to the home page
            session.clear()
            session['username'] = user['username']
            return redirect(url_for('home_bp.home'))

        except services.UnknownUserException:
            # username not known
            username_not_recognised = 'Username not recognised - please supply another'

        except services.AuthenticationException:
            # authentication failed
            password_does_not_match_username = '******'

    # for a GET or a failed POST, return the Login Web page.
    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form=form,
        selected_movies=utilities.get_selected_movies(),
        genre_urls=utilities.get_genres_and_urls()
    )
예제 #3
0
def test_can_add_user():
    mem_repo = MemoryRepository()
    new_user_name = "kelly007"
    new_password = "******"
    auth_services.add_user(new_user_name, new_password, mem_repo)
    user_as_dict = auth_services.get_user(new_user_name, mem_repo)
    assert user_as_dict["username"] == new_user_name

    # check that password has been encrypted.
    assert user_as_dict["password"].startswith("pbkdf2:sha256:")
예제 #4
0
def test_can_add_user(in_memory_repo):
    new_username = '******'
    new_password = '******'

    auth_services.add_user(new_username, new_password, in_memory_repo)

    user_as_dict = auth_services.get_user(new_username, in_memory_repo)
    assert user_as_dict['username'] == new_username

    # Check that password has been encrypted.
    assert user_as_dict['password'].startswith('pbkdf2:sha256:')
def login():
    form = LoginForm()
    username_not_recognised = None
    password_does_not_match_username = None

    if form.validate_on_submit():
        # Successful POST, i.e. the username and password have passed validation checking.
        # Use the service layer to lookup the user.
        try:
            user = services.get_user(form.username.data, repo.repo_instance)

            # Authenticate user.
            services.authenticate_user(user['username'], form.password.data,
                                       repo.repo_instance)

            # Initialise session and redirect the user to the home page.
            session.clear()
            session['username'] = user['username']
            return redirect(url_for('movies_bp.suggest_movie'))

        except services.UnknownUserException:
            # Username not known to the system, set a suitable error message.
            username_not_recognised = 'Username not recognised - please supply another'

        except services.AuthenticationException:
            # Authentication failed, set a suitable error message.
            password_does_not_match_username = '******'

    return render_template(
        'authentication/credentials.html',
        title='Login',
        username_error_message=username_not_recognised,
        password_error_message=password_does_not_match_username,
        form_login=form,
        title_form=SearchByTitleForm(),
        form=SearchForm(),
        handler_url=url_for('movies_bp.search'),
        handler_url_title=url_for('movies_bp.search_by_title'),
        selected_movies=utilities.get_selected_movies(),
        genre_urls=utilities.get_genres_and_urls())
예제 #6
0
def login():
    form = LoginForm()
    password_message = user_name_message = None
    if form.validate_on_submit():
        try:
            user = services.get_user(form.username.data,
                                     repo.repository_instance)
            if services.authenticate_credentials(user['username'],
                                                 form.password.data,
                                                 repo.repository_instance):
                session.clear()
                session['username'] = user['username']
                return redirect(url_for('home_bp.index'))
            else:
                password_message = 'Password does not match'
        except services.UnknownUserError:
            user_name_message = 'Username not found'
    return render_template('credentials.html',
                           title='Login',
                           username_error_message=user_name_message,
                           password_error_message=password_message,
                           form=form)