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

60 lines
1.8 KiB
Ruby
Raw Normal View History

2016-05-02 07:29:17 -04:00
class JwtController < ApplicationController
skip_before_action :authenticate_user!
skip_before_action :verify_authenticity_token
2016-05-13 17:23:02 -04:00
before_action :authenticate_project_or_user
2016-05-02 07:29:17 -04:00
2016-05-02 08:32:16 -04:00
SERVICES = {
Auth::ContainerRegistryAuthenticationService::AUDIENCE => Auth::ContainerRegistryAuthenticationService
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
2017-06-21 09:48:12 -04:00
result = service.new(@authentication_result.project, @authentication_result.actor, auth_params)
.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
2017-06-06 07:18:01 -04:00
@authentication_result = Gitlab::Auth::Result.new(nil, nil, :none, Gitlab::Auth.read_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|
@authentication_result = Gitlab::Auth.find_for_git_client(login, password, project: nil, ip: request.ip)
2016-05-13 17:23:02 -04:00
if @authentication_result.failed?
render_unauthorized
end
2016-05-13 17:23:02 -04:00
end
rescue Gitlab::Auth::MissingPersonalAccessTokenError
render_missing_personal_access_token
2016-09-15 16:17:12 -04: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}" }
2017-02-22 12:44:44 -05:00
]
2018-07-02 06:43:06 -04:00
}, status: :unauthorized
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
params.permit(:service, :scope, :account, :client_id)
2016-05-02 07:29:17 -04:00
end
end