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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
# Use for authentication only, in particular for Rack::Attack.
# Does not perform authorization of scopes, etc.
module Gitlab
module Auth
class RequestAuthenticator
2020-01-01 13:55:28 +05:30
include AuthFinders
2018-03-17 18:26:18 +05:30
attr_reader :request
def initialize(request)
@request = request
end
2022-05-03 16:02:30 +05:30
def find_authenticated_requester(request_formats)
user(request_formats) || deploy_token_from_request
end
2018-11-29 20:51:05 +05:30
def user(request_formats)
request_formats.each do |format|
user = find_sessionless_user(format)
return user if user
end
find_user_from_warden
2018-03-17 18:26:18 +05:30
end
2020-01-01 13:55:28 +05:30
def runner
find_runner_from_token
rescue Gitlab::Auth::AuthenticationError
nil
end
2018-11-29 20:51:05 +05:30
def find_sessionless_user(request_format)
2021-11-18 22:05:49 +05:30
find_user_from_dependency_proxy_token ||
find_user_from_web_access_token(request_format, scopes: [:api, :read_api]) ||
2019-12-04 20:38:33 +05:30
find_user_from_feed_token(request_format) ||
2020-01-01 13:55:28 +05:30
find_user_from_static_object_token(request_format) ||
2020-03-13 15:44:24 +05:30
find_user_from_basic_auth_job ||
2021-09-30 23:02:18 +05:30
find_user_from_job_token ||
find_user_from_personal_access_token_for_api_or_git ||
find_user_for_git_or_lfs_request
2018-03-17 18:26:18 +05:30
rescue Gitlab::Auth::AuthenticationError
nil
end
2018-03-27 19:54:05 +05:30
2022-05-07 20:08:51 +05:30
def can_sign_in_bot?(user)
user&.project_bot? && api_request?
end
2021-09-30 23:02:18 +05:30
# To prevent Rack Attack from incorrectly rate limiting
# authenticated Git activity, we need to authenticate the user
# from other means (e.g. HTTP Basic Authentication) only if the
# request originated from a Git or Git LFS
# request. Repositories::GitHttpClientController or
# Repositories::LfsApiController normally does the authentication,
# but Rack Attack runs before those controllers.
def find_user_for_git_or_lfs_request
return unless git_or_lfs_request?
find_user_from_lfs_token || find_user_from_basic_auth_password
end
def find_user_from_personal_access_token_for_api_or_git
return unless api_request? || git_or_lfs_request?
find_user_from_personal_access_token
end
2018-03-27 19:54:05 +05:30
def valid_access_token?(scopes: [])
validate_access_token!(scopes: scopes)
true
rescue Gitlab::Auth::AuthenticationError
false
end
2020-03-13 15:44:24 +05:30
private
2021-02-22 17:27:13 +05:30
def access_token
strong_memoize(:access_token) do
super || find_personal_access_token_from_http_basic_auth
end
end
2020-03-13 15:44:24 +05:30
def route_authentication_setting
@route_authentication_setting ||= {
2021-02-22 17:27:13 +05:30
job_token_allowed: api_request?,
2022-05-03 16:02:30 +05:30
basic_auth_personal_access_token: api_request? || git_request?,
deploy_token_allowed: api_request? || git_request?
2020-03-13 15:44:24 +05:30
}
end
2021-11-18 22:05:49 +05:30
def find_user_from_dependency_proxy_token
return unless dependency_proxy_request?
token, _ = ActionController::HttpAuthentication::Token.token_and_options(current_request)
return unless token
user_or_deploy_token = ::DependencyProxy::AuthTokenService.user_or_deploy_token_from_jwt(token)
# Do not return deploy tokens
# See https://gitlab.com/gitlab-org/gitlab/-/issues/342481
return unless user_or_deploy_token.is_a?(::User)
user_or_deploy_token
rescue ActiveRecord::RecordNotFound
nil # invalid id used return no user
end
def dependency_proxy_request?
Gitlab::PathRegex.dependency_proxy_route_regex.match?(current_request.path)
end
2018-03-17 18:26:18 +05:30
end
end
end
2022-04-04 11:22:00 +05:30
Gitlab::Auth::RequestAuthenticator.prepend_mod