2016-05-02 11:29:17 +00:00
|
|
|
class JwtController < ApplicationController
|
|
|
|
skip_before_action :authenticate_user!
|
|
|
|
skip_before_action :verify_authenticity_token
|
2016-05-13 21:23:02 +00:00
|
|
|
before_action :authenticate_project_or_user
|
2016-05-02 11:29:17 +00:00
|
|
|
|
2016-05-02 12:32:16 +00:00
|
|
|
SERVICES = {
|
2017-05-03 11:22:03 +00:00
|
|
|
Auth::ContainerRegistryAuthenticationService::AUDIENCE => Auth::ContainerRegistryAuthenticationService
|
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
|
|
|
|
2017-02-22 22:50:15 +00:00
|
|
|
result = service.new(@authentication_result.project, @authentication_result.actor, auth_params).
|
|
|
|
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
|
2017-05-31 13:55:12 +00:00
|
|
|
@authentication_result = Gitlab::Auth::Result.new(nil, nil, :none, Gitlab::Auth.read_api_abilities)
|
2016-09-20 15:07:34 +00:00
|
|
|
|
2016-05-13 21:23:02 +00:00
|
|
|
authenticate_with_http_basic do |login, password|
|
2016-09-15 11:49:11 +00:00
|
|
|
@authentication_result = Gitlab::Auth.find_for_git_client(login, password, project: nil, ip: request.ip)
|
2016-05-13 21:23:02 +00:00
|
|
|
|
2017-05-31 13:55:12 +00:00
|
|
|
if @authentication_result.failed? ||
|
|
|
|
(@authentication_result.actor.present? && !@authentication_result.actor.is_a?(User))
|
|
|
|
render_unauthorized
|
|
|
|
end
|
2016-05-13 21:23:02 +00:00
|
|
|
end
|
2016-09-15 20:17:12 +00:00
|
|
|
rescue Gitlab::Auth::MissingPersonalTokenError
|
|
|
|
render_missing_personal_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_missing_personal_token
|
2016-09-26 10:18:21 +00:00
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ code: 'UNAUTHORIZED',
|
|
|
|
message: "HTTP Basic: Access denied\n" \
|
|
|
|
"You have 2FA enabled, please use a personal access token for Git over HTTP.\n" \
|
|
|
|
"You can generate one at #{profile_personal_access_tokens_url}" }
|
2017-02-22 17:44:44 +00:00
|
|
|
]
|
|
|
|
}, status: 401
|
2016-09-26 10:18:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_unauthorized
|
|
|
|
render json: {
|
|
|
|
errors: [
|
|
|
|
{ code: 'UNAUTHORIZED',
|
|
|
|
message: 'HTTP Basic: Access denied' }
|
2017-02-22 17:44:44 +00:00
|
|
|
]
|
|
|
|
}, status: 401
|
2016-05-13 21:23:02 +00:00
|
|
|
end
|
|
|
|
|
2016-05-02 12:32:16 +00:00
|
|
|
def auth_params
|
2016-05-30 14:57:39 +00:00
|
|
|
params.permit(:service, :scope, :account, :client_id)
|
2016-05-02 11:29:17 +00:00
|
|
|
end
|
|
|
|
end
|