2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-13 20:05:18 -04:00
|
|
|
# Use for authentication only, in particular for Rack::Attack.
|
|
|
|
# Does not perform authorization of scopes, etc.
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
class RequestAuthenticator
|
2017-11-07 04:52:05 -05:00
|
|
|
include UserAuthFinders
|
|
|
|
|
|
|
|
attr_reader :request
|
|
|
|
|
2017-10-13 20:05:18 -04:00
|
|
|
def initialize(request)
|
2017-11-08 13:41:07 -05:00
|
|
|
@request = request
|
2017-10-13 20:05:18 -04:00
|
|
|
end
|
|
|
|
|
2018-11-28 14:06:02 -05: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-13 20:05:18 -04:00
|
|
|
end
|
|
|
|
|
2018-11-28 14:06:02 -05:00
|
|
|
def find_sessionless_user(request_format)
|
|
|
|
find_user_from_web_access_token(request_format) || find_user_from_feed_token(request_format)
|
2017-11-17 07:33:21 -05:00
|
|
|
rescue Gitlab::Auth::AuthenticationError
|
2017-11-08 13:41:07 -05:00
|
|
|
nil
|
2017-10-13 20:05:18 -04:00
|
|
|
end
|
2018-02-23 05:33:46 -05:00
|
|
|
|
|
|
|
def valid_access_token?(scopes: [])
|
|
|
|
validate_access_token!(scopes: scopes)
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Gitlab::Auth::AuthenticationError
|
|
|
|
false
|
|
|
|
end
|
2017-10-13 20:05:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|