2013-02-12 08:47:22 -05:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
require_dependency 'email'
|
2013-03-04 13:44:41 -05:00
|
|
|
require_dependency 'enum'
|
2013-06-06 10:40:10 -04:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-03-04 13:44:41 -05:00
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
class Users::OmniauthCallbacksController < ApplicationController
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
BUILTIN_AUTH = [
|
|
|
|
Auth::FacebookAuthenticator.new,
|
2013-08-25 21:04:16 -04:00
|
|
|
Auth::OpenIdAuthenticator.new("google", "https://www.google.com/accounts/o8/id", trusted: true),
|
2014-05-21 18:19:40 -04:00
|
|
|
Auth::GoogleOAuth2Authenticator.new,
|
2013-08-25 21:04:16 -04:00
|
|
|
Auth::OpenIdAuthenticator.new("yahoo", "https://me.yahoo.com", trusted: true),
|
2013-08-23 02:20:43 -04:00
|
|
|
Auth::GithubAuthenticator.new,
|
2014-02-11 20:25:54 -05:00
|
|
|
Auth::TwitterAuthenticator.new
|
2013-08-23 02:20:43 -04:00
|
|
|
]
|
|
|
|
|
2013-06-04 20:30:51 -04:00
|
|
|
skip_before_filter :redirect_to_login_if_required
|
2013-02-12 08:47:22 -05:00
|
|
|
|
|
|
|
layout false
|
|
|
|
|
2013-03-04 13:44:41 -05:00
|
|
|
def self.types
|
2013-05-23 16:40:50 -04:00
|
|
|
@types ||= Enum.new(:facebook, :twitter, :google, :yahoo, :github, :persona, :cas)
|
2013-03-04 13:44:41 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
# need to be able to call this
|
|
|
|
skip_before_filter :check_xhr
|
|
|
|
|
2013-07-29 01:13:13 -04: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 11:02:59 -04:00
|
|
|
skip_before_filter :verify_authenticity_token, only: :complete
|
2013-02-12 08:47:22 -05:00
|
|
|
|
|
|
|
def complete
|
2013-08-23 02:20:43 -04:00
|
|
|
auth = request.env["omniauth.auth"]
|
2013-11-19 12:58:12 -05:00
|
|
|
auth[:session] = session
|
2013-08-01 01:59:57 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
authenticator = self.class.find_authenticator(params[:provider])
|
2013-03-04 13:44:41 -05:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
@data = authenticator.after_authenticate(auth)
|
|
|
|
@data.authenticator_name = authenticator.name
|
2013-08-01 01:59:57 -04:00
|
|
|
|
2013-08-28 03:18:31 -04:00
|
|
|
if @data.user
|
|
|
|
user_found(@data.user)
|
|
|
|
elsif SiteSetting.invite_only?
|
|
|
|
@data.requires_invite = true
|
|
|
|
else
|
|
|
|
session[:authentication] = @data.session_data
|
|
|
|
end
|
2013-06-05 14:11:02 -04:00
|
|
|
|
2013-03-01 14:22:54 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2013-09-23 12:46:25 -04:00
|
|
|
format.json { render json: @data.to_client_hash }
|
2013-03-01 14:22:54 -05:00
|
|
|
end
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
|
2013-02-14 14:11:13 -05:00
|
|
|
def failure
|
|
|
|
flash[:error] = I18n.t("login.omniauth_error", strategy: params[:strategy].titleize)
|
2013-03-22 14:08:11 -04:00
|
|
|
render layout: 'no_js'
|
2013-02-14 14:11:13 -05:00
|
|
|
end
|
|
|
|
|
2013-08-18 00:43:59 -04:00
|
|
|
|
2013-08-23 02:20:43 -04: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-18 00:43:59 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-12 08:47:22 -05:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
Discourse.auth_providers.each do |provider|
|
2013-11-01 18:57:50 -04:00
|
|
|
return provider.authenticator if provider.name == name
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
2013-03-01 10:23:21 -05:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
raise Discourse::InvalidAccess.new("provider is not found")
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
protected
|
2013-02-25 23:28:32 -05:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
def user_found(user)
|
|
|
|
# automatically activate any account if a provider marked the email valid
|
|
|
|
if !user.active && @data.email_valid
|
|
|
|
user.toggle(:active).save
|
2013-08-01 22:03:53 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
# log on any account that is active with forum access
|
|
|
|
if Guardian.new(user).can_access_forum? && user.active
|
|
|
|
log_on_user(user)
|
2014-01-21 16:53:46 -05: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
|
2013-08-23 02:20:43 -04:00
|
|
|
@data.authenticated = true
|
2013-03-01 10:23:21 -05:00
|
|
|
else
|
2013-08-28 03:18:31 -04:00
|
|
|
if SiteSetting.must_approve_users? && !user.approved?
|
2013-08-23 02:20:43 -04:00
|
|
|
@data.awaiting_approval = true
|
2013-07-11 02:02:18 -04:00
|
|
|
else
|
2013-08-23 02:20:43 -04:00
|
|
|
@data.awaiting_activation = true
|
2013-07-11 02:02:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|