BigW Consortium Gitlab

jwt_controller.rb 1.32 KB
Newer Older
Kamil Trzcinski committed
1 2 3
class JwtController < ApplicationController
  skip_before_action :authenticate_user!
  skip_before_action :verify_authenticity_token
4
  before_action :authenticate_project_or_user
Kamil Trzcinski committed
5

6
  SERVICES = {
7
    Auth::ContainerRegistryAuthenticationService::AUDIENCE => Auth::ContainerRegistryAuthenticationService,
8 9
  }

Kamil Trzcinski committed
10
  def auth
11
    service = SERVICES[params[:service]]
12
    return head :not_found unless service
Kamil Trzcinski committed
13

14
    result = service.new(@project, @user, auth_params).execute
Kamil Trzcinski committed
15

16
    render json: result, status: result[:http_status]
Kamil Trzcinski committed
17 18
  end

19
  private
Kamil Trzcinski committed
20

21 22 23 24 25 26 27 28 29
  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

30
      render_403
31 32 33
    end
  end

34
  def auth_params
35
    params.permit(:service, :scope, :account, :client_id)
Kamil Trzcinski committed
36 37
  end

38
  def authenticate_project(login, password)
39
    if login == 'gitlab-ci-token'
40
      Project.find_by(builds_enabled: true, runners_token: password)
Kamil Trzcinski committed
41 42 43 44
    end
  end

  def authenticate_user(login, password)
45
    user = Gitlab::Auth.find_with_user_password(login, password)
46
    Gitlab::Auth.rate_limit!(request.ip, success: user.present?, login: login)
Kamil Trzcinski committed
47 48 49
    user
  end
end