gitlab-org--gitlab-foss/app/controllers/jwt_controller.rb

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

106 lines
3.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-05-02 11:29:17 +00:00
class JwtController < ApplicationController
skip_around_action :set_session_storage
2016-05-02 11:29:17 +00:00
skip_before_action :authenticate_user!
skip_before_action :verify_authenticity_token
# 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 11:29:17 +00:00
feature_category :authentication_and_authorization
# https://gitlab.com/gitlab-org/gitlab/-/issues/357037
urgency :low
2016-05-02 12:32:16 +00:00
SERVICES = {
::Auth::ContainerRegistryAuthenticationService::AUDIENCE => ::Auth::ContainerRegistryAuthenticationService,
::Auth::DependencyProxyAuthenticationService::AUDIENCE => ::Auth::DependencyProxyAuthenticationService
2017-02-21 23:32:18 +00:00
}.freeze
2016-05-02 12:32:16 +00:00
2016-05-02 11:29:17 +00:00
def auth
2016-05-02 12:32:16 +00:00
service = SERVICES[params[:service]]
2016-05-15 00:45:33 +00:00
return head :not_found unless service
2016-05-02 11:29:17 +00:00
result = service.new(@authentication_result.project, auth_user, auth_params)
2017-06-21 13:48:12 +00:00
.execute(authentication_abilities: @authentication_result.authentication_abilities)
2016-05-02 11:29:17 +00:00
2016-05-14 19:04:04 +00:00
render json: result, status: result[:http_status]
2016-05-02 11:29:17 +00:00
end
2016-05-02 12:32:16 +00:00
private
2016-05-02 11:29:17 +00:00
2016-05-13 21:23:02 +00:00
def authenticate_project_or_user
@authentication_result = Gitlab::Auth::Result.new(nil, nil, :none, Gitlab::Auth.read_only_authentication_abilities)
2016-09-20 15:07:34 +00:00
2016-05-13 21:23:02 +00:00
authenticate_with_http_basic do |login, password|
@authentication_result = Gitlab::Auth.find_for_git_client(login, password, project: nil, ip: request.ip)
2016-05-13 21:23:02 +00:00
if @authentication_result.failed?
log_authentication_failed(login, @authentication_result)
render_unauthorized
end
2016-05-13 21:23:02 +00:00
end
rescue Gitlab::Auth::MissingPersonalAccessTokenError
render_missing_personal_access_token
2016-09-15 20:17:12 +00:00
end
def render_missing_personal_access_token
render json: {
errors: [
{ code: 'UNAUTHORIZED',
message: _('HTTP Basic: Access denied\n' \
'You must use a personal access token with \'api\' scope for Git over HTTP.\n' \
'You can generate one at %{profile_personal_access_tokens_url}') % { profile_personal_access_tokens_url: profile_personal_access_tokens_url } }
2017-02-22 17:44:44 +00:00
]
2018-07-02 10:43:06 +00:00
}, status: :unauthorized
end
def log_authentication_failed(login, result)
log_info = {
message: 'JWT authentication failed',
http_user: login,
remote_ip: request.ip,
auth_service: params[:service],
'auth_result.type': result.type,
'auth_result.actor_type': result.actor&.class
}.merge(::Gitlab::ApplicationContext.current)
Gitlab::AuthLogger.warn(log_info)
end
def render_unauthorized
render json: {
errors: [
{ code: 'UNAUTHORIZED',
message: 'HTTP Basic: Access denied' }
2017-02-22 17:44:44 +00:00
]
2018-07-02 10:43:06 +00:00
}, status: :unauthorized
2016-05-13 21:23:02 +00:00
end
2016-05-02 12:32:16 +00:00
def auth_params
params.permit(:service, :account, :client_id)
.merge(additional_params)
end
def additional_params
{ scopes: scopes_param, deploy_token: @authentication_result.deploy_token }.compact
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 11:29:17 +00:00
end
def auth_user
strong_memoize(:auth_user) do
@authentication_result.auth_user
end
end
2016-05-02 11:29:17 +00:00
end