Created
March 27, 2009 20:41
-
-
Save elliottkember/86887 to your computer and use it in GitHub Desktop.
Simple OAuth in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map.with_options :controller => 'user_sessions' do |session| | |
session.callback '/callback', :action => 'callback' | |
session.logout '/logout', :action => 'destroy' | |
session.login '/login', :action => 'new' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
url = TwitterOauth.get_authentication_url(session) | |
redirect_to url | |
end | |
def callback | |
access_token = TwitterOauth.get_access_token(session) | |
screen_name = TwitterOauth.get_screen_name(access_token) | |
unless screen_name # Their login has failed. | |
flash[:error] = "Something went horribly wrong with Twitter's OAuth." | |
return redirect_to login_url | |
end | |
user = User.find_or_create_by_screen_name(screen_name) | |
user.token = access_token.token | |
user.secret = access_token.secret | |
if user.save | |
session[:user] = user.id | |
return redirect_to root_url | |
end | |
flash[:error] = "Something went horribly wrong with registration" | |
return redirect_to login_url | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TwitterOauth | |
class << self | |
def consumer | |
OAuth::Consumer.new("token", "secret", {:site=>"http://twitter.com"}) | |
end | |
def get_authentication_url(session) | |
# Get a request token, save the session variables that we need to keep | |
request_token = consumer.get_request_token | |
session['oauth_request_token_token'] = request_token.token | |
session['oauth_request_token_secret'] = request_token.secret | |
return request_token.authorize_url | |
end | |
def get_access_token(session) | |
# If we've already retrieved an access token | |
if session['access_token_token'] and session['access_token_secret'] | |
access_token = OAuth::AccessToken.new(consumer, session['access_token_token'], session['access_token_secret']) | |
else | |
request_token = OAuth::RequestToken.new(consumer, session['oauth_request_token_token'], session['oauth_request_token_secret']) | |
access_token = request_token.get_access_token | |
end | |
session['access_token_token'] = access_token.token | |
session['access_token_secret'] = access_token.secret | |
return access_token | |
end | |
def get_screen_name(access_token) | |
# Whoooooo are you? Who, who - who, who | |
response = consumer.request(:get, '/account/verify_credentials.json', access_token, { :scheme => :query_string }) | |
if response.code == "200" | |
user_info = JSON.parse(response.body) | |
return user_info['screen_name'] if user_info['screen_name'] | |
end | |
RAILS_DEFAULT_LOGGER.error "Failed to get user info via OAuth" | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment