shingara-devise 0.4.3 → 0.4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/TODO CHANGED
@@ -1,3 +1,4 @@
1
+ * Allow authentication keys to be configured, so things like username and subdomain can be used
1
2
  * Devise::Timeoutable
2
3
  * Devise::TestHelper
3
4
  * Use request_ip in session cookies
@@ -8,7 +8,7 @@ module Devise
8
8
  :confirmations => :confirmable
9
9
  }.freeze
10
10
 
11
- STRATEGIES = [:rememberable, :authenticatable].freeze
11
+ STRATEGIES = [:authenticatable].freeze
12
12
  TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
13
13
 
14
14
  # Maps the messages types that comes from warden to a flash type.
@@ -58,23 +58,9 @@ module Devise
58
58
  mattr_accessor :mappings
59
59
  @@mappings = {}
60
60
 
61
+ mattr_accessor :orm
61
62
  @@orm = 'active_record'
62
63
 
63
- # ORM choice
64
- def self.orm()
65
- @@orm
66
- end
67
-
68
- def self.orm=(type_orm)
69
- if type_orm == 'MongoMapper'
70
- MongoMapper::Document::ClassMethods.send(:include, Devise::Models)
71
- require 'devise/orm/mongo_mapper'
72
- MongoMapper::Document::ClassMethods.send(:include, Devise::Orm::MongoMapper)
73
- end
74
- @@orm = type_orm
75
- end
76
-
77
-
78
64
  class << self
79
65
  # Default way to setup Devise. Run script/generate devise_install to create
80
66
  # a fresh initializer with all configuration values.
@@ -123,6 +109,12 @@ module Devise
123
109
  # If the user provided a warden hook, call it now.
124
110
  @warden_config.try :call, manager
125
111
  end
112
+
113
+ ##
114
+ # The class to call with orm define
115
+ def model_orm
116
+ @@model_orm ||= "Devise::Orm::#{@@orm.camelize}".constantize
117
+ end
126
118
  end
127
119
  end
128
120
 
@@ -2,10 +2,10 @@
2
2
  # This is done by checking the time frame the user is able to sign in without
3
3
  # confirming it's account. If the user has not confirmed it's account during
4
4
  # this time frame, he/she will not able to sign in anymore.
5
- Warden::Manager.after_set_user do |record, auth, options|
5
+ Warden::Manager.after_set_user do |record, warden, options|
6
6
  if record && record.respond_to?(:active?) && !record.active?
7
7
  scope = options[:scope]
8
- auth.logout(scope)
8
+ warden.logout(scope)
9
9
  throw :warden, :scope => scope, :params => { :unconfirmed => true }
10
10
  end
11
11
  end
@@ -3,15 +3,17 @@
3
3
  # that specific user and adds a cookie with this user info to sign in this user
4
4
  # automatically without asking for credentials. Refer to rememberable strategy
5
5
  # for more info.
6
- Warden::Manager.after_authentication do |record, auth, options|
6
+ Warden::Manager.after_authentication do |record, warden, options|
7
7
  scope = options[:scope]
8
- remember_me = auth.params[scope].try(:fetch, :remember_me, nil)
8
+ remember_me = warden.params[scope].try(:fetch, :remember_me, nil)
9
9
 
10
10
  if Devise::TRUE_VALUES.include?(remember_me) && record.respond_to?(:remember_me!)
11
11
  record.remember_me!
12
- auth.cookies['remember_token'] = {
12
+
13
+ warden.response.set_cookie "remember_#{scope}_token", {
13
14
  :value => record.class.serialize_into_cookie(record),
14
- :expires => record.remember_expires_at
15
+ :expires => record.remember_expires_at,
16
+ :path => "/"
15
17
  }
16
18
  end
17
19
  end
@@ -19,9 +21,9 @@ end
19
21
  # Before logout hook to forget the user in the given scope, only if rememberable
20
22
  # is activated for this scope. Also clear remember token to ensure the user
21
23
  # won't be remembered again.
22
- Warden::Manager.before_logout do |record, auth, scope|
24
+ Warden::Manager.before_logout do |record, warden, scope|
23
25
  if record.respond_to?(:forget_me!)
24
26
  record.forget_me!
25
- auth.cookies.delete('remember_token')
27
+ warden.response.delete_cookie "remember_#{scope}_token"
26
28
  end
27
29
  end
@@ -22,7 +22,7 @@ module Devise
22
22
  # # is the modules included in the class
23
23
  #
24
24
  class Mapping #:nodoc:
25
- attr_reader :name, :as, :path_names, :path_prefix
25
+ attr_reader :name, :as, :path_names, :path_prefix, :route_options
26
26
 
27
27
  # Loop through all mappings looking for a map that matches with the requested
28
28
  # path (ie /users/sign_in). If a path prefix is given, it's taken into account.
@@ -40,14 +40,13 @@ module Devise
40
40
  end
41
41
 
42
42
  def initialize(name, options) #:nodoc:
43
- options.assert_valid_keys(:class_name, :as, :path_names, :singular, :path_prefix)
44
-
45
- @as = (options[:as] || name).to_sym
46
- @klass = (options[:class_name] || name.to_s.classify).to_s
47
- @name = (options[:singular] || name.to_s.singularize).to_sym
48
- @path_names = options[:path_names] || {}
49
- @path_prefix = options[:path_prefix] || ""
43
+ @as = (options.delete(:as) || name).to_sym
44
+ @klass = (options.delete(:class_name) || name.to_s.classify).to_s
45
+ @name = (options.delete(:singular) || name.to_s.singularize).to_sym
46
+ @path_names = options.delete(:path_names) || {}
47
+ @path_prefix = options.delete(:path_prefix) || ""
50
48
  @path_prefix << "/" unless @path_prefix[-1] == ?/
49
+ @route_options = options || {}
51
50
 
52
51
  setup_path_names
53
52
  end
@@ -0,0 +1,35 @@
1
+ module Devise
2
+ module Middlewares
3
+ class Rememberable
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ auth = env['warden']
10
+ scopes = select_cookies(auth.request)
11
+ scopes.each do |scope, token|
12
+ mapping = Devise.mappings[scope]
13
+ next unless mapping && mapping.for.include?(:rememberable)
14
+ user = mapping.to.serialize_from_cookie(token)
15
+ auth.set_user(user, :scope => scope) if user
16
+ end
17
+
18
+ @app.call(env)
19
+ end
20
+
21
+ protected
22
+
23
+ def select_cookies(request)
24
+ scopes = {}
25
+ matching = /remember_(#{Devise.mappings.keys.join("|")})_token/
26
+ request.cookies.each do |key, value|
27
+ if key.to_s =~ matching
28
+ scopes[$1.to_sym] = value
29
+ end
30
+ end
31
+ scopes
32
+ end
33
+ end
34
+ end
35
+ end
@@ -92,13 +92,8 @@ module Devise
92
92
 
93
93
  # Convert new keys to methods which overwrites Devise defaults
94
94
  options.each { |key, value| send(:"#{key}=", value) }
95
-
96
- if Devise.orm == 'MongoMapper'
97
- modules.each do |mod|
98
- send(mod)
99
- end
100
- end
101
-
95
+ send :include, Devise.model_orm
96
+ add_fields(modules)
102
97
  end
103
98
 
104
99
  # Stores all modules included inside the model, so we are able to verify
@@ -1,6 +1,6 @@
1
1
  require 'digest/sha1'
2
2
  require 'devise/hooks/rememberable'
3
- require 'devise/strategies/rememberable'
3
+ require 'devise/middlewares/rememberable'
4
4
 
5
5
  module Devise
6
6
  module Models
@@ -0,0 +1,17 @@
1
+ module Devise
2
+ module Orm
3
+ module ActiveRecord
4
+ include Devise::Orm::Base
5
+ end
6
+ end
7
+ end
8
+
9
+
10
+ # Include alld devise definition about ActiveRecord
11
+ Rails.configuration.after_initialize do
12
+ if defined?(ActiveRecord)
13
+ ActiveRecord::Base.extend Devise::Models
14
+ ActiveRecord::Base.extend Devise::Orm::ActiveRecord
15
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Devise
2
+ module Orm
3
+ module Base
4
+ def add_fields(modules)
5
+ # Nothing happen because no modules
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,6 +1,9 @@
1
1
  module Devise
2
2
  module Orm
3
3
  module MongoMapper
4
+
5
+ include Devise::Orm::Base
6
+
4
7
  def authenticatable
5
8
  key :email, String
6
9
  key :encrypted_password, String
@@ -21,6 +24,17 @@ module Devise
21
24
  key :remember_token, String
22
25
  key :remember_created_at, DateTime
23
26
  end
27
+
28
+ ##
29
+ # Add all keys
30
+ def add_fields(modules)
31
+ modules.each do |mod|
32
+ send(mod)
33
+ end
34
+ end
24
35
  end
25
36
  end
26
37
  end
38
+
39
+ MongoMapper::Document::ClassMethods.send(:include, Devise::Models)
40
+ MongoMapper::Document::ClassMethods.send(:include, Devise::Orm::MongoMapper)
@@ -2,16 +2,16 @@ require 'devise/rails/routes'
2
2
  require 'devise/rails/warden_compat'
3
3
 
4
4
  Rails.configuration.after_initialize do
5
- if defined?(ActiveRecord)
6
- ActiveRecord::Base.extend Devise::Models
7
- ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
8
- end
9
-
10
5
  # Adds Warden Manager to Rails middleware stack, configuring default devise
11
6
  # strategy and also the failure app.
12
7
  Rails.configuration.middleware.use Warden::Manager do |manager|
13
8
  Devise.configure_warden_manager(manager)
14
9
  end
15
10
 
11
+ # If using a rememberable module, include the middleware that log users.
12
+ Rails.configuration.middleware.use Devise::Middlewares::Rememberable
13
+
16
14
  I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'locales', 'en.yml'))
15
+
16
+ require "devise/orm/#{Devise.orm}"
17
17
  end
@@ -64,6 +64,10 @@ module ActionController::Routing
64
64
  #
65
65
  # map.devise_for :users, :path_prefix => "/:locale"
66
66
  #
67
+ # Any other options will be passed to route definition. If you need conditions for your routes, just map:
68
+ #
69
+ # map.devise_for :users, :conditions => { :subdomain => /.+/ }
70
+ #
67
71
  # If you are using a dynamic prefix, like :locale above, you need to configure default_url_options through Devise. You can do that in config/initializers/devise.rb or setting a Devise.default_url_options:
68
72
  #
69
73
  # Devise.default_url_options do
@@ -75,10 +79,12 @@ module ActionController::Routing
75
79
 
76
80
  resources.map!(&:to_sym)
77
81
  resources.each do |resource|
78
- mapping = Devise::Mapping.new(resource, options)
82
+ mapping = Devise::Mapping.new(resource, options.dup)
79
83
  Devise.mappings[mapping.name] = mapping
80
84
 
81
- with_options(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_") do |routes|
85
+ route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
86
+
87
+ with_options(route_options) do |routes|
82
88
  mapping.for.each do |strategy|
83
89
  send(strategy, routes, mapping) if self.respond_to?(strategy, true)
84
90
  end
@@ -1,6 +1,5 @@
1
1
  # Taken from RailsWarden, thanks to Hassox. http://github.com/hassox/rails_warden
2
2
  module Warden::Mixins::Common
3
- # Gets the rails request object by default if it's available
4
3
  def request
5
4
  return @request if @request
6
5
  if env['action_controller.rescue.request']
@@ -19,8 +18,12 @@ module Warden::Mixins::Common
19
18
  raw_session.clear
20
19
  end
21
20
 
22
- # Proxy to request cookies
23
- def cookies
24
- request.cookies
21
+ def response
22
+ return @response if @response
23
+ if env['action_controller.rescue.response']
24
+ @response = env['action_controller.rescue.response']
25
+ else
26
+ Rack::Response.new(env)
27
+ end
25
28
  end
26
29
  end
@@ -1,3 +1,3 @@
1
1
  module Devise
2
- VERSION = "0.4.3".freeze
2
+ VERSION = "0.4.3.1".freeze
3
3
  end
@@ -50,7 +50,7 @@ class DeviseTest < ActiveSupport::TestCase
50
50
  Devise.configure_warden_manager(manager)
51
51
 
52
52
  assert_equal Devise::Failure, manager.failure_app
53
- assert_equal [:rememberable, :authenticatable], manager.default_strategies
53
+ assert_equal [:authenticatable], manager.default_strategies
54
54
  assert manager.silence_missing_strategies
55
55
  end
56
56
 
@@ -6,19 +6,17 @@ class RememberMeTest < ActionController::IntegrationTest
6
6
  Devise.remember_for = 1
7
7
  user = create_user
8
8
  user.remember_me!
9
- cookies['remember_token'] = User.serialize_into_cookie(user) + add_to_token
9
+ cookies['remember_user_token'] = User.serialize_into_cookie(user) + add_to_token
10
10
  user
11
11
  end
12
12
 
13
13
  test 'do not remember the user if he has not checked remember me option' do
14
14
  user = sign_in_as_user
15
-
16
15
  assert_nil user.reload.remember_token
17
16
  end
18
17
 
19
18
  test 'generate remember token after sign in' do
20
19
  user = sign_in_as_user :remember_me => true
21
-
22
20
  assert_not_nil user.reload.remember_token
23
21
  end
24
22
 
@@ -85,6 +85,14 @@ class MappingTest < ActiveSupport::TestCase
85
85
  end
86
86
  end
87
87
 
88
+ test 'should have default route options' do
89
+ assert_equal({}, Devise.mappings[:user].route_options)
90
+ end
91
+
92
+ test 'should allow passing route options to devise routes' do
93
+ assert_equal({ :requirements => { :extra => 'value' } }, Devise.mappings[:manager].route_options)
94
+ end
95
+
88
96
  test 'magic predicates' do
89
97
  mapping = Devise.mappings[:user]
90
98
  assert mapping.authenticatable?
@@ -4,7 +4,9 @@ ActionController::Routing::Routes.draw do |map|
4
4
  map.devise_for :account, :path_names => {
5
5
  :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification'
6
6
  }
7
- map.devise_for :organizers, :singular => 'manager', :path_prefix => '/:locale'
7
+ map.devise_for :organizers, :singular => 'manager',
8
+ :path_prefix => '/:locale',
9
+ :requirements => { :extra => 'value' }
8
10
 
9
11
  map.resources :users, :only => :index
10
12
  map.resources :admins, :only => :index
@@ -69,11 +69,14 @@ class MapRoutingTest < ActionController::TestCase
69
69
  end
70
70
 
71
71
  test 'map organizer with custom singular name' do
72
- assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en"}, '/en/organizers/password/new')
72
+ assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/password/new')
73
73
  end
74
74
 
75
75
  test 'map organizer with path prefix' do
76
- assert_recognizes({:controller => 'sessions', :action => 'new', :locale => "en"}, '/en/organizers/sign_in')
76
+ assert_recognizes({:controller => 'sessions', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/sign_in')
77
77
  end
78
78
 
79
+ test 'map organizer with additional route options' do
80
+ assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/password/new')
81
+ end
79
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shingara-devise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-11-11 00:00:00 +01:00
13
+ date: 2009-11-13 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -73,6 +73,7 @@ files:
73
73
  - lib/devise/hooks/rememberable.rb
74
74
  - lib/devise/locales/en.yml
75
75
  - lib/devise/mapping.rb
76
+ - lib/devise/middlewares/rememberable.rb
76
77
  - lib/devise/migrations.rb
77
78
  - lib/devise/models.rb
78
79
  - lib/devise/models/authenticatable.rb
@@ -80,13 +81,14 @@ files:
80
81
  - lib/devise/models/recoverable.rb
81
82
  - lib/devise/models/rememberable.rb
82
83
  - lib/devise/models/validatable.rb
84
+ - lib/devise/orm/active_record.rb
85
+ - lib/devise/orm/base.rb
83
86
  - lib/devise/orm/mongo_mapper.rb
84
87
  - lib/devise/rails.rb
85
88
  - lib/devise/rails/routes.rb
86
89
  - lib/devise/rails/warden_compat.rb
87
90
  - lib/devise/strategies/authenticatable.rb
88
91
  - lib/devise/strategies/base.rb
89
- - lib/devise/strategies/rememberable.rb
90
92
  - lib/devise/version.rb
91
93
  - lib/devise/warden.rb
92
94
  has_rdoc: true
@@ -1,35 +0,0 @@
1
- module Devise
2
- module Strategies
3
- # Remember the user through the remember token. This strategy is responsible
4
- # to verify whether there is a cookie with the remember token, and to
5
- # recreate the user from this cookie if it exists. Must be called *before*
6
- # authenticatable.
7
- class Rememberable < Devise::Strategies::Base
8
-
9
- # A valid strategy for rememberable needs a remember token in the cookies.
10
- def valid?
11
- super && remember_me_cookie.present?
12
- end
13
-
14
- # To authenticate a user we deserialize the cookie and attempt finding
15
- # the record in the database. If the attempt fails, we pass to another
16
- # strategy handle the authentication.
17
- def authenticate!
18
- if resource = mapping.to.serialize_from_cookie(remember_me_cookie)
19
- success!(resource)
20
- else
21
- pass
22
- end
23
- end
24
-
25
- private
26
-
27
- # Accessor for remember cookie
28
- def remember_me_cookie
29
- cookies['remember_token']
30
- end
31
- end
32
- end
33
- end
34
-
35
- Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)