summaryrefslogtreecommitdiff
path: root/lib/gitlab/api_authentication/token_locator.rb
blob: df342905d2ef2d82e7bcef4d800f7d9380f1c984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

module Gitlab
  module APIAuthentication
    class TokenLocator
      UsernameAndPassword = Struct.new(:username, :password)

      include ActiveModel::Validations
      include ActionController::HttpAuthentication::Basic

      attr_reader :location

      validates :location, inclusion: {
        in: %i[
          http_basic_auth
          http_token
          http_bearer_token
          http_deploy_token_header
          http_job_token_header
          http_private_token_header
          token_param
        ]
      }

      def initialize(location)
        @location = location
        validate!
      end

      def extract(request)
        case @location
        when :http_basic_auth
          extract_from_http_basic_auth request
        when :http_token
          extract_from_http_token request
        when :http_bearer_token
          extract_from_http_bearer_token request
        when :http_deploy_token_header
          extract_from_http_deploy_token_header request
        when :http_job_token_header
          extract_from_http_job_token_header request
        when :http_private_token_header
          extract_from_http_private_token_header request
        when :token_param
          extract_from_token_param request
        end
      end

      private

      def extract_from_http_basic_auth(request)
        username, password = user_name_and_password(request)
        return unless username.present? && password.present?

        UsernameAndPassword.new(username, password)
      end

      def extract_from_http_token(request)
        password = request.headers['Authorization']
        return unless password.present?

        UsernameAndPassword.new(nil, password)
      end

      def extract_from_http_bearer_token(request)
        password = request.headers['Authorization']
        return unless password.present?

        UsernameAndPassword.new(nil, password.split(' ').last)
      end

      def extract_from_http_deploy_token_header(request)
        password = request.headers['Deploy-Token']
        return unless password.present?

        UsernameAndPassword.new(nil, password)
      end

      def extract_from_http_job_token_header(request)
        password = request.headers['Job-Token']
        return unless password.present?

        UsernameAndPassword.new(nil, password)
      end

      def extract_from_http_private_token_header(request)
        password = request.headers['Private-Token']
        return unless password.present?

        UsernameAndPassword.new(nil, password)
      end

      def extract_from_token_param(request)
        password = request.query_parameters['token']
        return unless password.present?

        UsernameAndPassword.new(nil, password)
      end
    end
  end
end