2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
AuthenticationError = Class.new(StandardError)
|
|
|
|
MissingTokenError = Class.new(AuthenticationError)
|
|
|
|
TokenNotFoundError = Class.new(AuthenticationError)
|
|
|
|
ExpiredError = Class.new(AuthenticationError)
|
|
|
|
RevokedError = Class.new(AuthenticationError)
|
2019-02-13 22:33:31 +05:30
|
|
|
ImpersonationDisabled = Class.new(AuthenticationError)
|
2018-03-17 18:26:18 +05:30
|
|
|
UnauthorizedError = Class.new(AuthenticationError)
|
|
|
|
|
|
|
|
class InsufficientScopeError < AuthenticationError
|
|
|
|
attr_reader :scopes
|
|
|
|
def initialize(scopes)
|
|
|
|
@scopes = scopes.map { |s| s.try(:name) || s }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module UserAuthFinders
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
PRIVATE_TOKEN_HEADER = 'HTTP_PRIVATE_TOKEN'.freeze
|
|
|
|
PRIVATE_TOKEN_PARAM = :private_token
|
|
|
|
|
|
|
|
# Check the Rails session for valid authentication details
|
|
|
|
def find_user_from_warden
|
|
|
|
current_request.env['warden']&.authenticate if verified_request?
|
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
def find_user_from_feed_token(request_format)
|
|
|
|
return unless valid_rss_format?(request_format)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# NOTE: feed_token was renamed from rss_token but both needs to be supported because
|
|
|
|
# users might have already added the feed to their RSS reader before the rename
|
|
|
|
token = current_request.params[:feed_token].presence || current_request.params[:rss_token].presence
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless token
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
User.find_by_feed_token(token) || raise(UnauthorizedError)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
# We only allow Private Access Tokens with `api` scope to be used by web
|
|
|
|
# requests on RSS feeds or ICS files for backwards compatibility.
|
|
|
|
# It is also used by GraphQL/API requests.
|
|
|
|
def find_user_from_web_access_token(request_format)
|
|
|
|
return unless access_token && valid_web_access_format?(request_format)
|
|
|
|
|
|
|
|
validate_access_token!(scopes: [:api])
|
|
|
|
|
|
|
|
access_token.user || raise(UnauthorizedError)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def find_user_from_access_token
|
|
|
|
return unless access_token
|
|
|
|
|
|
|
|
validate_access_token!
|
|
|
|
|
|
|
|
access_token.user || raise(UnauthorizedError)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_access_token!(scopes: [])
|
|
|
|
return unless access_token
|
|
|
|
|
|
|
|
case AccessTokenValidationService.new(access_token, request: request).validate(scopes: scopes)
|
|
|
|
when AccessTokenValidationService::INSUFFICIENT_SCOPE
|
|
|
|
raise InsufficientScopeError.new(scopes)
|
|
|
|
when AccessTokenValidationService::EXPIRED
|
|
|
|
raise ExpiredError
|
|
|
|
when AccessTokenValidationService::REVOKED
|
|
|
|
raise RevokedError
|
2019-02-13 22:33:31 +05:30
|
|
|
when AccessTokenValidationService::IMPERSONATION_DISABLED
|
|
|
|
raise ImpersonationDisabled
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def route_authentication_setting
|
|
|
|
return {} unless respond_to?(:route_setting)
|
|
|
|
|
|
|
|
route_setting(:authentication) || {}
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def access_token
|
|
|
|
strong_memoize(:access_token) do
|
|
|
|
find_oauth_access_token || find_personal_access_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_personal_access_token
|
|
|
|
token =
|
|
|
|
current_request.params[PRIVATE_TOKEN_PARAM].presence ||
|
|
|
|
current_request.env[PRIVATE_TOKEN_HEADER].presence
|
|
|
|
|
|
|
|
return unless token
|
|
|
|
|
|
|
|
# Expiration, revocation and scopes are verified in `validate_access_token!`
|
2018-11-18 11:00:15 +05:30
|
|
|
PersonalAccessToken.find_by_token(token) || raise(UnauthorizedError)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def find_oauth_access_token
|
|
|
|
token = Doorkeeper::OAuth::Token.from_request(current_request, *Doorkeeper.configuration.access_token_methods)
|
|
|
|
return unless token
|
|
|
|
|
|
|
|
# Expiration, revocation and scopes are verified in `validate_access_token!`
|
|
|
|
oauth_token = OauthAccessToken.by_token(token)
|
|
|
|
raise UnauthorizedError unless oauth_token
|
|
|
|
|
|
|
|
oauth_token.revoke_previous_refresh_token!
|
|
|
|
oauth_token
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if the request is GET/HEAD, or if CSRF token is valid.
|
|
|
|
def verified_request?
|
|
|
|
Gitlab::RequestForgeryProtection.verified?(current_request.env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_action_dispatch_request(request)
|
|
|
|
ActionDispatch::Request.new(request.env.dup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_request
|
|
|
|
@current_request ||= ensure_action_dispatch_request(request)
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
def valid_web_access_format?(request_format)
|
|
|
|
case request_format
|
|
|
|
when :rss
|
|
|
|
rss_request?
|
|
|
|
when :ics
|
|
|
|
ics_request?
|
|
|
|
when :api
|
|
|
|
api_request?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_rss_format?(request_format)
|
|
|
|
case request_format
|
|
|
|
when :rss
|
|
|
|
rss_request?
|
|
|
|
when :ics
|
|
|
|
ics_request?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def rss_request?
|
|
|
|
current_request.path.ends_with?('.atom') || current_request.format.atom?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ics_request?
|
|
|
|
current_request.path.ends_with?('.ics') || current_request.format.ics?
|
|
|
|
end
|
2018-11-29 20:51:05 +05:30
|
|
|
|
|
|
|
def api_request?
|
|
|
|
current_request.path.starts_with?("/api/")
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|