Commit 82313885 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'if-293668-fix_forti_authenticator_access_token' into 'master'

FortiAuthenticator strategy to rely on the correct config

See merge request gitlab-org/gitlab!49943
parents 54cc5473 d727491e
...@@ -18,6 +18,9 @@ module Gitlab ...@@ -18,6 +18,9 @@ module Gitlab
# Successful authentication results in HTTP 200: OK # Successful authentication results in HTTP 200: OK
# https://docs.fortinet.com/document/fortiauthenticator/6.2.0/rest-api-solution-guide/704555/authentication-auth # https://docs.fortinet.com/document/fortiauthenticator/6.2.0/rest-api-solution-guide/704555/authentication-auth
response.ok? ? success : error_from_response(response) response.ok? ? success : error_from_response(response)
rescue StandardError => ex
Gitlab::AppLogger.error(ex)
error(ex.message)
end end
private private
...@@ -32,7 +35,7 @@ module Gitlab ...@@ -32,7 +35,7 @@ module Gitlab
def api_credentials def api_credentials
{ username: ::Gitlab.config.forti_authenticator.username, { username: ::Gitlab.config.forti_authenticator.username,
password: ::Gitlab.config.forti_authenticator.token } password: ::Gitlab.config.forti_authenticator.access_token }
end end
end end
end end
......
...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do ...@@ -12,6 +12,7 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do
let(:api_token) { 's3cr3t' } let(:api_token) { 's3cr3t' }
let(:forti_authenticator_auth_url) { "https://#{host}:#{port}/api/v1/auth/" } let(:forti_authenticator_auth_url) { "https://#{host}:#{port}/api/v1/auth/" }
let(:response_status) { 200 }
subject(:validate) { described_class.new(user).validate(otp_code) } subject(:validate) { described_class.new(user).validate(otp_code) }
...@@ -23,20 +24,20 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do ...@@ -23,20 +24,20 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do
host: host, host: host,
port: port, port: port,
username: api_username, username: api_username,
token: api_token access_token: api_token
) )
request_body = { username: user.username, request_body = { username: user.username,
token_code: otp_code } token_code: otp_code }
stub_request(:post, forti_authenticator_auth_url) stub_request(:post, forti_authenticator_auth_url)
.with(body: JSON(request_body), headers: { 'Content-Type' => 'application/json' }) .with(body: JSON(request_body),
.to_return(status: response_status, body: '', headers: {}) headers: { 'Content-Type': 'application/json' },
basic_auth: [api_username, api_token])
.to_return(status: response_status, body: '')
end end
context 'successful validation' do context 'successful validation' do
let(:response_status) { 200 }
it 'returns success' do it 'returns success' do
expect(validate[:status]).to eq(:success) expect(validate[:status]).to eq(:success)
end end
...@@ -50,6 +51,16 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do ...@@ -50,6 +51,16 @@ RSpec.describe Gitlab::Auth::Otp::Strategies::FortiAuthenticator do
end end
end end
context 'unexpected error' do
it 'returns error' do
error_message = 'boom!'
stub_request(:post, forti_authenticator_auth_url).to_raise(StandardError.new(error_message))
expect(validate[:status]).to eq(:error)
expect(validate[:message]).to eq(error_message)
end
end
def stub_forti_authenticator_config(forti_authenticator_settings) def stub_forti_authenticator_config(forti_authenticator_settings)
allow(::Gitlab.config.forti_authenticator).to(receive_messages(forti_authenticator_settings)) allow(::Gitlab.config.forti_authenticator).to(receive_messages(forti_authenticator_settings))
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