auth.rb 5.9 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1
module Gitlab
2
  module Auth
3 4
    class MissingPersonalTokenError < StandardError; end

5 6 7 8
    SCOPES = [:api, :read_user]
    DEFAULT_SCOPES = [:api]
    OPTIONAL_SCOPES = SCOPES - DEFAULT_SCOPES

9
    class << self
10
      def find_for_git_client(login, password, project:, ip:)
11 12
        raise "Must provide an IP for rate limiting" if ip.nil?

13
        result =
14
          service_request_check(login, password, project) ||
15 16 17
          build_access_token_check(login, password) ||
          user_with_password_for_git(login, password) ||
          oauth_access_token_check(login, password) ||
18
          lfs_token_check(login, password) ||
19
          personal_access_token_check(login, password) ||
20
          Gitlab::Auth::Result.new
21

22
        rate_limit!(ip, success: result.success?, login: login)
23

24
        result
25 26
      end

27
      def find_with_user_password(login, password)
28 29 30 31 32 33 34 35 36 37 38 39 40 41
        user = User.by_login(login)

        # If no user is found, or it's an LDAP server, try LDAP.
        #   LDAP users are only authenticated via LDAP
        if user.nil? || user.ldap_user?
          # Second chance - try LDAP authentication
          return nil unless Gitlab::LDAP::Config.enabled?

          Gitlab::LDAP::Authentication.login(login, password)
        else
          user if user.valid_password?(password)
        end
      end

J
Jacob Vosmaer 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      def rate_limit!(ip, success:, login:)
        rate_limiter = Gitlab::Auth::IpRateLimiter.new(ip)
        return unless rate_limiter.enabled?

        if success
          # Repeated login 'failures' are normal behavior for some Git clients so
          # it is important to reset the ban counter once the client has proven
          # they are not a 'bad guy'.
          rate_limiter.reset!
        else
          # Register a login failure so that Rack::Attack can block the next
          # request from this IP if needed.
          rate_limiter.register_fail!

          if rate_limiter.banned?
            Rails.logger.info "IP #{ip} failed to login " \
              "as #{login} but has been temporarily banned from Git auth"
          end
        end
      end

63 64
      private

65
      def service_request_check(login, password, project)
66 67
        matched_login = /(?<service>^[a-zA-Z]*-ci)-token$/.match(login)

68
        return unless project && matched_login.present?
69 70 71

        underscored_service = matched_login['service'].underscore

72
        if Service.available_services_names.include?(underscored_service)
73 74
          # We treat underscored_service as a trusted input because it is included
          # in the Service.available_services_names whitelist.
J
Jacob Vosmaer 已提交
75
          service = project.public_send("#{underscored_service}_service")
76

77
          if service && service.activated? && service.valid_token?(password)
78
            Gitlab::Auth::Result.new(nil, project, :ci, build_authentication_abilities)
79 80 81 82 83 84
          end
        end
      end

      def user_with_password_for_git(login, password)
        user = find_with_user_password(login, password)
85 86
        return unless user

87
        raise Gitlab::Auth::MissingPersonalTokenError if user.two_factor_enabled?
88

89
        Gitlab::Auth::Result.new(user, nil, :gitlab_or_ldap, full_authentication_abilities)
90 91
      end

92 93 94
      def oauth_access_token_check(login, password)
        if login == "oauth2" && password.present?
          token = Doorkeeper::AccessToken.by_token(password)
95
          if token && token.accessible? && token_has_scope?(token)
96
            user = User.find_by(id: token.resource_owner_id)
97
            Gitlab::Auth::Result.new(user, nil, :oauth, read_authentication_abilities)
98
          end
99 100
        end
      end
101 102 103

      def personal_access_token_check(login, password)
        if login && password
104
          token = PersonalAccessToken.active.find_by_token(password)
105
          validation = User.by_login(login)
106 107 108 109

          if token && token.user == validation && token_has_scope?(token)
            Gitlab::Auth::Result.new(validation, nil, :personal_token, full_authentication_abilities)
          end
110 111
        end
      end
P
Patricio Cano 已提交
112

113 114 115 116
      def token_has_scope?(token)
        AccessTokenValidationService.sufficient_scope?(token, ['api'])
      end

117 118 119 120 121 122 123 124 125 126
      def lfs_token_check(login, password)
        deploy_key_matches = login.match(/\Alfs\+deploy-key-(\d+)\z/)

        actor =
          if deploy_key_matches
            DeployKey.find(deploy_key_matches[1])
          else
            User.by_login(login)
          end

127
        return unless actor
128

129
        token_handler = Gitlab::LfsToken.new(actor)
130

131 132 133 134 135 136 137
        authentication_abilities =
          if token_handler.user?
            full_authentication_abilities
          else
            read_authentication_abilities
          end

138
        Result.new(actor, nil, token_handler.type, authentication_abilities) if Devise.secure_compare(token_handler.token, password)
139 140
      end

141 142 143 144
      def build_access_token_check(login, password)
        return unless login == 'gitlab-ci-token'
        return unless password

145
        build = ::Ci::Build.running.find_by_token(password)
146
        return unless build
K
Kamil Trzcinski 已提交
147
        return unless build.project.builds_enabled?
148 149 150

        if build.user
          # If user is assigned to build, use restricted credentials of user
151
          Gitlab::Auth::Result.new(build.user, build.project, :build, build_authentication_abilities)
152 153
        else
          # Otherwise use generic CI credentials (backward compatibility)
154
          Gitlab::Auth::Result.new(nil, build.project, :ci, build_authentication_abilities)
155 156
        end
      end
157

158 159
      public

160
      def build_authentication_abilities
161 162
        [
          :read_project,
163 164 165
          :build_download_code,
          :build_read_container_image,
          :build_create_container_image
166 167 168
        ]
      end

169
      def read_authentication_abilities
170 171
        [
          :read_project,
172
          :download_code,
173 174 175 176
          :read_container_image
        ]
      end

177 178
      def full_authentication_abilities
        read_authentication_abilities + [
179
          :push_code,
180
          :create_container_image
181 182
        ]
      end
183
    end
D
Dmitriy Zaporozhets 已提交
184 185
  end
end