Commit b5261197 authored by Giorgenes Gelatti's avatar Giorgenes Gelatti

Fix job auth specs

parent 7c8aa97c
...@@ -140,11 +140,11 @@ module EE ...@@ -140,11 +140,11 @@ module EE
end end
def job_token_authentication? def job_token_authentication?
initial_current_user && find_current_job initial_current_user && @job_token_authentication # rubocop:disable Gitlab/ModuleWithInstanceVariables
end end
def current_ci_job def current_ci_job
find_current_job @job_token_authentication
end end
def warden def warden
......
...@@ -6,22 +6,25 @@ module EE ...@@ -6,22 +6,25 @@ module EE
module UserAuthFinders module UserAuthFinders
extend ActiveSupport::Concern extend ActiveSupport::Concern
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::Gitlab::Utils::StrongMemoize
JOB_TOKEN_HEADER = "HTTP_JOB_TOKEN".freeze JOB_TOKEN_HEADER = "HTTP_JOB_TOKEN".freeze
JOB_TOKEN_PARAM = :job_token JOB_TOKEN_PARAM = :job_token
def find_user_from_bearer_token def find_user_from_bearer_token
find_current_job&.user || find_user_from_job_bearer_token ||
find_user_from_access_token find_user_from_access_token
end end
def find_user_from_job_token def find_user_from_job_token
return unless job_token return unless route_authentication_setting[:job_token_allowed]
token = (params[JOB_TOKEN_PARAM] || env[JOB_TOKEN_HEADER]).to_s
return unless token.present?
raise ::Gitlab::Auth::UnauthorizedError unless find_current_job job = find_job_from_token(token)
raise ::Gitlab::Auth::UnauthorizedError unless job
find_current_job.user job.user
end end
override :find_oauth_access_token override :find_oauth_access_token
...@@ -33,31 +36,30 @@ module EE ...@@ -33,31 +36,30 @@ module EE
override :validate_access_token! override :validate_access_token!
def validate_access_token!(scopes: []) def validate_access_token!(scopes: [])
# if we have a successful job token, don't go ahead and try regular validation as it will fail # return early if we've already authenticated via a job token
# for the job token @job_token_authentication.present? || super # rubocop:disable Gitlab/ModuleWithInstanceVariables
find_current_job || super
end end
def scim_request? def scim_request?
current_request.path.starts_with?("/api/scim/") current_request.path.starts_with?("/api/scim/")
end end
def find_current_job private
return unless job_token
strong_memoize(:find_current_job) do def find_job_from_token(token)
::Ci::Build.find_by_token(job_token) @job_token_authentication ||= ::Ci::Build.find_by_token(token)
end
end end
private def find_user_from_job_bearer_token
def job_token
return unless route_authentication_setting[:job_token_allowed] return unless route_authentication_setting[:job_token_allowed]
strong_memoize(:job_token) do token = parsed_oauth_token
(params[JOB_TOKEN_PARAM] || env[JOB_TOKEN_HEADER] || parsed_oauth_token).to_s return unless token
end
job = find_job_from_token(token)
return unless job
job.user
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment