debian-mirror-gitlab/lib/gitlab/auth.rb

325 lines
10 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Gitlab
module Auth
2018-03-17 18:26:18 +05:30
MissingPersonalAccessTokenError = Class.new(StandardError)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
# Scopes used for GitLab API access
API_SCOPES = [:api, :read_user].freeze
# Scopes used for GitLab Repository access
REPOSITORY_SCOPES = [:read_repository, :write_repository].freeze
# Scopes used for GitLab Docker Registry access
2017-09-10 17:25:29 +05:30
REGISTRY_SCOPES = [:read_registry].freeze
2019-07-31 22:56:46 +05:30
# Scopes used for GitLab as admin
ADMIN_SCOPES = [:sudo].freeze
2017-08-17 22:00:37 +05:30
# Scopes used for OpenID Connect
OPENID_SCOPES = [:openid].freeze
2019-03-02 22:35:43 +05:30
# OpenID Connect profile scopes
PROFILE_SCOPES = [:profile, :email].freeze
2017-08-17 22:00:37 +05:30
# Default scopes for OAuth applications that don't define their own
DEFAULT_SCOPES = [:api].freeze
class << self
2018-11-18 11:00:15 +05:30
def omniauth_enabled?
Gitlab.config.omniauth.enabled
2018-11-08 19:23:39 +05:30
end
def find_for_git_client(login, password, project:, ip:)
raise "Must provide an IP for rate limiting" if ip.nil?
2017-08-17 22:00:37 +05:30
# `user_with_password_for_git` should be the last check
# because it's the most expensive, especially when LDAP
# is enabled.
2016-09-29 09:46:39 +05:30
result =
service_request_check(login, password, project) ||
build_access_token_check(login, password) ||
2018-03-17 18:26:18 +05:30
lfs_token_check(login, password, project) ||
2017-08-17 22:00:37 +05:30
oauth_access_token_check(login, password) ||
personal_access_token_check(password) ||
2018-05-09 12:01:36 +05:30
deploy_token_check(login, password) ||
2017-09-10 17:25:29 +05:30
user_with_password_for_git(login, password) ||
2016-09-29 09:46:39 +05:30
Gitlab::Auth::Result.new
2016-09-29 09:46:39 +05:30
rate_limit!(ip, success: result.success?, login: login)
2017-08-17 22:00:37 +05:30
Gitlab::Auth::UniqueIpsLimiter.limit_user!(result.actor)
2018-03-17 18:26:18 +05:30
return result if result.success? || authenticate_using_internal_or_ldap_password?
2017-09-10 17:25:29 +05:30
# If sign-in is disabled and LDAP is not configured, recommend a
# personal access token on failed auth attempts
2018-03-17 18:26:18 +05:30
raise Gitlab::Auth::MissingPersonalAccessTokenError
end
def find_with_user_password(login, password)
2018-03-27 19:54:05 +05:30
# Avoid resource intensive checks if login credentials are not provided
return unless login.present? && password.present?
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
# Nothing to do here if internal auth is disabled and LDAP is
# not configured
return unless authenticate_using_internal_or_ldap_password?
2017-08-17 22:00:37 +05:30
Gitlab::Auth::UniqueIpsLimiter.limit_user! do
user = User.by_login(login)
2018-10-15 14:42:47 +05:30
break if user && !user.active?
2018-03-27 19:54:05 +05:30
authenticators = []
if user
authenticators << Gitlab::Auth::OAuth::Provider.authentication(user, 'database')
# Add authenticators for all identities if user is not nil
user&.identities&.each do |identity|
authenticators << Gitlab::Auth::OAuth::Provider.authentication(user, identity.provider)
end
else
# If no user is provided, try LDAP.
# LDAP users are only authenticated via LDAP
authenticators << Gitlab::Auth::LDAP::Authentication
2017-08-17 22:00:37 +05:30
end
2018-03-27 19:54:05 +05:30
authenticators.compact!
2018-04-04 21:44:52 +05:30
# return found user that was authenticated first for given login credentials
authenticators.find do |auth|
authenticated_user = auth.login(login, password)
break authenticated_user if authenticated_user
end
end
end
2019-09-30 21:07:59 +05:30
# rubocop:disable Gitlab/RailsLogger
def rate_limit!(ip, success:, login:)
rate_limiter = Gitlab::Auth::IpRateLimiter.new(ip)
return unless rate_limiter.enabled?
if success
# Repeated login 'failures' are normal behavior for some Git clients so
# it is important to reset the ban counter once the client has proven
# they are not a 'bad guy'.
rate_limiter.reset!
else
# Register a login failure so that Rack::Attack can block the next
# request from this IP if needed.
rate_limiter.register_fail!
if rate_limiter.banned?
Rails.logger.info "IP #{ip} failed to login " \
"as #{login} but has been temporarily banned from Git auth"
end
end
end
2019-09-30 21:07:59 +05:30
# rubocop:enable Gitlab/RailsLogger
private
2018-03-17 18:26:18 +05:30
def authenticate_using_internal_or_ldap_password?
2018-03-27 19:54:05 +05:30
Gitlab::CurrentSettings.password_authentication_enabled_for_git? || Gitlab::Auth::LDAP::Config.enabled?
2018-03-17 18:26:18 +05:30
end
2016-09-29 09:46:39 +05:30
def service_request_check(login, password, project)
matched_login = /(?<service>^[a-zA-Z]*-ci)-token$/.match(login)
2016-09-29 09:46:39 +05:30
return unless project && matched_login.present?
underscored_service = matched_login['service'].underscore
2016-09-29 09:46:39 +05:30
if Service.available_services_names.include?(underscored_service)
# We treat underscored_service as a trusted input because it is included
# in the Service.available_services_names whitelist.
2018-03-17 18:26:18 +05:30
service = project.public_send("#{underscored_service}_service") # rubocop:disable GitlabSecurity/PublicSend
2016-09-29 09:46:39 +05:30
if service && service.activated? && service.valid_token?(password)
Gitlab::Auth::Result.new(nil, project, :ci, build_authentication_abilities)
2016-09-13 17:45:13 +05:30
end
end
end
def user_with_password_for_git(login, password)
user = find_with_user_password(login, password)
2016-09-29 09:46:39 +05:30
return unless user
2018-03-17 18:26:18 +05:30
raise Gitlab::Auth::MissingPersonalAccessTokenError if user.two_factor_enabled?
2016-09-29 09:46:39 +05:30
Gitlab::Auth::Result.new(user, nil, :gitlab_or_ldap, full_authentication_abilities)
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def oauth_access_token_check(login, password)
if login == "oauth2" && password.present?
token = Doorkeeper::AccessToken.by_token(password)
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
if valid_oauth_token?(token)
2016-09-13 17:45:13 +05:30
user = User.find_by(id: token.resource_owner_id)
2017-08-17 22:00:37 +05:30
Gitlab::Auth::Result.new(user, nil, :oauth, full_authentication_abilities)
2016-09-13 17:45:13 +05:30
end
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
def personal_access_token_check(password)
return unless password.present?
2018-11-18 11:00:15 +05:30
token = PersonalAccessTokensFinder.new(state: 'active').find_by_token(password)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
if token && valid_scoped_token?(token, all_available_scopes)
2018-03-17 18:26:18 +05:30
Gitlab::Auth::Result.new(token.user, nil, :personal_access_token, abilities_for_scopes(token.scopes))
2016-09-29 09:46:39 +05:30
end
end
2017-08-17 22:00:37 +05:30
def valid_oauth_token?(token)
2017-09-10 17:25:29 +05:30
token && token.accessible? && valid_scoped_token?(token, [:api])
end
def valid_scoped_token?(token, scopes)
AccessTokenValidationService.new(token).include_any_scope?(scopes)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def abilities_for_scopes(scopes)
abilities_by_scope = {
api: full_authentication_abilities,
2018-05-09 12:01:36 +05:30
read_registry: [:read_container_image],
2019-07-31 22:56:46 +05:30
read_repository: [:download_code],
write_repository: [:download_code, :push_code]
2018-03-17 18:26:18 +05:30
}
scopes.flat_map do |scope|
abilities_by_scope.fetch(scope.to_sym, [])
end.uniq
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
def deploy_token_check(login, password)
return unless password.present?
token =
DeployToken.active.find_by(token: password)
return unless token && login
return if login != token.username
scopes = abilities_for_scopes(token.scopes)
2019-07-31 22:56:46 +05:30
if valid_scoped_token?(token, all_available_scopes)
2018-05-09 12:01:36 +05:30
Gitlab::Auth::Result.new(token, token.project, :deploy_token, scopes)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
2019-02-15 15:39:39 +05:30
def lfs_token_check(login, encoded_token, project)
2016-09-29 09:46:39 +05:30
deploy_key_matches = login.match(/\Alfs\+deploy-key-(\d+)\z/)
actor =
if deploy_key_matches
DeployKey.find(deploy_key_matches[1])
else
User.by_login(login)
end
return unless actor
token_handler = Gitlab::LfsToken.new(actor)
authentication_abilities =
if token_handler.user?
full_authentication_abilities
2018-03-17 18:26:18 +05:30
elsif token_handler.deploy_key_pushable?(project)
read_write_authentication_abilities
2016-09-29 09:46:39 +05:30
else
2019-07-31 22:56:46 +05:30
read_only_authentication_abilities
2016-09-29 09:46:39 +05:30
end
2019-02-15 15:39:39 +05:30
if token_handler.token_valid?(encoded_token)
2017-08-17 22:00:37 +05:30
Gitlab::Auth::Result.new(actor, nil, token_handler.type, authentication_abilities)
end
2016-09-29 09:46:39 +05:30
end
def build_access_token_check(login, password)
return unless login == 'gitlab-ci-token'
return unless password
2018-11-08 19:23:39 +05:30
build = find_build_by_token(password)
2016-09-29 09:46:39 +05:30
return unless build
return unless build.project.builds_enabled?
if build.user
# If user is assigned to build, use restricted credentials of user
Gitlab::Auth::Result.new(build.user, build.project, :build, build_authentication_abilities)
else
# Otherwise use generic CI credentials (backward compatibility)
Gitlab::Auth::Result.new(nil, build.project, :ci, build_authentication_abilities)
end
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
public
def build_authentication_abilities
[
:read_project,
:build_download_code,
:build_read_container_image,
:build_create_container_image
]
end
2019-07-31 22:56:46 +05:30
def read_only_authentication_abilities
2016-09-29 09:46:39 +05:30
[
:read_project,
:download_code,
:read_container_image
]
end
2018-03-17 18:26:18 +05:30
def read_write_authentication_abilities
2019-07-31 22:56:46 +05:30
read_only_authentication_abilities + [
2016-09-29 09:46:39 +05:30
:push_code,
2018-03-17 18:26:18 +05:30
:create_container_image
]
end
def full_authentication_abilities
read_write_authentication_abilities + [
2017-09-10 17:25:29 +05:30
:admin_container_image
2016-09-29 09:46:39 +05:30
]
end
2017-09-10 17:25:29 +05:30
2019-07-31 22:56:46 +05:30
def available_scopes_for(current_user)
scopes = non_admin_available_scopes
scopes += ADMIN_SCOPES if current_user.admin?
2018-03-17 18:26:18 +05:30
scopes
end
2019-07-31 22:56:46 +05:30
def all_available_scopes
non_admin_available_scopes + ADMIN_SCOPES
end
2018-03-17 18:26:18 +05:30
# Other available scopes
def optional_scopes
2019-07-31 22:56:46 +05:30
all_available_scopes + OPENID_SCOPES + PROFILE_SCOPES - DEFAULT_SCOPES
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
def registry_scopes
return [] unless Gitlab.config.registry.enabled
REGISTRY_SCOPES
2017-09-10 17:25:29 +05:30
end
2018-11-08 19:23:39 +05:30
private
2019-07-31 22:56:46 +05:30
def non_admin_available_scopes
API_SCOPES + REPOSITORY_SCOPES + registry_scopes
end
2018-11-08 19:23:39 +05:30
def find_build_by_token(token)
2019-02-15 15:39:39 +05:30
::Ci::Build.running.find_by_token(token)
2018-11-08 19:23:39 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
end