Commit 161a05b9 authored by Tiago Botelho's avatar Tiago Botelho

Writes specs

parent f7420102
...@@ -21,9 +21,11 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController ...@@ -21,9 +21,11 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
# Extend the standard implementation to also increment # Extend the standard implementation to also increment
# the number of failed sign in attempts # the number of failed sign in attempts
def failure def failure
user = User.find_by_username(params[:username]) if params[:username].present? && AuthHelper.form_based_provider?(failed_strategy.name)
user = User.by_login(params[:username])
user&.increment_failed_attempts! user&.increment_failed_attempts!
end
super super
end end
......
---
title: Limit the number of failed logins when using LDAP for authentication
merge_request: 43525
author:
type: added
...@@ -10,6 +10,41 @@ describe OmniauthCallbacksController do ...@@ -10,6 +10,41 @@ describe OmniauthCallbacksController do
stub_omniauth_provider(provider, context: request) stub_omniauth_provider(provider, context: request)
end end
context 'when the user is on the last sign in attempt' do
let(:extern_uid) { 'my-uid' }
before do
user.update(failed_attempts: User.maximum_attempts.pred)
subject.response = ActionDispatch::Response.new
end
context 'when using a form based provider' do
let(:provider) { :ldap }
it 'locks the user when sign in fails' do
allow(subject).to receive(:params).and_return(ActionController::Parameters.new(username: user.username))
request.env['omniauth.error.strategy'] = OmniAuth::Strategies::LDAP.new(nil)
subject.send(:failure)
expect(user.reload).to be_access_locked
end
end
context 'when using a button based provider' do
let(:provider) { :github }
it 'does not lock the user when sign in fails' do
request.env['omniauth.error.strategy'] = OmniAuth::Strategies::GitHub.new(nil)
subject.send(:failure)
expect(user.reload).not_to be_access_locked
end
end
end
context 'strategies' do
context 'github' do context 'github' do
let(:extern_uid) { 'my-uid' } let(:extern_uid) { 'my-uid' }
let(:provider) { :github } let(:provider) { :github }
...@@ -89,4 +124,5 @@ describe OmniauthCallbacksController do ...@@ -89,4 +124,5 @@ describe OmniauthCallbacksController do
expect(controller).to set_flash[:alert].to('Wrong extern UID provided. Make sure Auth0 is configured correctly.') expect(controller).to set_flash[:alert].to('Wrong extern UID provided. Make sure Auth0 is configured correctly.')
end end
end end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment