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

88 lines
2.7 KiB
Ruby
Raw Normal View History

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 = {
2016-05-14 23:23:31 +00:00
Auth::ContainerRegistryAuthenticationService::AUDIENCE => Auth::ContainerRegistryAuthenticationService,
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
2016-05-09 17:47:06 +00:00
result = service.new(@project, @user, auth_params).execute
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
authenticate_with_http_basic do |login, password|
# if it's possible we first try to authenticate project with login and password
@project = authenticate_project(login, password)
return if @project
@user = authenticate_user(login, password)
return if @user
2016-05-14 19:04:04 +00:00
render_403
2016-05-13 21:23:02 +00:00
end
end
2016-05-02 12:32:16 +00:00
def auth_params
params.permit(:service, :scope, :account, :client_id)
2016-05-02 11:29:17 +00:00
end
2016-05-09 17:47:06 +00:00
def authenticate_project(login, password)
if login == 'gitlab-ci-token'
2016-05-13 21:23:02 +00:00
Project.find_by(builds_enabled: true, runners_token: password)
2016-05-02 11:29:17 +00:00
end
end
def authenticate_user(login, password)
2016-05-14 19:16:44 +00:00
# TODO: this is a copy and paste from grack_auth,
# it should be refactored in the future
2016-05-02 11:29:17 +00:00
user = Gitlab::Auth.new.find(login, password)
# If the user authenticated successfully, we reset the auth failure count
# from Rack::Attack for that IP. A client may attempt to authenticate
# with a username and blank password first, and only after it receives
# a 401 error does it present a password. Resetting the count prevents
# false positives from occurring.
#
# Otherwise, we let Rack::Attack know there was a failed authentication
# attempt from this IP. This information is stored in the Rails cache
# (Redis) and will be used by the Rack::Attack middleware to decide
# whether to block requests from this IP.
config = Gitlab.config.rack_attack.git_basic_auth
if config.enabled
if user
# A successful login will reset the auth failure count from this IP
Rack::Attack::Allow2Ban.reset(request.ip, config)
else
banned = Rack::Attack::Allow2Ban.filter(request.ip, config) do
# Unless the IP is whitelisted, return true so that Allow2Ban
# increments the counter (stored in Rails.cache) for the IP
if config.ip_whitelist.include?(request.ip)
false
else
true
end
end
if banned
Rails.logger.info "IP #{request.ip} failed to login " \
"as #{login} but has been temporarily banned from Git auth"
2016-05-13 21:23:02 +00:00
return
2016-05-02 11:29:17 +00:00
end
end
end
user
end
end