/app/controllers/omniauth_callbacks_controller.rb

https://gitlab.com/dandunckelman/gitlab-ce · Ruby · 158 lines · 127 code · 24 blank · 7 comment · 23 complexity · 0c7b69ee85d4091c9810631d363fba1d MD5 · raw file

  1. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  2. include AuthenticatesWithTwoFactor
  3. include Devise::Controllers::Rememberable
  4. protect_from_forgery except: [:kerberos, :saml, :cas3]
  5. Gitlab.config.omniauth.providers.each do |provider|
  6. define_method provider['name'] do
  7. handle_omniauth
  8. end
  9. end
  10. # Extend the standard message generation to accept our custom exception
  11. def failure_message
  12. exception = env["omniauth.error"]
  13. error = exception.error_reason if exception.respond_to?(:error_reason)
  14. error ||= exception.error if exception.respond_to?(:error)
  15. error ||= exception.message if exception.respond_to?(:message)
  16. error ||= env["omniauth.error.type"].to_s
  17. error.to_s.humanize if error
  18. end
  19. # We only find ourselves here
  20. # if the authentication to LDAP was successful.
  21. def ldap
  22. ldap_user = Gitlab::LDAP::User.new(oauth)
  23. ldap_user.save if ldap_user.changed? # will also save new users
  24. @user = ldap_user.gl_user
  25. @user.remember_me = params[:remember_me] if ldap_user.persisted?
  26. # Do additional LDAP checks for the user filter and EE features
  27. if ldap_user.allowed?
  28. if @user.two_factor_enabled?
  29. prompt_for_two_factor(@user)
  30. else
  31. log_audit_event(@user, with: :ldap)
  32. sign_in_and_redirect(@user)
  33. end
  34. else
  35. flash[:alert] = "Access denied for your LDAP account."
  36. redirect_to new_user_session_path
  37. end
  38. end
  39. def saml
  40. if current_user
  41. log_audit_event(current_user, with: :saml)
  42. # Update SAML identity if data has changed.
  43. identity = current_user.identities.find_by(extern_uid: oauth['uid'], provider: :saml)
  44. if identity.nil?
  45. current_user.identities.create(extern_uid: oauth['uid'], provider: :saml)
  46. redirect_to profile_account_path, notice: 'Authentication method updated'
  47. else
  48. redirect_to after_sign_in_path_for(current_user)
  49. end
  50. else
  51. saml_user = Gitlab::Saml::User.new(oauth)
  52. saml_user.save if saml_user.changed?
  53. @user = saml_user.gl_user
  54. continue_login_process
  55. end
  56. rescue Gitlab::OAuth::SignupDisabledError
  57. handle_signup_error
  58. end
  59. def omniauth_error
  60. @provider = params[:provider]
  61. @error = params[:error]
  62. render 'errors/omniauth_error', layout: "oauth_error", status: 422
  63. end
  64. def cas3
  65. ticket = params['ticket']
  66. if ticket
  67. handle_service_ticket oauth['provider'], ticket
  68. end
  69. handle_omniauth
  70. end
  71. def authentiq
  72. if params['sid']
  73. handle_service_ticket oauth['provider'], params['sid']
  74. end
  75. handle_omniauth
  76. end
  77. private
  78. def handle_omniauth
  79. if current_user
  80. # Add new authentication method
  81. current_user.identities.find_or_create_by(extern_uid: oauth['uid'], provider: oauth['provider'])
  82. log_audit_event(current_user, with: oauth['provider'])
  83. redirect_to profile_account_path, notice: 'Authentication method updated'
  84. else
  85. oauth_user = Gitlab::OAuth::User.new(oauth)
  86. oauth_user.save
  87. @user = oauth_user.gl_user
  88. continue_login_process
  89. end
  90. rescue Gitlab::OAuth::SignupDisabledError
  91. handle_signup_error
  92. end
  93. def handle_service_ticket(provider, ticket)
  94. Gitlab::OAuth::Session.create provider, ticket
  95. session[:service_tickets] ||= {}
  96. session[:service_tickets][provider] = ticket
  97. end
  98. def continue_login_process
  99. # Only allow properly saved users to login.
  100. if @user.persisted? && @user.valid?
  101. log_audit_event(@user, with: oauth['provider'])
  102. if @user.two_factor_enabled?
  103. params[:remember_me] = '1' if remember_me?
  104. prompt_for_two_factor(@user)
  105. else
  106. remember_me(@user) if remember_me?
  107. sign_in_and_redirect(@user)
  108. end
  109. else
  110. error_message = @user.errors.full_messages.to_sentence
  111. return redirect_to omniauth_error_path(oauth['provider'], error: error_message)
  112. end
  113. end
  114. def handle_signup_error
  115. label = Gitlab::OAuth::Provider.label_for(oauth['provider'])
  116. message = "Signing in using your #{label} account without a pre-existing GitLab account is not allowed."
  117. if current_application_settings.signup_enabled?
  118. message << " Create a GitLab account first, and then connect it to your #{label} account."
  119. end
  120. flash[:notice] = message
  121. redirect_to new_user_session_path
  122. end
  123. def oauth
  124. @oauth ||= request.env['omniauth.auth']
  125. end
  126. def log_audit_event(user, options = {})
  127. AuditEventService.new(user, user, options)
  128. .for_authentication.security_event
  129. end
  130. def remember_me?
  131. request_params = request.env['omniauth.params']
  132. (request_params['remember_me'] == '1') if request_params.present?
  133. end
  134. end