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-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-03-04 13:44:41 -05:00
|
|
|
# Make sure we support that provider
|
|
|
|
provider = params[:provider]
|
2013-04-09 01:55:39 -04:00
|
|
|
raise Discourse::InvalidAccess.new unless self.class.types.keys.map(&:to_s).include?(provider)
|
2013-03-04 13:44:41 -05:00
|
|
|
|
|
|
|
# Check if the provider is enabled
|
|
|
|
raise Discourse::InvalidAccess.new("provider is not enabled") unless SiteSetting.send("enable_#{provider}_logins?")
|
|
|
|
|
|
|
|
# Call the appropriate logic
|
|
|
|
send("create_or_sign_on_user_using_#{provider}", request.env["omniauth.auth"])
|
2013-03-01 14:22:54 -05:00
|
|
|
|
2013-06-05 14:11:02 -04:00
|
|
|
@data[:awaiting_approval] = true if invite_only?
|
|
|
|
|
2013-03-01 14:22:54 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2013-03-22 14:08:11 -04:00
|
|
|
format.json { render json: @data }
|
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-02-12 08:47:22 -05:00
|
|
|
def create_or_sign_on_user_using_twitter(auth_token)
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
screen_name = data["nickname"]
|
|
|
|
twitter_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
session[:authentication] = {
|
|
|
|
twitter_user_id: twitter_user_id,
|
|
|
|
twitter_screen_name: screen_name
|
|
|
|
}
|
|
|
|
|
2013-03-23 11:02:59 -04:00
|
|
|
user_info = TwitterUserInfo.where(twitter_user_id: twitter_user_id).first
|
2013-02-12 08:47:22 -05:00
|
|
|
|
|
|
|
@data = {
|
|
|
|
username: screen_name,
|
|
|
|
auth_provider: "Twitter"
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:02:18 -04:00
|
|
|
process_user_info(user_info, screen_name)
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_sign_on_user_using_facebook(auth_token)
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
raw_info = auth_token["extra"]["raw_info"]
|
|
|
|
|
|
|
|
email = data[:email]
|
|
|
|
name = data["name"]
|
|
|
|
fb_uid = auth_token["uid"]
|
2013-02-14 14:11:13 -05:00
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
username = UserNameSuggester.suggest(name)
|
2013-02-12 08:47:22 -05:00
|
|
|
|
|
|
|
session[:authentication] = {
|
|
|
|
facebook: {
|
2013-06-25 11:23:23 -04:00
|
|
|
facebook_user_id: fb_uid,
|
2013-02-12 08:47:22 -05:00
|
|
|
link: raw_info["link"],
|
|
|
|
username: raw_info["username"],
|
|
|
|
first_name: raw_info["first_name"],
|
|
|
|
last_name: raw_info["last_name"],
|
|
|
|
email: raw_info["email"],
|
|
|
|
gender: raw_info["gender"],
|
|
|
|
name: raw_info["name"]
|
|
|
|
},
|
|
|
|
email: email,
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
2013-03-23 11:02:59 -04:00
|
|
|
user_info = FacebookUserInfo.where(facebook_user_id: fb_uid).first
|
2013-02-12 08:47:22 -05:00
|
|
|
|
|
|
|
@data = {
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
email: email,
|
|
|
|
auth_provider: "Facebook",
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_info
|
2013-06-25 11:23:23 -04:00
|
|
|
if user = user_info.user
|
|
|
|
user.toggle(:active).save unless user.active?
|
2013-04-03 12:23:28 -04:00
|
|
|
|
|
|
|
# If we have to approve users
|
|
|
|
if Guardian.new(user).can_access_forum?
|
|
|
|
log_on_user(user)
|
|
|
|
@data[:authenticated] = true
|
|
|
|
else
|
|
|
|
@data[:awaiting_approval] = true
|
|
|
|
end
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
else
|
2013-06-25 11:23:23 -04:00
|
|
|
if user = User.where(email: email).first
|
|
|
|
user.create_facebook_user_info! session[:authentication][:facebook]
|
|
|
|
user.toggle(:active).save unless user.active?
|
2013-02-12 08:47:22 -05:00
|
|
|
log_on_user(user)
|
|
|
|
@data[:authenticated] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-23 16:40:50 -04:00
|
|
|
def create_or_sign_on_user_using_cas(auth_token)
|
|
|
|
logger.error "authtoken #{auth_token}"
|
2013-07-04 02:30:13 -04:00
|
|
|
|
|
|
|
email = auth_token[:info][:email] if auth_token[:info]
|
|
|
|
email ||= if SiteSetting.cas_domainname.present?
|
|
|
|
"#{auth_token[:extra][:user]}@#{SiteSetting.cas_domainname}"
|
|
|
|
else
|
|
|
|
auth_token[:extra][:user]
|
|
|
|
end
|
|
|
|
|
2013-05-23 16:40:50 -04:00
|
|
|
username = auth_token[:extra][:user]
|
2013-07-04 02:30:13 -04:00
|
|
|
|
|
|
|
name = if auth_token[:info] && auth_token[:info][:name]
|
|
|
|
auth_token[:info][:name]
|
|
|
|
else
|
|
|
|
auth_token["uid"]
|
|
|
|
end
|
|
|
|
|
2013-05-23 16:40:50 -04:00
|
|
|
cas_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
session[:authentication] = {
|
|
|
|
cas: {
|
|
|
|
cas_user_id: cas_user_id ,
|
|
|
|
username: username
|
|
|
|
},
|
|
|
|
email: email,
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
|
|
|
user_info = CasUserInfo.where(:cas_user_id => cas_user_id ).first
|
|
|
|
|
|
|
|
@data = {
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
email: email,
|
|
|
|
auth_provider: "CAS",
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_info
|
2013-06-25 11:23:23 -04:00
|
|
|
if user = user_info.user
|
|
|
|
user.toggle(:active).save unless user.active?
|
2013-05-23 16:40:50 -04:00
|
|
|
log_on_user(user)
|
|
|
|
@data[:authenticated] = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
user = User.where(email: email).first
|
|
|
|
if user
|
|
|
|
CasUserInfo.create!(session[:authentication][:cas].merge(user_id: user.id))
|
2013-06-25 11:23:23 -04:00
|
|
|
user.toggle(:active).save unless user.active?
|
2013-05-23 16:40:50 -04:00
|
|
|
log_on_user(user)
|
|
|
|
@data[:authenticated] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
def create_or_sign_on_user_using_openid(auth_token)
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
identity_url = auth_token[:extra][:identity_url]
|
|
|
|
|
|
|
|
email = data[:email]
|
|
|
|
|
2013-02-13 12:36:59 -05:00
|
|
|
# If the auth supplies a name / username, use those. Otherwise start with email.
|
|
|
|
name = data[:name] || data[:email]
|
|
|
|
username = data[:nickname] || data[:email]
|
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
user_open_id = UserOpenId.find_by_url(identity_url)
|
|
|
|
|
|
|
|
if user_open_id.blank? && user = User.find_by_email(email)
|
|
|
|
# we trust so do an email lookup
|
|
|
|
user_open_id = UserOpenId.create(url: identity_url , user_id: user.id, email: email, active: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
authenticated = user_open_id # if authed before
|
|
|
|
|
|
|
|
if authenticated
|
|
|
|
user = user_open_id.user
|
|
|
|
|
|
|
|
# If we have to approve users
|
2013-04-03 12:23:28 -04:00
|
|
|
if Guardian.new(user).can_access_forum?
|
2013-02-12 08:47:22 -05:00
|
|
|
log_on_user(user)
|
|
|
|
@data = {authenticated: true}
|
2013-04-03 12:23:28 -04:00
|
|
|
else
|
|
|
|
@data = {awaiting_approval: true}
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
@data = {
|
|
|
|
email: email,
|
2013-02-13 12:36:59 -05:00
|
|
|
name: User.suggest_name(name),
|
2013-06-06 10:40:10 -04:00
|
|
|
username: UserNameSuggester.suggest(username),
|
2013-02-12 08:47:22 -05:00
|
|
|
email_valid: true ,
|
2013-02-19 16:27:38 -05:00
|
|
|
auth_provider: data[:provider] || params[:provider].try(:capitalize)
|
2013-02-12 08:47:22 -05:00
|
|
|
}
|
|
|
|
session[:authentication] = {
|
|
|
|
email: @data[:email],
|
|
|
|
email_valid: @data[:email_valid],
|
|
|
|
openid_url: identity_url
|
|
|
|
}
|
|
|
|
end
|
2013-03-01 10:23:21 -05:00
|
|
|
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|
|
|
|
|
2013-03-04 13:44:41 -05:00
|
|
|
alias_method :create_or_sign_on_user_using_yahoo, :create_or_sign_on_user_using_openid
|
|
|
|
alias_method :create_or_sign_on_user_using_google, :create_or_sign_on_user_using_openid
|
|
|
|
|
2013-02-25 23:28:32 -05:00
|
|
|
def create_or_sign_on_user_using_github(auth_token)
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
screen_name = data["nickname"]
|
|
|
|
github_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
session[:authentication] = {
|
|
|
|
github_user_id: github_user_id,
|
|
|
|
github_screen_name: screen_name
|
|
|
|
}
|
|
|
|
|
2013-03-23 11:02:59 -04:00
|
|
|
user_info = GithubUserInfo.where(github_user_id: github_user_id).first
|
2013-02-25 23:28:32 -05:00
|
|
|
|
|
|
|
@data = {
|
|
|
|
username: screen_name,
|
|
|
|
auth_provider: "Github"
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:02:18 -04:00
|
|
|
process_user_info(user_info, screen_name)
|
2013-03-01 10:23:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_sign_on_user_using_persona(auth_token)
|
|
|
|
|
|
|
|
email = auth_token[:info][:email]
|
|
|
|
|
|
|
|
user = User.find_by_email(email)
|
|
|
|
|
|
|
|
if user
|
2013-04-03 12:23:28 -04:00
|
|
|
|
|
|
|
if Guardian.new(user).can_access_forum?
|
2013-03-01 10:23:21 -05:00
|
|
|
log_on_user(user)
|
|
|
|
@data = {authenticated: true}
|
2013-04-03 12:23:28 -04:00
|
|
|
else
|
|
|
|
@data = {awaiting_approval: true}
|
2013-03-01 10:23:21 -05:00
|
|
|
end
|
2013-04-03 12:23:28 -04:00
|
|
|
|
2013-03-01 10:23:21 -05:00
|
|
|
else
|
|
|
|
@data = {
|
|
|
|
email: email,
|
|
|
|
email_valid: true,
|
|
|
|
name: User.suggest_name(email),
|
2013-06-06 10:40:10 -04:00
|
|
|
username: UserNameSuggester.suggest(email),
|
2013-03-01 10:23:21 -05:00
|
|
|
auth_provider: params[:provider].try(:capitalize)
|
|
|
|
}
|
|
|
|
|
|
|
|
session[:authentication] = {
|
|
|
|
email: email,
|
|
|
|
email_valid: true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-25 23:28:32 -05:00
|
|
|
end
|
|
|
|
|
2013-06-05 14:11:02 -04:00
|
|
|
private
|
|
|
|
|
2013-07-11 02:02:18 -04:00
|
|
|
def process_user_info(user_info, screen_name)
|
|
|
|
if user_info
|
|
|
|
if user_info.user.active?
|
|
|
|
|
|
|
|
if Guardian.new(user_info.user).can_access_forum?
|
|
|
|
log_on_user(user_info.user)
|
|
|
|
@data[:authenticated] = true
|
|
|
|
else
|
|
|
|
@data[:awaiting_approval] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
@data[:awaiting_activation] = true
|
|
|
|
# send another email ?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@data[:name] = screen_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-05 14:11:02 -04:00
|
|
|
def invite_only?
|
|
|
|
SiteSetting.invite_only? && !@data[:authenticated]
|
|
|
|
end
|
2013-02-12 08:47:22 -05:00
|
|
|
end
|