discourse/app/controllers/twitter_controller.rb

86 lines
2.4 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class TwitterController < ApplicationController
skip_before_filter :check_xhr, only: [:frame, :complete]
layout false
2013-02-07 10:45:24 -05:00
def frame
2013-02-05 14:16:51 -05:00
# defer the require as late as possible
require 'oauth'
consumer = oauth_consumer
2013-02-07 10:45:24 -05:00
host = request.host
2013-02-05 14:16:51 -05:00
host = "#{host}:#{request.port}" if request.port != 80
request_token = consumer.get_request_token(:oauth_callback => "http://#{host}/twitter/complete")
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to request_token.authorize_url
end
def complete
require 'oauth'
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
consumer = oauth_consumer
2013-02-07 10:45:24 -05:00
unless session[:request_token] && session[:request_token_secret]
2013-02-05 14:16:51 -05:00
render :text => ('No authentication information was found in the session. Please try again.') and return
end
unless params[:oauth_token].blank? || session[:request_token] == params[:oauth_token]
render :text => ('Authentication information does not match session information. Please try again.') and return
end
request_token = OAuth::RequestToken.new(consumer, session[:request_token], session[:request_token_secret])
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
screen_name = access_token.params["screen_name"]
twitter_user_id = access_token.params["user_id"]
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
session[:authentication] = {
twitter_user_id: twitter_user_id,
twitter_screen_name: screen_name
}
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
user_info = TwitterUserInfo.where(:twitter_user_id => twitter_user_id).first
@data = {
username: screen_name,
auth_provider: "Twitter"
}
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
if user_info
if user_info.user.active
log_on_user(user_info.user)
@data[:authenticated] = true
else
@data[:awaiting_activation] = true
2013-02-07 10:45:24 -05:00
# send another email ?
2013-02-05 14:16:51 -05:00
end
else
#TODO typheous or some other webscale http request lib that does not block thins
require 'open-uri'
parsed = ::JSON.parse(open("http://api.twitter.com/1/users/show.json?screen_name=#{screen_name}").read)
@data[:name] = parsed["name"]
end
end
2013-02-07 10:45:24 -05:00
protected
2013-02-05 14:16:51 -05:00
def oauth_consumer
OAuth::Consumer.new(
2013-02-07 10:45:24 -05:00
SiteSetting.twitter_consumer_key,
2013-02-05 14:16:51 -05:00
SiteSetting.twitter_consumer_secret,
:site => "https://api.twitter.com",
:authorize_path => '/oauth/authenticate'
)
end
end