2013-02-12 07:47:22 -06:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
require_dependency 'email'
|
2013-03-04 12:44:41 -06:00
|
|
|
require_dependency 'enum'
|
2013-06-06 09:40:10 -05:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-03-04 12:44:41 -06:00
|
|
|
|
2013-02-12 07:47:22 -06:00
|
|
|
class Users::OmniauthCallbacksController < ApplicationController
|
2013-08-23 01:20:43 -05:00
|
|
|
|
|
|
|
BUILTIN_AUTH = [
|
|
|
|
Auth::FacebookAuthenticator.new,
|
2014-05-21 17:19:40 -05:00
|
|
|
Auth::GoogleOAuth2Authenticator.new,
|
2013-08-25 20:04:16 -05:00
|
|
|
Auth::OpenIdAuthenticator.new("yahoo", "https://me.yahoo.com", trusted: true),
|
2013-08-23 01:20:43 -05:00
|
|
|
Auth::GithubAuthenticator.new,
|
2016-02-24 19:21:59 -06:00
|
|
|
Auth::TwitterAuthenticator.new,
|
|
|
|
Auth::InstagramAuthenticator.new
|
2013-08-23 01:20:43 -05:00
|
|
|
]
|
|
|
|
|
2013-06-04 19:30:51 -05:00
|
|
|
skip_before_filter :redirect_to_login_if_required
|
2013-02-12 07:47:22 -06:00
|
|
|
|
|
|
|
layout false
|
|
|
|
|
2013-03-04 12:44:41 -06:00
|
|
|
def self.types
|
2016-02-24 19:21:59 -06:00
|
|
|
@types ||= Enum.new(:facebook, :instagram, :twitter, :google, :yahoo, :github, :persona, :cas)
|
2013-03-04 12:44:41 -06:00
|
|
|
end
|
|
|
|
|
2013-02-12 07:47:22 -06:00
|
|
|
# need to be able to call this
|
|
|
|
skip_before_filter :check_xhr
|
|
|
|
|
2013-07-29 00:13:13 -05:00
|
|
|
# this is the only spot where we allow CSRF, our openid / oauth redirect
|
|
|
|
# will not have a CSRF token, however the payload is all validated so its safe
|
2013-03-23 10:02:59 -05:00
|
|
|
skip_before_filter :verify_authenticity_token, only: :complete
|
2013-02-12 07:47:22 -06:00
|
|
|
|
|
|
|
def complete
|
2013-08-23 01:20:43 -05:00
|
|
|
auth = request.env["omniauth.auth"]
|
2013-11-19 11:58:12 -06:00
|
|
|
auth[:session] = session
|
2013-08-01 00:59:57 -05:00
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
authenticator = self.class.find_authenticator(params[:provider])
|
2015-10-12 22:49:09 -05:00
|
|
|
provider = Discourse.auth_providers && Discourse.auth_providers.find{|p| p.name == params[:provider]}
|
2013-03-04 12:44:41 -06:00
|
|
|
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result = authenticator.after_authenticate(auth)
|
2013-08-01 00:59:57 -05:00
|
|
|
|
2015-10-12 20:23:34 -05:00
|
|
|
origin = request.env['omniauth.origin']
|
|
|
|
if origin.present?
|
|
|
|
parsed = URI.parse(@origin) rescue nil
|
|
|
|
if parsed
|
|
|
|
@origin = parsed.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @origin.present?
|
|
|
|
@origin = Discourse.base_uri("/")
|
|
|
|
end
|
|
|
|
|
2015-06-24 11:12:43 -05:00
|
|
|
if @auth_result.failed?
|
|
|
|
flash[:error] = @auth_result.failed_reason.html_safe
|
|
|
|
return render('failure')
|
|
|
|
else
|
|
|
|
@auth_result.authenticator_name = authenticator.name
|
|
|
|
complete_response_data
|
2015-10-12 22:49:09 -05:00
|
|
|
|
|
|
|
if provider && provider.full_screen_login
|
2015-10-28 16:16:56 -05:00
|
|
|
cookies['_bypass_cache'] = true
|
2015-10-12 22:49:09 -05:00
|
|
|
flash[:authentication_data] = @auth_result.to_client_hash.to_json
|
|
|
|
redirect_to @origin
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json { render json: @auth_result.to_client_hash }
|
|
|
|
end
|
2015-06-24 11:12:43 -05:00
|
|
|
end
|
2013-03-01 13:22:54 -06:00
|
|
|
end
|
2013-02-12 07:47:22 -06:00
|
|
|
end
|
|
|
|
|
2013-02-14 13:11:13 -06:00
|
|
|
def failure
|
2015-01-05 23:28:29 -06:00
|
|
|
flash[:error] = I18n.t("login.omniauth_error")
|
2015-01-15 14:56:53 -06:00
|
|
|
render layout: 'no_ember'
|
2013-02-14 13:11:13 -06:00
|
|
|
end
|
|
|
|
|
2013-08-17 23:43:59 -05:00
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
def self.find_authenticator(name)
|
|
|
|
BUILTIN_AUTH.each do |authenticator|
|
|
|
|
if authenticator.name == name
|
|
|
|
raise Discourse::InvalidAccess.new("provider is not enabled") unless SiteSetting.send("enable_#{name}_logins?")
|
|
|
|
return authenticator
|
2013-08-17 23:43:59 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-12 07:47:22 -06:00
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
Discourse.auth_providers.each do |provider|
|
2013-11-01 17:57:50 -05:00
|
|
|
return provider.authenticator if provider.name == name
|
2013-02-12 07:47:22 -06:00
|
|
|
end
|
2013-03-01 09:23:21 -06:00
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
raise Discourse::InvalidAccess.new("provider is not found")
|
2013-02-12 07:47:22 -06:00
|
|
|
end
|
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
protected
|
2013-02-25 22:28:32 -06:00
|
|
|
|
2014-09-30 11:24:22 -05:00
|
|
|
def complete_response_data
|
2015-06-24 11:12:43 -05:00
|
|
|
if @auth_result.user
|
|
|
|
user_found(@auth_result.user)
|
2014-09-30 11:24:22 -05:00
|
|
|
elsif SiteSetting.invite_only?
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.requires_invite = true
|
2014-09-30 11:24:22 -05:00
|
|
|
else
|
2015-06-24 11:12:43 -05:00
|
|
|
session[:authentication] = @auth_result.session_data
|
2014-09-30 11:24:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-23 01:20:43 -05:00
|
|
|
def user_found(user)
|
|
|
|
# automatically activate any account if a provider marked the email valid
|
2015-06-24 11:12:43 -05:00
|
|
|
if !user.active && @auth_result.email_valid
|
2013-08-23 01:20:43 -05:00
|
|
|
user.toggle(:active).save
|
2013-08-01 21:03:53 -05:00
|
|
|
end
|
|
|
|
|
2015-03-02 11:13:10 -06:00
|
|
|
if ScreenedIpAddress.should_block?(request.remote_ip)
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.not_allowed_from_ip_address = true
|
2015-03-02 11:13:10 -06:00
|
|
|
elsif ScreenedIpAddress.block_admin_login?(user, request.remote_ip)
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.admin_not_allowed_from_ip_address = true
|
2014-09-04 17:50:27 -05:00
|
|
|
elsif Guardian.new(user).can_access_forum? && user.active # log on any account that is active with forum access
|
2013-08-23 01:20:43 -05:00
|
|
|
log_on_user(user)
|
2014-01-21 15:53:46 -06:00
|
|
|
Invite.invalidate_for_email(user.email) # invite link can't be used to log in anymore
|
|
|
|
session[:authentication] = nil # don't carry around old auth info, perhaps move elsewhere
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.authenticated = true
|
2013-03-01 09:23:21 -06:00
|
|
|
else
|
2013-08-28 02:18:31 -05:00
|
|
|
if SiteSetting.must_approve_users? && !user.approved?
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.awaiting_approval = true
|
2013-07-11 01:02:18 -05:00
|
|
|
else
|
2015-06-24 11:12:43 -05:00
|
|
|
@auth_result.awaiting_activation = true
|
2013-07-11 01:02:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 07:47:22 -06:00
|
|
|
end
|