/app/controllers/omniauth_callbacks_controller.rb

https://gitlab.com/solidnerd/gitlab-ce · Ruby · 192 lines · 145 code · 43 blank · 4 comment · 18 complexity · b5ac507291eb7accc94fdf04ac834963 MD5 · raw file

  1. # frozen_string_literal: true
  2. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  3. include AuthenticatesWithTwoFactor
  4. include Devise::Controllers::Rememberable
  5. protect_from_forgery except: [:kerberos, :saml, :cas3], prepend: true
  6. def handle_omniauth
  7. omniauth_flow(Gitlab::Auth::OAuth)
  8. end
  9. AuthHelper.providers_for_base_controller.each do |provider|
  10. alias_method provider, :handle_omniauth
  11. end
  12. # Extend the standard implementation to also increment
  13. # the number of failed sign in attempts
  14. def failure
  15. if params[:username].present? && AuthHelper.form_based_provider?(failed_strategy.name)
  16. user = User.by_login(params[:username])
  17. user&.increment_failed_attempts!
  18. end
  19. super
  20. end
  21. # Extend the standard message generation to accept our custom exception
  22. def failure_message
  23. exception = request.env["omniauth.error"]
  24. error = exception.error_reason if exception.respond_to?(:error_reason)
  25. error ||= exception.error if exception.respond_to?(:error)
  26. error ||= exception.message if exception.respond_to?(:message)
  27. error ||= request.env["omniauth.error.type"].to_s
  28. error.to_s.humanize if error
  29. end
  30. def saml
  31. omniauth_flow(Gitlab::Auth::Saml)
  32. end
  33. def omniauth_error
  34. @provider = params[:provider]
  35. @error = params[:error]
  36. render 'errors/omniauth_error', layout: "oauth_error", status: 422
  37. end
  38. def cas3
  39. ticket = params['ticket']
  40. if ticket
  41. handle_service_ticket oauth['provider'], ticket
  42. end
  43. handle_omniauth
  44. end
  45. def authentiq
  46. if params['sid']
  47. handle_service_ticket oauth['provider'], params['sid']
  48. end
  49. handle_omniauth
  50. end
  51. def auth0
  52. if oauth['uid'].blank?
  53. fail_auth0_login
  54. else
  55. handle_omniauth
  56. end
  57. end
  58. private
  59. def omniauth_flow(auth_module, identity_linker: nil)
  60. if current_user
  61. log_audit_event(current_user, with: oauth['provider'])
  62. identity_linker ||= auth_module::IdentityLinker.new(current_user, oauth)
  63. identity_linker.link
  64. if identity_linker.changed?
  65. redirect_identity_linked
  66. elsif identity_linker.failed?
  67. redirect_identity_link_failed(identity_linker.error_message)
  68. else
  69. redirect_identity_exists
  70. end
  71. else
  72. sign_in_user_flow(auth_module::User)
  73. end
  74. end
  75. def redirect_identity_exists
  76. redirect_to after_sign_in_path_for(current_user)
  77. end
  78. def redirect_identity_link_failed(error_message)
  79. redirect_to profile_account_path, notice: "Authentication failed: #{error_message}"
  80. end
  81. def redirect_identity_linked
  82. redirect_to profile_account_path, notice: 'Authentication method updated'
  83. end
  84. def handle_service_ticket(provider, ticket)
  85. Gitlab::Auth::OAuth::Session.create provider, ticket
  86. session[:service_tickets] ||= {}
  87. session[:service_tickets][provider] = ticket
  88. end
  89. def sign_in_user_flow(auth_user_class)
  90. auth_user = auth_user_class.new(oauth)
  91. user = auth_user.find_and_update!
  92. if auth_user.valid_sign_in?
  93. log_audit_event(user, with: oauth['provider'])
  94. set_remember_me(user)
  95. if user.two_factor_enabled? && !auth_user.bypass_two_factor?
  96. prompt_for_two_factor(user)
  97. else
  98. sign_in_and_redirect(user)
  99. end
  100. else
  101. fail_login(user)
  102. end
  103. rescue Gitlab::Auth::OAuth::User::SigninDisabledForProviderError
  104. handle_disabled_provider
  105. rescue Gitlab::Auth::OAuth::User::SignupDisabledError
  106. handle_signup_error
  107. end
  108. def handle_signup_error
  109. label = Gitlab::Auth::OAuth::Provider.label_for(oauth['provider'])
  110. message = ["Signing in using your #{label} account without a pre-existing GitLab account is not allowed."]
  111. if Gitlab::CurrentSettings.allow_signup?
  112. message << "Create a GitLab account first, and then connect it to your #{label} account."
  113. end
  114. flash[:notice] = message.join(' ')
  115. redirect_to new_user_session_path
  116. end
  117. def oauth
  118. @oauth ||= request.env['omniauth.auth']
  119. end
  120. def fail_login(user)
  121. error_message = user.errors.full_messages.to_sentence
  122. return redirect_to omniauth_error_path(oauth['provider'], error: error_message)
  123. end
  124. def fail_auth0_login
  125. flash[:alert] = 'Wrong extern UID provided. Make sure Auth0 is configured correctly.'
  126. redirect_to new_user_session_path
  127. end
  128. def handle_disabled_provider
  129. label = Gitlab::Auth::OAuth::Provider.label_for(oauth['provider'])
  130. flash[:alert] = "Signing in using #{label} has been disabled"
  131. redirect_to new_user_session_path
  132. end
  133. def log_audit_event(user, options = {})
  134. AuditEventService.new(user, user, options)
  135. .for_authentication.security_event
  136. end
  137. def set_remember_me(user)
  138. return unless remember_me?
  139. if user.two_factor_enabled?
  140. params[:remember_me] = '1'
  141. else
  142. remember_me(user)
  143. end
  144. end
  145. def remember_me?
  146. request_params = request.env['omniauth.params']
  147. (request_params['remember_me'] == '1') if request_params.present?
  148. end
  149. end