PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gitlab/auth.rb

https://gitlab.com/Ben305/gitlab-ce
Ruby | 255 lines | 177 code | 54 blank | 24 comment | 32 complexity | 1a6b96232df33163e9241a64ab86ec26 MD5 | raw file
  1. module Gitlab
  2. module Auth
  3. MissingPersonalAccessTokenError = Class.new(StandardError)
  4. REGISTRY_SCOPES = [:read_registry].freeze
  5. # Scopes used for GitLab API access
  6. API_SCOPES = [:api, :read_user, :sudo].freeze
  7. # Scopes used for OpenID Connect
  8. OPENID_SCOPES = [:openid].freeze
  9. # Default scopes for OAuth applications that don't define their own
  10. DEFAULT_SCOPES = [:api].freeze
  11. class << self
  12. include Gitlab::CurrentSettings
  13. def find_for_git_client(login, password, project:, ip:)
  14. raise "Must provide an IP for rate limiting" if ip.nil?
  15. # `user_with_password_for_git` should be the last check
  16. # because it's the most expensive, especially when LDAP
  17. # is enabled.
  18. result =
  19. service_request_check(login, password, project) ||
  20. build_access_token_check(login, password) ||
  21. lfs_token_check(login, password, project) ||
  22. oauth_access_token_check(login, password) ||
  23. personal_access_token_check(password) ||
  24. user_with_password_for_git(login, password) ||
  25. Gitlab::Auth::Result.new
  26. rate_limit!(ip, success: result.success?, login: login)
  27. Gitlab::Auth::UniqueIpsLimiter.limit_user!(result.actor)
  28. return result if result.success? || authenticate_using_internal_or_ldap_password?
  29. # If sign-in is disabled and LDAP is not configured, recommend a
  30. # personal access token on failed auth attempts
  31. raise Gitlab::Auth::MissingPersonalAccessTokenError
  32. end
  33. def find_with_user_password(login, password)
  34. # Avoid resource intensive login checks if password is not provided
  35. return unless password.present?
  36. # Nothing to do here if internal auth is disabled and LDAP is
  37. # not configured
  38. return unless authenticate_using_internal_or_ldap_password?
  39. Gitlab::Auth::UniqueIpsLimiter.limit_user! do
  40. user = User.by_login(login)
  41. # If no user is found, or it's an LDAP server, try LDAP.
  42. # LDAP users are only authenticated via LDAP
  43. if user.nil? || user.ldap_user?
  44. # Second chance - try LDAP authentication
  45. Gitlab::LDAP::Authentication.login(login, password)
  46. elsif current_application_settings.password_authentication_enabled_for_git?
  47. user if user.active? && user.valid_password?(password)
  48. end
  49. end
  50. end
  51. def rate_limit!(ip, success:, login:)
  52. rate_limiter = Gitlab::Auth::IpRateLimiter.new(ip)
  53. return unless rate_limiter.enabled?
  54. if success
  55. # Repeated login 'failures' are normal behavior for some Git clients so
  56. # it is important to reset the ban counter once the client has proven
  57. # they are not a 'bad guy'.
  58. rate_limiter.reset!
  59. else
  60. # Register a login failure so that Rack::Attack can block the next
  61. # request from this IP if needed.
  62. rate_limiter.register_fail!
  63. if rate_limiter.banned?
  64. Rails.logger.info "IP #{ip} failed to login " \
  65. "as #{login} but has been temporarily banned from Git auth"
  66. end
  67. end
  68. end
  69. private
  70. def authenticate_using_internal_or_ldap_password?
  71. current_application_settings.password_authentication_enabled_for_git? || Gitlab::LDAP::Config.enabled?
  72. end
  73. def service_request_check(login, password, project)
  74. matched_login = /(?<service>^[a-zA-Z]*-ci)-token$/.match(login)
  75. return unless project && matched_login.present?
  76. underscored_service = matched_login['service'].underscore
  77. if Service.available_services_names.include?(underscored_service)
  78. # We treat underscored_service as a trusted input because it is included
  79. # in the Service.available_services_names whitelist.
  80. service = project.public_send("#{underscored_service}_service") # rubocop:disable GitlabSecurity/PublicSend
  81. if service && service.activated? && service.valid_token?(password)
  82. Gitlab::Auth::Result.new(nil, project, :ci, build_authentication_abilities)
  83. end
  84. end
  85. end
  86. def user_with_password_for_git(login, password)
  87. user = find_with_user_password(login, password)
  88. return unless user
  89. raise Gitlab::Auth::MissingPersonalAccessTokenError if user.two_factor_enabled?
  90. Gitlab::Auth::Result.new(user, nil, :gitlab_or_ldap, full_authentication_abilities)
  91. end
  92. def oauth_access_token_check(login, password)
  93. if login == "oauth2" && password.present?
  94. token = Doorkeeper::AccessToken.by_token(password)
  95. if valid_oauth_token?(token)
  96. user = User.find_by(id: token.resource_owner_id)
  97. Gitlab::Auth::Result.new(user, nil, :oauth, full_authentication_abilities)
  98. end
  99. end
  100. end
  101. def personal_access_token_check(password)
  102. return unless password.present?
  103. token = PersonalAccessTokensFinder.new(state: 'active').find_by(token: password)
  104. if token && valid_scoped_token?(token, available_scopes)
  105. Gitlab::Auth::Result.new(token.user, nil, :personal_access_token, abilities_for_scopes(token.scopes))
  106. end
  107. end
  108. def valid_oauth_token?(token)
  109. token && token.accessible? && valid_scoped_token?(token, [:api])
  110. end
  111. def valid_scoped_token?(token, scopes)
  112. AccessTokenValidationService.new(token).include_any_scope?(scopes)
  113. end
  114. def abilities_for_scopes(scopes)
  115. abilities_by_scope = {
  116. api: full_authentication_abilities,
  117. read_registry: [:read_container_image]
  118. }
  119. scopes.flat_map do |scope|
  120. abilities_by_scope.fetch(scope.to_sym, [])
  121. end.uniq
  122. end
  123. def lfs_token_check(login, password, project)
  124. deploy_key_matches = login.match(/\Alfs\+deploy-key-(\d+)\z/)
  125. actor =
  126. if deploy_key_matches
  127. DeployKey.find(deploy_key_matches[1])
  128. else
  129. User.by_login(login)
  130. end
  131. return unless actor
  132. token_handler = Gitlab::LfsToken.new(actor)
  133. authentication_abilities =
  134. if token_handler.user?
  135. full_authentication_abilities
  136. elsif token_handler.deploy_key_pushable?(project)
  137. read_write_authentication_abilities
  138. else
  139. read_authentication_abilities
  140. end
  141. if Devise.secure_compare(token_handler.token, password)
  142. Gitlab::Auth::Result.new(actor, nil, token_handler.type, authentication_abilities)
  143. end
  144. end
  145. def build_access_token_check(login, password)
  146. return unless login == 'gitlab-ci-token'
  147. return unless password
  148. build = ::Ci::Build.running.find_by_token(password)
  149. return unless build
  150. return unless build.project.builds_enabled?
  151. if build.user
  152. # If user is assigned to build, use restricted credentials of user
  153. Gitlab::Auth::Result.new(build.user, build.project, :build, build_authentication_abilities)
  154. else
  155. # Otherwise use generic CI credentials (backward compatibility)
  156. Gitlab::Auth::Result.new(nil, build.project, :ci, build_authentication_abilities)
  157. end
  158. end
  159. public
  160. def build_authentication_abilities
  161. [
  162. :read_project,
  163. :build_download_code,
  164. :build_read_container_image,
  165. :build_create_container_image
  166. ]
  167. end
  168. def read_authentication_abilities
  169. [
  170. :read_project,
  171. :download_code,
  172. :read_container_image
  173. ]
  174. end
  175. def read_write_authentication_abilities
  176. read_authentication_abilities + [
  177. :push_code,
  178. :create_container_image
  179. ]
  180. end
  181. def full_authentication_abilities
  182. read_write_authentication_abilities + [
  183. :admin_container_image
  184. ]
  185. end
  186. def available_scopes(current_user = nil)
  187. scopes = API_SCOPES + registry_scopes
  188. scopes.delete(:sudo) if current_user && !current_user.admin?
  189. scopes
  190. end
  191. # Other available scopes
  192. def optional_scopes
  193. available_scopes + OPENID_SCOPES - DEFAULT_SCOPES
  194. end
  195. def registry_scopes
  196. return [] unless Gitlab.config.registry.enabled
  197. REGISTRY_SCOPES
  198. end
  199. end
  200. end
  201. end