2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-14 20:45:48 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe JwtController do
|
2016-05-15 01:33:06 -04:00
|
|
|
let(:service) { double(execute: {}) }
|
|
|
|
let(:service_class) { double(new: service) }
|
|
|
|
let(:service_name) { 'test' }
|
|
|
|
let(:parameters) { { service: service_name } }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
stub_const('JwtController::SERVICES', service_name => service_class)
|
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
|
|
|
|
context 'existing service' do
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: parameters }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(200) }
|
2016-05-15 01:33:06 -04:00
|
|
|
|
|
|
|
context 'returning custom http code' do
|
|
|
|
let(:service) { double(execute: { http_status: 505 }) }
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(505) }
|
2016-05-15 01:33:06 -04:00
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 13:37:15 -05:00
|
|
|
context 'when using authenticated request' do
|
2016-05-14 20:45:48 -04:00
|
|
|
context 'using CI token' do
|
2016-09-15 07:49:11 -04:00
|
|
|
let(:build) { create(:ci_build, :running) }
|
|
|
|
let(:project) { build.project }
|
|
|
|
let(:headers) { { authorization: credentials('gitlab-ci-token', build.token) } }
|
2016-05-15 01:33:06 -04:00
|
|
|
|
2016-05-14 20:45:48 -04:00
|
|
|
context 'project with enabled CI' do
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: parameters, headers: headers }
|
2016-09-15 07:49:11 -04:00
|
|
|
|
2018-12-16 07:01:11 -05:00
|
|
|
it { expect(service_class).to have_received(:new).with(project, nil, ActionController::Parameters.new(parameters).permit!) }
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'project with disabled CI' do
|
2016-08-01 18:31:21 -04:00
|
|
|
before do
|
|
|
|
project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
|
|
|
|
end
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: parameters, headers: headers }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(401) }
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
2017-05-31 09:55:12 -04:00
|
|
|
|
|
|
|
context 'using personal access tokens' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:pat) { create(:personal_access_token, user: user, scopes: ['read_registry']) }
|
|
|
|
let(:headers) { { authorization: credentials('personal_access_token', pat.token) } }
|
|
|
|
|
2017-08-04 09:17:20 -04:00
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
end
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: parameters, headers: headers }
|
2017-05-31 09:55:12 -04:00
|
|
|
|
|
|
|
it 'authenticates correctly' do
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2018-12-16 07:01:11 -05:00
|
|
|
expect(service_class).to have_received(:new).with(nil, user, ActionController::Parameters.new(parameters).permit!)
|
2017-05-31 09:55:12 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'using User login' do
|
|
|
|
let(:user) { create(:user) }
|
2016-09-15 17:27:01 -04:00
|
|
|
let(:headers) { { authorization: credentials(user.username, user.password) } }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: parameters, headers: headers }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2018-12-16 07:01:11 -05:00
|
|
|
it { expect(service_class).to have_received(:new).with(nil, user, ActionController::Parameters.new(parameters).permit!) }
|
2016-09-15 16:17:12 -04:00
|
|
|
|
2018-07-13 13:45:07 -04:00
|
|
|
context 'when passing a flat array of scopes' do
|
|
|
|
# We use this trick to make rails to generate a query_string:
|
|
|
|
# scope=scope1&scope=scope2
|
|
|
|
# It works because :scope and 'scope' are the same as string, but different objects
|
|
|
|
let(:parameters) do
|
|
|
|
{
|
|
|
|
:service => service_name,
|
|
|
|
:scope => 'scope1',
|
|
|
|
'scope' => 'scope2'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service_parameters) do
|
2018-12-16 07:01:11 -05:00
|
|
|
ActionController::Parameters.new({ service: service_name, scopes: %w(scope1 scope2) }).permit!
|
2018-07-13 13:45:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(service_class).to have_received(:new).with(nil, user, service_parameters) }
|
|
|
|
end
|
|
|
|
|
2016-09-15 16:17:12 -04:00
|
|
|
context 'when user has 2FA enabled' do
|
|
|
|
let(:user) { create(:user, :two_factor) }
|
|
|
|
|
|
|
|
context 'without personal token' do
|
|
|
|
it 'rejects the authorization attempt' do
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-06-07 15:49:45 -04:00
|
|
|
expect(response.body).to include('You must use a personal access token with \'api\' scope for Git over HTTP')
|
2016-09-15 16:17:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with personal token' do
|
|
|
|
let(:access_token) { create(:personal_access_token, user: user) }
|
|
|
|
let(:headers) { { authorization: credentials(user.username, access_token.token) } }
|
|
|
|
|
2016-11-08 13:37:15 -05:00
|
|
|
it 'accepts the authorization attempt' do
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-09-15 16:17:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-20 09:30:26 -04:00
|
|
|
|
|
|
|
it 'does not cause session based checks to be activated' do
|
|
|
|
expect(Gitlab::Session).not_to receive(:with_session)
|
|
|
|
|
|
|
|
get '/jwt/auth', params: parameters, headers: headers
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'using invalid login' do
|
2016-05-15 01:33:06 -04:00
|
|
|
let(:headers) { { authorization: credentials('invalid', 'password') } }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-06-07 15:49:45 -04:00
|
|
|
context 'when internal auth is enabled' do
|
|
|
|
it 'rejects the authorization attempt' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get '/jwt/auth', params: parameters, headers: headers
|
2017-06-07 15:49:45 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-06-07 15:49:45 -04:00
|
|
|
expect(response.body).not_to include('You must use a personal access token with \'api\' scope for Git over HTTP')
|
|
|
|
end
|
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-06-07 15:49:45 -04:00
|
|
|
context 'when internal auth is disabled' do
|
|
|
|
it 'rejects the authorization attempt with personal access token message' do
|
2019-12-16 07:07:43 -05:00
|
|
|
allow_next_instance_of(ApplicationSetting) do |instance|
|
|
|
|
allow(instance).to receive(:password_authentication_enabled_for_git?) { false }
|
|
|
|
end
|
2018-12-17 17:52:17 -05:00
|
|
|
get '/jwt/auth', params: parameters, headers: headers
|
2017-06-07 15:49:45 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-06-07 15:49:45 -04:00
|
|
|
expect(response.body).to include('You must use a personal access token with \'api\' scope for Git over HTTP')
|
|
|
|
end
|
|
|
|
end
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 13:37:15 -05:00
|
|
|
context 'when using unauthenticated request' do
|
|
|
|
it 'accepts the authorization attempt' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get '/jwt/auth', params: parameters
|
2016-11-08 13:37:15 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-11-08 13:37:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows read access' do
|
2019-04-15 09:05:55 -04:00
|
|
|
expect(service).to receive(:execute).with(authentication_abilities: Gitlab::Auth.read_only_authentication_abilities)
|
2016-11-08 13:37:15 -05:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get '/jwt/auth', params: parameters
|
2016-11-08 13:37:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-14 20:45:48 -04:00
|
|
|
context 'unknown service' do
|
2018-12-17 17:52:17 -05:00
|
|
|
subject! { get '/jwt/auth', params: { service: 'unknown' } }
|
2016-05-14 20:45:48 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
it { expect(response).to have_gitlab_http_status(404) }
|
2016-05-14 20:45:48 -04:00
|
|
|
end
|
|
|
|
|
2016-05-15 01:33:06 -04:00
|
|
|
def credentials(login, password)
|
2016-05-14 20:45:48 -04:00
|
|
|
ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
|
|
|
|
end
|
|
|
|
end
|