Navigation Menu

Skip to content

Commit

Permalink
Initial routes and module setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 26, 2010
1 parent 9ab64c5 commit 6c5be8d
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/controllers/devise/oauth_callbacks_controller.rb
@@ -0,0 +1,9 @@
class Devise::OauthCallbacksController < ApplicationController
include Devise::Controllers::InternalHelpers

def twitter
end

def github
end
end
35 changes: 29 additions & 6 deletions lib/devise.rb
Expand Up @@ -161,14 +161,38 @@ module Strategies
mattr_accessor :navigational_formats
@@navigational_formats = [:html]

# When set to true, signing out an user signs out all other scopes.
mattr_accessor :sign_out_all_scopes
@@sign_out_all_scopes = false

# Oauth providers
mattr_accessor :oauth_providers
@@oauth_providers = []

# PRIVATE CONFIGURATION

# Oauth configurations.
mattr_reader :oauth_configs
@@oauth_configs = {}

# Private methods to interface with Warden.
mattr_accessor :warden_config
@@warden_config = nil
@@warden_config_block = nil

# When set to true, signing out an user signs out all other scopes.
mattr_accessor :sign_out_all_scopes
@@sign_out_all_scopes = false
# Specify an oauth provider.
#
# config.oauth :github, APP_ID, APP_SECRET,
# :site => 'https://github.com/',
# :authorize_path => '/login/oauth/authorize',
# :access_token_path => '/login/oauth/access_token',
# :scope => %w(user public_repo)
#
def self.oauth(provider, *args)
@@oauth_providers << provider
@@oauth_providers.uniq!
@@oauth_configs[provider] = Devise::OAuth::Config.new(*args)
end

def self.use_default_scope=(*)
ActiveSupport::Deprecation.warn "config.use_default_scope is deprecated and removed from Devise. " <<
Expand Down Expand Up @@ -209,7 +233,6 @@ def self.add_mapping(resource, options)
# +model+ - String representing the load path to a custom *model* for this module (to autoload.)
# +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
# +route+ - Symbol representing the named *route* helper for this module.
# +flash+ - Symbol representing the *flash messages* used by this helper.
# +strategy+ - Symbol representing if this module got a custom *strategy*.
#
# All values, except :model, accept also a boolean and will have the same name as the given module
Expand Down Expand Up @@ -243,8 +266,8 @@ def self.add_module(module_name, options = {})
end

if options[:model]
model_path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, model_path)
path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, path)
end

Devise::Mapping.add_module module_name
Expand Down
11 changes: 11 additions & 0 deletions lib/devise/models/oauthable.rb
@@ -0,0 +1,11 @@
module Devise
module Models
module Oauthable
extend ActiveSupport::Concern

module ClassMethods
Devise::Models.config(self, :oauth_providers)
end
end
end
end
5 changes: 4 additions & 1 deletion lib/devise/modules.rb
Expand Up @@ -8,7 +8,10 @@
s.add_module :rememberable
end

# Misc after
# Other authentications
d.add_module :oauthable, :controller => :oauth_callbacks, :route => :oauth_callback

# Misc after
d.add_module :recoverable, :controller => :passwords, :route => :password
d.add_module :registerable, :controller => :registrations, :route => :registration
d.add_module :validatable
Expand Down
5 changes: 5 additions & 0 deletions lib/devise/rails/routes.rb
Expand Up @@ -231,6 +231,11 @@ def devise_registration(mapping, controllers) #:nodoc:
:path_names => { :new => mapping.path_names[:sign_up] }, :controller => controllers[:registrations]
end

def devise_oauth_callback(mapping, controllers) #:nodoc:
get "/oauth/:action/callback", :action => Regexp.union(mapping.to.oauth_providers.map(&:to_s)),
:to => controllers[:oauth_callbacks], :as => :outh_callback
end

def with_devise_exclusive_scope(new_path, new_as) #:nodoc:
old_as, old_path, old_module = @scope[:as], @scope[:path], @scope[:module]
@scope[:as], @scope[:path], @scope[:module] = new_as, new_path, nil
Expand Down
2 changes: 1 addition & 1 deletion test/rails_app/app/active_record/user.rb
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
devise :database_authenticatable, :confirmable, :lockable, :recoverable,
:registerable, :rememberable, :timeoutable, :token_authenticatable,
:trackable, :validatable
:trackable, :validatable, :oauthable, :oauth_providers => [:github, :twitter]

attr_accessible :username, :email, :password, :password_confirmation
end
2 changes: 1 addition & 1 deletion test/rails_app/app/mongoid/user.rb
Expand Up @@ -6,5 +6,5 @@ class User

devise :database_authenticatable, :confirmable, :lockable, :recoverable,
:registerable, :rememberable, :timeoutable, :token_authenticatable,
:trackable, :validatable
:trackable, :validatable, :oauthable, :oauth_providers => [:github, :twitter]
end
9 changes: 9 additions & 0 deletions test/routes_test.rb
Expand Up @@ -86,6 +86,15 @@ class DefaultRoutingTest < ActionController::TestCase
assert_recognizes({:controller => 'devise/registrations', :action => 'destroy'}, {:path => 'users', :method => :delete})
end

test 'map oauth callbacks' do
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'twitter'}, {:path => 'users/oauth/twitter/callback', :method => :get})
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'github'}, {:path => 'users/oauth/github/callback', :method => :get})

assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'facebook'}, {:path => 'users/oauth/facebook/callback', :method => :get})
end
end

protected

def assert_named_route(result, name)
Expand Down

0 comments on commit 6c5be8d

Please sign in to comment.