Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We're moving right along here...OAuth is essentially complete. Seriou…
…sly.
- Loading branch information
Michael Bleigh
committed
Mar 18, 2009
1 parent
fa9b42c
commit c34d3f4
Showing
15 changed files
with
309 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TwitterAuth Generator | ||
===================== | ||
|
||
The TwitterAuth generator allows you to generate the components necessary to implement Twitter as a Single Sign-On provider for your site. | ||
|
||
To run it, you simply need to call it: | ||
|
||
script/generate twitter_auth | ||
|
||
This will generate the migration necessary for the users table as well as generate a User model that extends the appropriate TwitterAuth model template and a config/twitter.yml that allows you to set your OAuth consumer key and secret. | ||
|
||
By default, TwitterAuth uses OAuth as its authentication strategy. If you wish to use HTTP Basic you can pass in the --basic option. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<% if options[:oauth] -%> | ||
development: | ||
strategy: oauth | ||
oauth_consumer_key: devkey | ||
oauth_consumer_secret: devsecret | ||
oauth_callback: "http://localhost:3000/oauth_callback" | ||
test: | ||
strategy: oauth | ||
oauth_consumer_key: testkey | ||
oauth_consumer_secret: testsecret | ||
oauth_callback: "http://localhost:3000/oauth_callback" | ||
production: | ||
strategy: oauth | ||
oauth_consumer_key: prodkey | ||
oauth_consumer_secret: prodsecret | ||
<% else -%> | ||
development: | ||
strategy: basic | ||
test: | ||
strategy: basic | ||
production: | ||
strategy: basic | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class User < TwitterAuth::<%= options[:oauth] ? "Oauth" : "Basic" %>User | ||
# Extend and define your user model as you see fit. | ||
# All of the authentication logic is handled by the | ||
# parent TwitterAuth user class. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class TwitterAuthGenerator < Rails::Generator::Base | ||
default_options :oauth => true, :basic => false | ||
|
||
def manifest | ||
record do |m| | ||
m.class_collisions 'User' | ||
|
||
m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => 'twitter_auth_migration' | ||
m.template 'user.rb', File.join('app','models','user.rb') | ||
end | ||
end | ||
|
||
protected | ||
|
||
def banner | ||
"Usage: #{$0} twitter_auth" | ||
end | ||
|
||
def add_options!(opt) | ||
opt.separator '' | ||
opt.separator 'Options:' | ||
|
||
opt.on('-O', '--oauth', 'Use the OAuth authentication strategy to connect to Twitter. (default)') { |v| | ||
options[:oauth] = v | ||
options[:basic] = !v | ||
} | ||
|
||
opt.on('-B', '--basic', 'Use the HTTP Basic authentication strategy to connect to Twitter.') { |v| | ||
options[:basic] = v | ||
options[:oauth] = !v | ||
} | ||
end | ||
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
require 'json' | ||
require 'twitter_auth' | ||
|
||
RAILS_DEFAULT_LOGGER.info("** TwitterAuth initialized properly.") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
ActionController::Routing::Routes.draw do |map| | ||
map.connect ':controller/:action/:id' | ||
end | ||
|
||
class TwitterAuthTestController < ApplicationController | ||
before_filter :login_required, :only => [:login_required_action] | ||
|
||
def login_required_action | ||
render :text => "You are logged in!" | ||
end | ||
|
||
def fail_auth | ||
authentication_failed('Auth FAIL.') | ||
end | ||
|
||
def pass_auth | ||
if params[:message] | ||
authentication_succeeded(params[:message]) | ||
else | ||
authentication_succeeded | ||
end | ||
end | ||
|
||
def access_denied_action | ||
access_denied | ||
end | ||
|
||
def redirect_back_action | ||
redirect_back_or_default(params[:to] || '/') | ||
end | ||
end | ||
|
||
describe TwitterAuthTestController do | ||
%w(authentication_failed authentication_succeeded current_user authorized? login_required access_denied store_location redirect_back_or_default).each do |m| | ||
it "should respond to the extension method '#{m}'" do | ||
controller.should respond_to(m) | ||
end | ||
end | ||
|
||
describe "#authentication_failed" do | ||
it 'should set the flash[:error] to the message passed in' do | ||
get :fail_auth | ||
flash[:error].should == 'Auth FAIL.' | ||
end | ||
|
||
it 'should redirect to the root' do | ||
get :fail_auth | ||
should redirect_to('/') | ||
end | ||
end | ||
|
||
describe "#authentication_succeeded" do | ||
it 'should set the flash[:notice] to a default success message' do | ||
get :pass_auth | ||
flash[:notice].should == 'You have logged in successfully.' | ||
end | ||
|
||
it 'should be able ot receive a custom message' do | ||
get :pass_auth, :message => 'Eat at Joes.' | ||
flash[:notice].should == 'Eat at Joes.' | ||
end | ||
end | ||
|
||
describe '#current_user' do | ||
it 'should find the user based on the session user_id' do | ||
user = Factory.create(:twitter_oauth_user) | ||
request.session[:user_id] = user.id | ||
controller.send(:current_user).should == user | ||
end | ||
|
||
it 'should return nil if there is no user matching that id' do | ||
request.session[:user_id] = 2345 | ||
controller.send(:current_user).should be_nil | ||
end | ||
|
||
it 'should memoize the result (and not do a double find)' do | ||
user = Factory.create(:twitter_oauth_user) | ||
User.should_receive(:find_by_id).once.and_return(user) | ||
controller.send(:current_user).should == user | ||
controller.send(:current_user).should == user | ||
end | ||
end | ||
|
||
describe "#authorized?" do | ||
it 'should be true if there is a current_user' do | ||
user = Factory.create(:twitter_oauth_user) | ||
controller.stub!(:current_user).and_return(user) | ||
controller.send(:authorized?).should be_true | ||
end | ||
|
||
it 'should be false if there is not current_user' do | ||
controller.stub!(:current_user).and_return(nil) | ||
controller.send(:authorized?).should be_false | ||
end | ||
end | ||
|
||
describe '#access_denied' do | ||
it 'should redirect to the login path' do | ||
get :access_denied_action | ||
should redirect_to(login_path) | ||
end | ||
|
||
it 'should store the location first' do | ||
controller.should_receive(:store_location).once | ||
get :access_denied_action | ||
end | ||
end | ||
|
||
describe '#redirect_back_or_default' do | ||
it 'should redirect if there is a session[:return_to]' do | ||
request.session[:return_to] = '/' | ||
get :redirect_back_action, :to => '/notroot' | ||
should redirect_to('/') | ||
end | ||
|
||
it 'should redirect to the default provided otherwise' do | ||
get :redirect_back_action, :to => '/someurl' | ||
should redirect_to('/someurl') | ||
end | ||
end | ||
end |
Oops, something went wrong.