/app/controllers/omniauth_callbacks_controller.rb

https://gitlab.com/twang2218/gitlab · Ruby · 186 lines · 149 code · 30 blank · 7 comment · 23 complexity · 42e7cd288b7512bd5741d7b2780f1645 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. if Gitlab::LDAP::Config.enabled?
  11. Gitlab::LDAP::Config.available_servers.each do |server|
  12. define_method server['provider_name'] do
  13. ldap
  14. end
  15. end
  16. end
  17. # Extend the standard message generation to accept our custom exception
  18. def failure_message
  19. exception = env["omniauth.error"]
  20. error = exception.error_reason if exception.respond_to?(:error_reason)
  21. error ||= exception.error if exception.respond_to?(:error)
  22. error ||= exception.message if exception.respond_to?(:message)
  23. error ||= env["omniauth.error.type"].to_s
  24. error.to_s.humanize if error
  25. end
  26. # We only find ourselves here
  27. # if the authentication to LDAP was successful.
  28. def ldap
  29. ldap_user = Gitlab::LDAP::User.new(oauth)
  30. ldap_user.save if ldap_user.changed? # will also save new users
  31. @user = ldap_user.gl_user
  32. @user.remember_me = params[:remember_me] if ldap_user.persisted?
  33. # Do additional LDAP checks for the user filter and EE features
  34. if ldap_user.allowed?
  35. if @user.two_factor_enabled?
  36. prompt_for_two_factor(@user)
  37. else
  38. log_audit_event(@user, with: oauth['provider'])
  39. sign_in_and_redirect(@user)
  40. end
  41. else
  42. fail_ldap_login
  43. end
  44. end
  45. def saml
  46. if current_user
  47. log_audit_event(current_user, with: :saml)
  48. # Update SAML identity if data has changed.
  49. identity = current_user.identities.with_extern_uid(:saml, oauth['uid']).take
  50. if identity.nil?
  51. current_user.identities.create(extern_uid: oauth['uid'], provider: :saml)
  52. redirect_to profile_account_path, notice: '认证方法已更新'
  53. else
  54. redirect_to after_sign_in_path_for(current_user)
  55. end
  56. else
  57. saml_user = Gitlab::Saml::User.new(oauth)
  58. saml_user.save if saml_user.changed?
  59. @user = saml_user.gl_user
  60. continue_login_process
  61. end
  62. rescue Gitlab::OAuth::SignupDisabledError
  63. handle_signup_error
  64. end
  65. def omniauth_error
  66. @provider = params[:provider]
  67. @error = params[:error]
  68. render 'errors/omniauth_error', layout: "oauth_error", status: 422
  69. end
  70. def cas3
  71. ticket = params['ticket']
  72. if ticket
  73. handle_service_ticket oauth['provider'], ticket
  74. end
  75. handle_omniauth
  76. end
  77. def authentiq
  78. if params['sid']
  79. handle_service_ticket oauth['provider'], params['sid']
  80. end
  81. handle_omniauth
  82. end
  83. private
  84. def handle_omniauth
  85. if current_user
  86. # Add new authentication method
  87. current_user.identities
  88. .with_extern_uid(oauth['provider'], oauth['uid'])
  89. .first_or_create(extern_uid: oauth['uid'])
  90. log_audit_event(current_user, with: oauth['provider'])
  91. redirect_to profile_account_path, notice: '认证方法已更新'
  92. else
  93. oauth_user = Gitlab::OAuth::User.new(oauth)
  94. oauth_user.save
  95. @user = oauth_user.gl_user
  96. continue_login_process
  97. end
  98. rescue Gitlab::OAuth::SigninDisabledForProviderError
  99. handle_disabled_provider
  100. rescue Gitlab::OAuth::SignupDisabledError
  101. handle_signup_error
  102. end
  103. def handle_service_ticket(provider, ticket)
  104. Gitlab::OAuth::Session.create provider, ticket
  105. session[:service_tickets] ||= {}
  106. session[:service_tickets][provider] = ticket
  107. end
  108. def continue_login_process
  109. # Only allow properly saved users to login.
  110. if @user.persisted? && @user.valid?
  111. log_audit_event(@user, with: oauth['provider'])
  112. if @user.two_factor_enabled?
  113. params[:remember_me] = '1' if remember_me?
  114. prompt_for_two_factor(@user)
  115. else
  116. remember_me(@user) if remember_me?
  117. sign_in_and_redirect(@user)
  118. end
  119. else
  120. fail_login
  121. end
  122. end
  123. def handle_signup_error
  124. label = Gitlab::OAuth::Provider.label_for(oauth['provider'])
  125. message = "没有绑定 GitLab 账号前是无法使用 #{label} 账号登陆系统。"
  126. if current_application_settings.allow_signup?
  127. message << "请先创建一个 GitLab 账号,然后再绑定 #{label} 账号。"
  128. end
  129. flash[:notice] = message
  130. redirect_to new_user_session_path
  131. end
  132. def oauth
  133. @oauth ||= request.env['omniauth.auth']
  134. end
  135. def fail_login
  136. error_message = @user.errors.full_messages.to_sentence
  137. return redirect_to omniauth_error_path(oauth['provider'], error: error_message)
  138. end
  139. def fail_ldap_login
  140. flash[:alert] = '您的 LDAP 账号被禁止访问。'
  141. redirect_to new_user_session_path
  142. end
  143. def handle_disabled_provider
  144. label = Gitlab::OAuth::Provider.label_for(oauth['provider'])
  145. flash[:alert] = "Signing in using #{label} has been disabled"
  146. redirect_to new_user_session_path
  147. end
  148. def log_audit_event(user, options = {})
  149. AuditEventService.new(user, user, options)
  150. .for_authentication.security_event
  151. end
  152. def remember_me?
  153. request_params = request.env['omniauth.params']
  154. (request_params['remember_me'] == '1') if request_params.present?
  155. end
  156. end