2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-02 07:29:17 -04:00
|
|
|
class JwtController < ApplicationController
|
2019-07-20 09:30:26 -04:00
|
|
|
skip_around_action :set_session_storage
|
2016-05-02 07:29:17 -04:00
|
|
|
skip_before_action :authenticate_user!
|
|
|
|
skip_before_action :verify_authenticity_token
|
2020-05-15 14:07:52 -04:00
|
|
|
|
|
|
|
# Add this before other actions, since we want to have the user or project
|
|
|
|
prepend_before_action :auth_user, :authenticate_project_or_user
|
2016-05-02 07:29:17 -04:00
|
|
|
|
2020-10-05 08:08:47 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2016-05-02 08:32:16 -04:00
|
|
|
SERVICES = {
|
2020-12-03 13:10:10 -05:00
|
|
|
::Auth::ContainerRegistryAuthenticationService::AUDIENCE => ::Auth::ContainerRegistryAuthenticationService,
|
|
|
|
::Auth::DependencyProxyAuthenticationService::AUDIENCE => ::Auth::DependencyProxyAuthenticationService
|
2017-02-21 18:32:18 -05:00
|
|
|
}.freeze
|
2016-05-02 08:32:16 -04:00
|
|
|
|
2016-05-02 07:29:17 -04:00
|
|
|
def auth
|
2016-05-02 08:32:16 -04:00
|
|
|
service = SERVICES[params[:service]]
|
2016-05-14 20:45:33 -04:00
|
|
|
return head :not_found unless service
|
2016-05-02 07:29:17 -04:00
|
|
|
|
2021-07-27 08:10:54 -04:00
|
|
|
result = service.new(@authentication_result.project, auth_user, auth_params)
|
2017-06-21 09:48:12 -04:00
|
|
|
.execute(authentication_abilities: @authentication_result.authentication_abilities)
|
2016-05-02 07:29:17 -04:00
|
|
|
|
2016-05-14 15:04:04 -04:00
|
|
|
render json: result, status: result[:http_status]
|
2016-05-02 07:29:17 -04:00
|
|
|
end
|
|
|
|
|
2016-05-02 08:32:16 -04:00
|
|
|
private
|
2016-05-02 07:29:17 -04:00
|
|
|
|
2016-05-13 17:23:02 -04:00
|
|
|
def authenticate_project_or_user
|
2019-04-15 09:05:55 -04:00
|
|
|
@authentication_result = Gitlab::Auth::Result.new(nil, nil, :none, Gitlab::Auth.read_only_authentication_abilities)
|
2016-09-20 11:07:34 -04:00
|
|
|
|
2016-05-13 17:23:02 -04:00
|
|
|
authenticate_with_http_basic do |login, password|
|
2018-04-03 17:34:56 -04:00
|
|
|
@authentication_result = Gitlab::Auth.find_for_git_client(login, password, project: nil, ip: request.ip)
|
2016-05-13 17:23:02 -04:00
|
|
|
|
2018-04-06 15:48:17 -04:00
|
|
|
if @authentication_result.failed?
|
2017-05-31 09:55:12 -04:00
|
|
|
render_unauthorized
|
|
|
|
end
|
2016-05-13 17:23:02 -04:00
|
|
|
end
|
2020-12-16 04:10:26 -05:00
|
|
|
rescue Gitlab::Auth::MissingPersonalAccessTokenError
|
2017-10-12 05:27:16 -04:00
|
|
|
render_missing_personal_access_token
|
2016-09-15 16:17:12 -04:00
|
|
|
end
|
|
|
|
|
2017-10-12 05:27:16 -04:00
|
|
|
def render_missing_personal_access_token
|
2016-09-26 06:18:21 -04:00
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ code: 'UNAUTHORIZED',
|
2019-04-16 06:32:05 -04:00
|
|
|
message: _('HTTP Basic: Access denied\n' \
|
2020-12-16 04:10:26 -05:00
|
|
|
'You must use a personal access token with \'api\' scope for Git over HTTP.\n' \
|
2019-04-16 06:32:05 -04:00
|
|
|
'You can generate one at %{profile_personal_access_tokens_url}') % { profile_personal_access_tokens_url: profile_personal_access_tokens_url } }
|
2017-02-22 12:44:44 -05:00
|
|
|
]
|
2018-07-02 06:43:06 -04:00
|
|
|
}, status: :unauthorized
|
2016-09-26 06:18:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized
|
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ code: 'UNAUTHORIZED',
|
|
|
|
message: 'HTTP Basic: Access denied' }
|
2017-02-22 12:44:44 -05:00
|
|
|
]
|
2018-07-02 06:43:06 -04:00
|
|
|
}, status: :unauthorized
|
2016-05-13 17:23:02 -04:00
|
|
|
end
|
|
|
|
|
2016-05-02 08:32:16 -04:00
|
|
|
def auth_params
|
2018-07-13 13:45:07 -04:00
|
|
|
params.permit(:service, :account, :client_id)
|
|
|
|
.merge(additional_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def additional_params
|
2021-07-27 08:10:54 -04:00
|
|
|
{ scopes: scopes_param, deploy_token: @authentication_result.deploy_token }.compact
|
2018-07-13 13:45:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# We have to parse scope here, because Docker Client does not send an array of scopes,
|
|
|
|
# but rather a flat list and we loose second scope when being processed by Rails:
|
|
|
|
# scope=scopeA&scope=scopeB
|
|
|
|
#
|
|
|
|
# This method makes to always return an array of scopes
|
|
|
|
def scopes_param
|
|
|
|
return unless params[:scope].present?
|
|
|
|
|
|
|
|
Array(Rack::Utils.parse_query(request.query_string)['scope'])
|
2016-05-02 07:29:17 -04:00
|
|
|
end
|
2020-05-11 11:09:37 -04:00
|
|
|
|
|
|
|
def auth_user
|
2020-05-15 14:07:52 -04:00
|
|
|
strong_memoize(:auth_user) do
|
2021-07-27 08:10:54 -04:00
|
|
|
@authentication_result.auth_user
|
2020-05-15 14:07:52 -04:00
|
|
|
end
|
2020-05-11 11:09:37 -04:00
|
|
|
end
|
2016-05-02 07:29:17 -04:00
|
|
|
end
|