2018-10-11 20:12:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-14 00:05:18 +00:00
|
|
|
# Use for authentication only, in particular for Rack::Attack.
|
|
|
|
# Does not perform authorization of scopes, etc.
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
class RequestAuthenticator
|
2019-12-11 12:08:10 +00:00
|
|
|
include AuthFinders
|
2017-11-07 09:52:05 +00:00
|
|
|
|
|
|
|
attr_reader :request
|
|
|
|
|
2017-10-14 00:05:18 +00:00
|
|
|
def initialize(request)
|
2017-11-08 18:41:07 +00:00
|
|
|
@request = request
|
2017-10-14 00:05:18 +00:00
|
|
|
end
|
|
|
|
|
2018-11-28 19:06:02 +00:00
|
|
|
def user(request_formats)
|
|
|
|
request_formats.each do |format|
|
|
|
|
user = find_sessionless_user(format)
|
|
|
|
|
|
|
|
return user if user
|
|
|
|
end
|
|
|
|
|
|
|
|
find_user_from_warden
|
2017-10-14 00:05:18 +00:00
|
|
|
end
|
|
|
|
|
2019-12-11 12:08:10 +00:00
|
|
|
def runner
|
|
|
|
find_runner_from_token
|
|
|
|
rescue Gitlab::Auth::AuthenticationError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-11-28 19:06:02 +00:00
|
|
|
def find_sessionless_user(request_format)
|
2021-01-04 21:10:19 +00:00
|
|
|
find_user_from_web_access_token(request_format, scopes: [:api, :read_api]) ||
|
2019-07-22 14:56:40 +00:00
|
|
|
find_user_from_feed_token(request_format) ||
|
2019-12-17 03:07:45 +00:00
|
|
|
find_user_from_static_object_token(request_format) ||
|
2020-01-09 12:08:03 +00:00
|
|
|
find_user_from_basic_auth_job ||
|
|
|
|
find_user_from_job_token
|
2017-11-17 12:33:21 +00:00
|
|
|
rescue Gitlab::Auth::AuthenticationError
|
2017-11-08 18:41:07 +00:00
|
|
|
nil
|
2017-10-14 00:05:18 +00:00
|
|
|
end
|
2018-02-23 10:33:46 +00:00
|
|
|
|
|
|
|
def valid_access_token?(scopes: [])
|
|
|
|
validate_access_token!(scopes: scopes)
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gitlab::Auth::AuthenticationError
|
|
|
|
false
|
|
|
|
end
|
2020-01-09 12:08:03 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-12-04 12:09:39 +00:00
|
|
|
def access_token
|
|
|
|
strong_memoize(:access_token) do
|
|
|
|
super || find_personal_access_token_from_http_basic_auth
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-09 12:08:03 +00:00
|
|
|
def route_authentication_setting
|
|
|
|
@route_authentication_setting ||= {
|
2020-12-04 12:09:39 +00:00
|
|
|
job_token_allowed: api_request?,
|
|
|
|
basic_auth_personal_access_token: api_request?
|
2020-01-09 12:08:03 +00:00
|
|
|
}
|
|
|
|
end
|
2017-10-14 00:05:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|