2012-09-12 02:23:16 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:36 -05:00
|
|
|
describe Gitlab::Auth, lib: true do
|
2016-04-29 12:58:55 -04:00
|
|
|
let(:gl_auth) { described_class }
|
2012-09-12 02:23:16 -04:00
|
|
|
|
2016-06-13 09:38:25 -04:00
|
|
|
describe 'find_for_git_client' do
|
2016-09-15 05:57:09 -04:00
|
|
|
context 'build token' do
|
|
|
|
subject { gl_auth.find_for_git_client('gitlab-ci-token', build.token, project: project, ip: 'ip') }
|
|
|
|
|
|
|
|
context 'for running build' do
|
|
|
|
let!(:build) { create(:ci_build, :running) }
|
|
|
|
let(:project) { build.project }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with('ip', success: true, login: 'gitlab-ci-token')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognises user-less build' do
|
2016-09-16 03:59:10 -04:00
|
|
|
expect(subject).to eq(Gitlab::Auth::Result.new(nil, build.project, :ci, build_authentication_abilities))
|
2016-09-15 05:57:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognises user token' do
|
|
|
|
build.update(user: create(:user))
|
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
expect(subject).to eq(Gitlab::Auth::Result.new(build.user, build.project, :build, build_authentication_abilities))
|
2016-09-15 05:57:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 07:47:54 -04:00
|
|
|
(HasStatus::AVAILABLE_STATUSES - ['running']).each do |build_status|
|
2016-09-16 05:06:31 -04:00
|
|
|
context "for #{build_status} build" do
|
|
|
|
let!(:build) { create(:ci_build, status: build_status) }
|
|
|
|
let(:project) { build.project }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with('ip', success: false, login: 'gitlab-ci-token')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'denies authentication' do
|
2016-09-16 07:47:54 -04:00
|
|
|
expect(subject).to eq(Gitlab::Auth::Result.new)
|
2016-09-16 05:06:31 -04:00
|
|
|
end
|
2016-09-15 05:57:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognizes other ci services' do
|
2016-04-29 12:58:55 -04:00
|
|
|
project = create(:empty_project)
|
2016-09-15 05:57:09 -04:00
|
|
|
project.create_drone_ci_service(active: true)
|
|
|
|
project.drone_ci_service.update(token: 'token')
|
2016-08-01 18:31:21 -04:00
|
|
|
|
2016-04-29 12:58:55 -04:00
|
|
|
ip = 'ip'
|
|
|
|
|
2016-09-15 05:57:09 -04:00
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'drone-ci-token')
|
2016-09-16 03:59:10 -04:00
|
|
|
expect(gl_auth.find_for_git_client('drone-ci-token', 'token', project: project, ip: ip)).to eq(Gitlab::Auth::Result.new(nil, project, :ci, build_authentication_abilities))
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognizes master passwords' do
|
|
|
|
user = create(:user, password: 'password')
|
|
|
|
ip = 'ip'
|
|
|
|
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
|
2016-09-16 03:59:10 -04:00
|
|
|
expect(gl_auth.find_for_git_client(user.username, 'password', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, nil, :gitlab_or_ldap, full_authentication_abilities))
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
|
2016-09-19 10:34:32 -04:00
|
|
|
it 'recognizes user lfs tokens' do
|
|
|
|
user = create(:user)
|
|
|
|
ip = 'ip'
|
2016-09-28 12:02:31 -04:00
|
|
|
token = Gitlab::LfsToken.new(user).token
|
2016-09-19 10:34:32 -04:00
|
|
|
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: user.username)
|
2016-09-19 14:22:10 -04:00
|
|
|
expect(gl_auth.find_for_git_client(user.username, token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, nil, :lfs_token, full_authentication_abilities))
|
2016-09-19 10:34:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recognizes deploy key lfs tokens' do
|
|
|
|
key = create(:deploy_key)
|
|
|
|
ip = 'ip'
|
2016-09-28 12:02:31 -04:00
|
|
|
token = Gitlab::LfsToken.new(key).token
|
2016-09-19 10:34:32 -04:00
|
|
|
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: "lfs+deploy-key-#{key.id}")
|
|
|
|
expect(gl_auth.find_for_git_client("lfs+deploy-key-#{key.id}", token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(key, nil, :lfs_deploy_token, read_authentication_abilities))
|
|
|
|
end
|
|
|
|
|
2016-04-29 12:58:55 -04:00
|
|
|
it 'recognizes OAuth tokens' do
|
|
|
|
user = create(:user)
|
|
|
|
application = Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user)
|
|
|
|
token = Doorkeeper::AccessToken.create!(application_id: application.id, resource_owner_id: user.id)
|
|
|
|
ip = 'ip'
|
|
|
|
|
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: true, login: 'oauth2')
|
2016-09-16 03:59:10 -04:00
|
|
|
expect(gl_auth.find_for_git_client("oauth2", token.token, project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new(user, nil, :oauth, read_authentication_abilities))
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns double nil for invalid credentials' do
|
|
|
|
login = 'foo'
|
|
|
|
ip = 'ip'
|
|
|
|
|
2016-09-15 16:17:12 -04:00
|
|
|
expect(gl_auth).to receive(:rate_limit!).with(ip, success: false, login: login)
|
2016-06-13 09:38:25 -04:00
|
|
|
expect(gl_auth.find_for_git_client(login, 'bar', project: nil, ip: ip)).to eq(Gitlab::Auth::Result.new)
|
2016-04-29 12:58:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-10 08:51:16 -04:00
|
|
|
describe 'find_with_user_password' do
|
2014-09-08 07:11:39 -04:00
|
|
|
let!(:user) do
|
|
|
|
create(:user,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password)
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|
2014-10-22 11:29:26 -04:00
|
|
|
let(:username) { 'John' } # username isn't lowercase, test this
|
2014-09-08 07:11:39 -04:00
|
|
|
let(:password) { 'my-secret' }
|
2012-09-12 02:23:16 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "finds user by valid login/password" do
|
2016-06-10 08:51:16 -04:00
|
|
|
expect( gl_auth.find_with_user_password(username, password) ).to eql user
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'finds user by valid email/password with case-insensitive email' do
|
2016-06-10 08:51:16 -04:00
|
|
|
expect(gl_auth.find_with_user_password(user.email.upcase, password)).to eql user
|
2014-10-22 11:29:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'finds user by valid username/password with case-insensitive username' do
|
2016-06-10 08:51:16 -04:00
|
|
|
expect(gl_auth.find_with_user_password(username.upcase, password)).to eql user
|
2014-10-22 11:29:26 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not find user with invalid password" do
|
2014-09-08 07:11:39 -04:00
|
|
|
password = 'wrong'
|
2016-06-10 08:51:16 -04:00
|
|
|
expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not find user with invalid login" do
|
2014-09-08 07:11:39 -04:00
|
|
|
user = 'wrong'
|
2016-06-10 08:51:16 -04:00
|
|
|
expect( gl_auth.find_with_user_password(username, password) ).not_to eql user
|
2013-03-11 02:44:45 -04:00
|
|
|
end
|
2014-09-08 07:26:25 -04:00
|
|
|
|
|
|
|
context "with ldap enabled" do
|
2015-05-21 17:49:06 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
|
|
|
|
end
|
2014-09-08 07:26:25 -04:00
|
|
|
|
|
|
|
it "tries to autheticate with db before ldap" do
|
2014-10-13 11:33:44 -04:00
|
|
|
expect(Gitlab::LDAP::Authentication).not_to receive(:login)
|
2014-09-08 07:26:25 -04:00
|
|
|
|
2016-06-10 08:51:16 -04:00
|
|
|
gl_auth.find_with_user_password(username, password)
|
2014-09-08 07:26:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses ldap as fallback to for authentication" do
|
2014-10-13 11:33:44 -04:00
|
|
|
expect(Gitlab::LDAP::Authentication).to receive(:login)
|
2014-09-08 07:26:25 -04:00
|
|
|
|
2016-06-10 08:51:16 -04:00
|
|
|
gl_auth.find_with_user_password('ldap_user', 'password')
|
2014-09-08 07:26:25 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|
2016-09-15 05:57:09 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
def build_authentication_abilities
|
2016-09-15 05:57:09 -04:00
|
|
|
[
|
|
|
|
:read_project,
|
|
|
|
:build_download_code,
|
|
|
|
:build_read_container_image,
|
|
|
|
:build_create_container_image
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
def read_authentication_abilities
|
2016-09-15 05:57:09 -04:00
|
|
|
[
|
|
|
|
:read_project,
|
|
|
|
:download_code,
|
|
|
|
:read_container_image
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
def full_authentication_abilities
|
|
|
|
read_authentication_abilities + [
|
2016-09-15 05:57:09 -04:00
|
|
|
:push_code,
|
2016-09-19 07:29:48 -04:00
|
|
|
:create_container_image
|
2016-09-15 05:57:09 -04:00
|
|
|
]
|
|
|
|
end
|
2012-09-12 02:23:16 -04:00
|
|
|
end
|