namxam-devise 1.1.0.win

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.rdoc +455 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +118 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +311 -0
  6. data/Rakefile +55 -0
  7. data/TODO +3 -0
  8. data/app/controllers/devise/confirmations_controller.rb +33 -0
  9. data/app/controllers/devise/passwords_controller.rb +41 -0
  10. data/app/controllers/devise/registrations_controller.rb +57 -0
  11. data/app/controllers/devise/sessions_controller.rb +23 -0
  12. data/app/controllers/devise/unlocks_controller.rb +34 -0
  13. data/app/helpers/devise_helper.rb +17 -0
  14. data/app/mailers/devise/mailer.rb +71 -0
  15. data/app/views/devise/confirmations/new.html.erb +12 -0
  16. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  17. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  18. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  19. data/app/views/devise/passwords/edit.html.erb +16 -0
  20. data/app/views/devise/passwords/new.html.erb +12 -0
  21. data/app/views/devise/registrations/edit.html.erb +25 -0
  22. data/app/views/devise/registrations/new.html.erb +18 -0
  23. data/app/views/devise/sessions/new.html.erb +17 -0
  24. data/app/views/devise/shared/_links.erb +19 -0
  25. data/app/views/devise/unlocks/new.html.erb +12 -0
  26. data/config/locales/en.yml +39 -0
  27. data/lib/devise.rb +290 -0
  28. data/lib/devise/controllers/helpers.rb +231 -0
  29. data/lib/devise/controllers/internal_helpers.rb +98 -0
  30. data/lib/devise/controllers/scoped_views.rb +35 -0
  31. data/lib/devise/controllers/url_helpers.rb +41 -0
  32. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  33. data/lib/devise/encryptors/base.rb +20 -0
  34. data/lib/devise/encryptors/bcrypt.rb +19 -0
  35. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  36. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  37. data/lib/devise/encryptors/sha1.rb +25 -0
  38. data/lib/devise/encryptors/sha512.rb +25 -0
  39. data/lib/devise/failure_app.rb +107 -0
  40. data/lib/devise/hooks/activatable.rb +11 -0
  41. data/lib/devise/hooks/forgetable.rb +11 -0
  42. data/lib/devise/hooks/rememberable.rb +35 -0
  43. data/lib/devise/hooks/timeoutable.rb +22 -0
  44. data/lib/devise/hooks/trackable.rb +9 -0
  45. data/lib/devise/mapping.rb +103 -0
  46. data/lib/devise/models.rb +80 -0
  47. data/lib/devise/models/authenticatable.rb +126 -0
  48. data/lib/devise/models/confirmable.rb +164 -0
  49. data/lib/devise/models/database_authenticatable.rb +110 -0
  50. data/lib/devise/models/lockable.rb +165 -0
  51. data/lib/devise/models/recoverable.rb +81 -0
  52. data/lib/devise/models/registerable.rb +8 -0
  53. data/lib/devise/models/rememberable.rb +104 -0
  54. data/lib/devise/models/timeoutable.rb +26 -0
  55. data/lib/devise/models/token_authenticatable.rb +60 -0
  56. data/lib/devise/models/trackable.rb +30 -0
  57. data/lib/devise/models/validatable.rb +53 -0
  58. data/lib/devise/modules.rb +23 -0
  59. data/lib/devise/orm/active_record.rb +36 -0
  60. data/lib/devise/orm/mongoid.rb +29 -0
  61. data/lib/devise/path_checker.rb +18 -0
  62. data/lib/devise/rails.rb +69 -0
  63. data/lib/devise/rails/routes.rb +248 -0
  64. data/lib/devise/rails/warden_compat.rb +39 -0
  65. data/lib/devise/schema.rb +97 -0
  66. data/lib/devise/strategies/authenticatable.rb +111 -0
  67. data/lib/devise/strategies/base.rb +33 -0
  68. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  69. data/lib/devise/strategies/rememberable.rb +43 -0
  70. data/lib/devise/strategies/token_authenticatable.rb +49 -0
  71. data/lib/devise/test_helpers.rb +90 -0
  72. data/lib/devise/version.rb +3 -0
  73. data/lib/generators/active_record/devise_generator.rb +28 -0
  74. data/lib/generators/active_record/templates/migration.rb +29 -0
  75. data/lib/generators/devise/devise_generator.rb +17 -0
  76. data/lib/generators/devise/install_generator.rb +24 -0
  77. data/lib/generators/devise/orm_helpers.rb +23 -0
  78. data/lib/generators/devise/templates/README +25 -0
  79. data/lib/generators/devise/templates/devise.rb +139 -0
  80. data/lib/generators/devise/views_generator.rb +63 -0
  81. data/lib/generators/devise_install_generator.rb +4 -0
  82. data/lib/generators/devise_views_generator.rb +4 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/test/controllers/helpers_test.rb +213 -0
  85. data/test/controllers/internal_helpers_test.rb +51 -0
  86. data/test/controllers/url_helpers_test.rb +58 -0
  87. data/test/devise_test.rb +65 -0
  88. data/test/encryptors_test.rb +30 -0
  89. data/test/failure_app_test.rb +123 -0
  90. data/test/integration/authenticatable_test.rb +344 -0
  91. data/test/integration/confirmable_test.rb +104 -0
  92. data/test/integration/database_authenticatable_test.rb +38 -0
  93. data/test/integration/http_authenticatable_test.rb +49 -0
  94. data/test/integration/lockable_test.rb +109 -0
  95. data/test/integration/recoverable_test.rb +141 -0
  96. data/test/integration/registerable_test.rb +153 -0
  97. data/test/integration/rememberable_test.rb +91 -0
  98. data/test/integration/timeoutable_test.rb +80 -0
  99. data/test/integration/token_authenticatable_test.rb +88 -0
  100. data/test/integration/trackable_test.rb +64 -0
  101. data/test/mailers/confirmation_instructions_test.rb +80 -0
  102. data/test/mailers/reset_password_instructions_test.rb +68 -0
  103. data/test/mailers/unlock_instructions_test.rb +62 -0
  104. data/test/mapping_test.rb +85 -0
  105. data/test/models/confirmable_test.rb +221 -0
  106. data/test/models/database_authenticatable_test.rb +148 -0
  107. data/test/models/lockable_test.rb +188 -0
  108. data/test/models/recoverable_test.rb +138 -0
  109. data/test/models/rememberable_test.rb +176 -0
  110. data/test/models/timeoutable_test.rb +28 -0
  111. data/test/models/token_authenticatable_test.rb +37 -0
  112. data/test/models/trackable_test.rb +5 -0
  113. data/test/models/validatable_test.rb +99 -0
  114. data/test/models_test.rb +77 -0
  115. data/test/orm/active_record.rb +9 -0
  116. data/test/orm/mongoid.rb +10 -0
  117. data/test/rails_app/app/active_record/admin.rb +3 -0
  118. data/test/rails_app/app/active_record/shim.rb +2 -0
  119. data/test/rails_app/app/active_record/user.rb +7 -0
  120. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  121. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  122. data/test/rails_app/app/controllers/home_controller.rb +7 -0
  123. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  124. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  125. data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
  126. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  127. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  128. data/test/rails_app/app/mongoid/admin.rb +6 -0
  129. data/test/rails_app/app/mongoid/shim.rb +16 -0
  130. data/test/rails_app/app/mongoid/user.rb +10 -0
  131. data/test/rails_app/config/application.rb +35 -0
  132. data/test/rails_app/config/boot.rb +13 -0
  133. data/test/rails_app/config/environment.rb +5 -0
  134. data/test/rails_app/config/environments/development.rb +19 -0
  135. data/test/rails_app/config/environments/production.rb +33 -0
  136. data/test/rails_app/config/environments/test.rb +33 -0
  137. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  138. data/test/rails_app/config/initializers/devise.rb +136 -0
  139. data/test/rails_app/config/initializers/inflections.rb +2 -0
  140. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  141. data/test/rails_app/config/routes.rb +47 -0
  142. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
  143. data/test/rails_app/db/schema.rb +86 -0
  144. data/test/routes_test.rb +146 -0
  145. data/test/support/assertions.rb +24 -0
  146. data/test/support/helpers.rb +54 -0
  147. data/test/support/integration.rb +88 -0
  148. data/test/support/test_silencer.rb +5 -0
  149. data/test/support/webrat/integrations/rails.rb +32 -0
  150. data/test/test_helper.rb +21 -0
  151. data/test/test_helpers_test.rb +72 -0
  152. metadata +230 -0
@@ -0,0 +1,39 @@
1
+ module Warden::Mixins::Common
2
+ def request
3
+ @request ||= ActionDispatch::Request.new(env)
4
+ end
5
+
6
+ def reset_session!
7
+ raw_session.inspect # why do I have to inspect it to get it to clear?
8
+ raw_session.clear
9
+ end
10
+
11
+ def cookies
12
+ request.cookie_jar
13
+ end
14
+ end
15
+
16
+ class Warden::SessionSerializer
17
+ def serialize(record)
18
+ [record.class.name, record.id]
19
+ end
20
+
21
+ def deserialize(keys)
22
+ klass, id = keys
23
+
24
+ if klass.is_a?(Class)
25
+ raise "Devise changed how it stores objects in session. If you are seeing this message, " <<
26
+ "you can fix it by changing one character in your cookie secret, forcing all previous " <<
27
+ "cookies to expire, or cleaning up your database sessions if you are using a db store."
28
+ end
29
+
30
+ klass.constantize.find(:first, :conditions => { :id => id })
31
+ rescue NameError => e
32
+ if e.message =~ /uninitialized constant/
33
+ Rails.logger.debug "Trying to deserialize invalid class #{klass}"
34
+ nil
35
+ else
36
+ raise
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,97 @@
1
+ module Devise
2
+ # Holds devise schema information. To use it, just include its methods
3
+ # and overwrite the apply_schema method.
4
+ module Schema
5
+
6
+ def authenticatable(*args)
7
+ ActiveSupport::Deprecation.warn "t.authenticatable in migrations is deprecated. Please use t.database_authenticatable instead.", caller
8
+ database_authenticatable(*args)
9
+ end
10
+
11
+ # Creates email, encrypted_password and password_salt.
12
+ #
13
+ # == Options
14
+ # * :null - When true, allow columns to be null.
15
+ # * :default - Should be set to "" when :null is false.
16
+ #
17
+ # == Notes
18
+ # For Datamapper compatibility, we explicitly hardcode the limit for the
19
+ # encrypter password field in 128 characters.
20
+ def database_authenticatable(options={})
21
+ null = options[:null] || false
22
+ default = options[:default] || ""
23
+
24
+ if options.delete(:encryptor)
25
+ ActiveSupport::Deprecation.warn ":encryptor as option is deprecated, simply remove it."
26
+ end
27
+
28
+ apply_devise_schema :email, String, :null => null, :default => default
29
+ apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
30
+ apply_devise_schema :password_salt, String, :null => null, :default => default
31
+ end
32
+
33
+ # Creates authentication_token.
34
+ def token_authenticatable(options={})
35
+ apply_devise_schema :authentication_token, String
36
+ end
37
+
38
+ # Creates confirmation_token, confirmed_at and confirmation_sent_at.
39
+ def confirmable
40
+ apply_devise_schema :confirmation_token, String
41
+ apply_devise_schema :confirmed_at, DateTime
42
+ apply_devise_schema :confirmation_sent_at, DateTime
43
+ end
44
+
45
+ # Creates reset_password_token.
46
+ def recoverable
47
+ apply_devise_schema :reset_password_token, String
48
+ end
49
+
50
+ # Creates remember_token and remember_created_at.
51
+ def rememberable
52
+ apply_devise_schema :remember_token, String
53
+ apply_devise_schema :remember_created_at, DateTime
54
+ end
55
+
56
+ # Creates sign_in_count, current_sign_in_at, last_sign_in_at,
57
+ # current_sign_in_ip, last_sign_in_ip.
58
+ def trackable
59
+ apply_devise_schema :sign_in_count, Integer, :default => 0
60
+ apply_devise_schema :current_sign_in_at, DateTime
61
+ apply_devise_schema :last_sign_in_at, DateTime
62
+ apply_devise_schema :current_sign_in_ip, String
63
+ apply_devise_schema :last_sign_in_ip, String
64
+ end
65
+
66
+ # Creates failed_attempts, unlock_token and locked_at depending on the options given.
67
+ #
68
+ # == Options
69
+ # * :unlock_strategy - The strategy used for unlock. Can be :time, :email, :both (default), :none.
70
+ # If :email or :both, creates a unlock_token field.
71
+ # * :lock_strategy - The strategy used for locking. Can be :failed_attempts (default) or :none.
72
+ def lockable(options={})
73
+ unlock_strategy = options[:unlock_strategy]
74
+ unlock_strategy ||= self.unlock_strategy if respond_to?(:unlock_strategy)
75
+ unlock_strategy ||= :both
76
+
77
+ lock_strategy = options[:lock_strategy]
78
+ lock_strategy ||= self.lock_strategy if respond_to?(:lock_strategy)
79
+ lock_strategy ||= :failed_attempts
80
+
81
+ if lock_strategy == :failed_attempts
82
+ apply_devise_schema :failed_attempts, Integer, :default => 0
83
+ end
84
+
85
+ if [:both, :email].include?(unlock_strategy)
86
+ apply_devise_schema :unlock_token, String
87
+ end
88
+
89
+ apply_devise_schema :locked_at, DateTime
90
+ end
91
+
92
+ # Overwrite with specific modification to create your own schema.
93
+ def apply_devise_schema(name, type, options={})
94
+ raise NotImplementedError
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,111 @@
1
+ require 'devise/strategies/base'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # This strategy should be used as basis for authentication strategies. It retrieves
6
+ # parameters both from params or from http authorization headers. See database_authenticatable
7
+ # for an example.
8
+ class Authenticatable < Base
9
+ attr_accessor :authentication_hash, :password
10
+
11
+ def valid?
12
+ valid_for_http_auth? || valid_for_params_auth?
13
+ end
14
+
15
+ private
16
+
17
+ # Check if this is strategy is valid for http authentication by:
18
+ #
19
+ # * Validating if the model allows params authentication;
20
+ # * If any of the authorization headers were sent;
21
+ # * If all authentication keys are present;
22
+ #
23
+ def valid_for_http_auth?
24
+ http_authenticatable? && request.authorization && with_authentication_hash(http_auth_hash)
25
+ end
26
+
27
+ # Check if this is strategy is valid for params authentication by:
28
+ #
29
+ # * Validating if the model allows params authentication;
30
+ # * If the request hits the sessions controller through POST;
31
+ # * If the params[scope] returns a hash with credentials;
32
+ # * If all authentication keys are present;
33
+ #
34
+ def valid_for_params_auth?
35
+ params_authenticatable? && valid_request? &&
36
+ valid_params? && with_authentication_hash(params_auth_hash)
37
+ end
38
+
39
+ # Check if the model accepts this strategy as http authenticatable.
40
+ def http_authenticatable?
41
+ mapping.to.http_authenticatable?(authenticatable_name)
42
+ end
43
+
44
+ # Check if the model accepts this strategy as params authenticatable.
45
+ def params_authenticatable?
46
+ mapping.to.params_authenticatable?(authenticatable_name)
47
+ end
48
+
49
+ # Extract the appropriate subhash for authentication from params.
50
+ def params_auth_hash
51
+ params[scope]
52
+ end
53
+
54
+ # Extract a hash with attributes:values from the http params.
55
+ def http_auth_hash
56
+ keys = [authentication_keys.first, :password]
57
+ Hash[*keys.zip(decode_credentials).flatten]
58
+ end
59
+
60
+ # By default, a request is valid if the controller is allowed and the VERB is POST.
61
+ def valid_request?
62
+ valid_controller? && valid_verb?
63
+ end
64
+
65
+ # Check if the controller is the one registered for authentication.
66
+ def valid_controller?
67
+ mapping.controllers[:sessions] == params[:controller]
68
+ end
69
+
70
+ # Check if it was a POST request.
71
+ def valid_verb?
72
+ request.post?
73
+ end
74
+
75
+ # If the request is valid, finally check if params_auth_hash returns a hash.
76
+ def valid_params?
77
+ params_auth_hash.is_a?(Hash)
78
+ end
79
+
80
+ # Check if password is present and is not equal to "X" (default value for token).
81
+ def valid_password?
82
+ password.present? && password != "X"
83
+ end
84
+
85
+ # Helper to decode credentials from HTTP.
86
+ def decode_credentials
87
+ username_and_password = request.authorization.split(' ', 2).last || ''
88
+ ActiveSupport::Base64.decode64(username_and_password).split(/:/, 2)
89
+ end
90
+
91
+ # Sets the authentication hash and the password from params_auth_hash or http_auth_hash.
92
+ def with_authentication_hash(hash)
93
+ self.authentication_hash = hash.slice(*authentication_keys)
94
+ self.password = hash[:password]
95
+ authentication_keys.all?{ |k| authentication_hash[k].present? }
96
+ end
97
+
98
+ # Holds the authentication keys.
99
+ def authentication_keys
100
+ @authentication_keys ||= mapping.to.authentication_keys
101
+ end
102
+
103
+ # Holds the authenticatable name for this class. Devise::Strategies::DatabaseAuthenticatable
104
+ # becomes simply :database.
105
+ def authenticatable_name
106
+ @authenticatable_name ||=
107
+ self.class.name.split("::").last.underscore.sub("_authenticatable", "").to_sym
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,33 @@
1
+ module Devise
2
+ module Strategies
3
+ # Base strategy for Devise. Responsible for verifying correct scope and mapping.
4
+ class Base < ::Warden::Strategies::Base
5
+ # Checks if a valid scope was given for devise and find mapping based on this scope.
6
+ def mapping
7
+ @mapping ||= begin
8
+ mapping = Devise.mappings[scope]
9
+ raise "Could not find mapping for #{scope}" unless mapping
10
+ mapping
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ def succeeded?
17
+ @result == :success
18
+ end
19
+
20
+ # Simply invokes valid_for_authentication? with the given block and deal with the result.
21
+ def validate(resource, &block)
22
+ result = resource && resource.valid_for_authentication?(&block)
23
+
24
+ case result
25
+ when Symbol, String
26
+ fail!(result)
27
+ else
28
+ result
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ require 'devise/strategies/authenticatable'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # Default strategy for signing in a user, based on his email and password in the database.
6
+ class DatabaseAuthenticatable < Authenticatable
7
+ def authenticate!
8
+ resource = valid_password? && mapping.to.find_for_database_authentication(authentication_hash)
9
+
10
+ if validate(resource){ resource.valid_password?(password) }
11
+ resource.after_database_authentication
12
+ success!(resource)
13
+ else
14
+ fail(:invalid)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Warden::Strategies.add(:database_authenticatable, Devise::Strategies::DatabaseAuthenticatable)
@@ -0,0 +1,43 @@
1
+ require 'devise/strategies/base'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # Remember the user through the remember token. This strategy is responsible
6
+ # to verify whether there is a cookie with the remember token, and to
7
+ # recreate the user from this cookie if it exists. Must be called *before*
8
+ # authenticatable.
9
+ class Rememberable < Devise::Strategies::Base
10
+ # A valid strategy for rememberable needs a remember token in the cookies.
11
+ def valid?
12
+ remember_cookie.present?
13
+ end
14
+
15
+ # To authenticate a user we deserialize the cookie and attempt finding
16
+ # the record in the database. If the attempt fails, we pass to another
17
+ # strategy handle the authentication.
18
+ def authenticate!
19
+ resource = mapping.to.serialize_from_cookie(*remember_cookie)
20
+
21
+ if validate(resource)
22
+ success!(resource)
23
+ else
24
+ cookies.delete(remember_key)
25
+ pass
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def remember_key
32
+ "remember_#{scope}_token"
33
+ end
34
+
35
+ # Accessor for remember cookie
36
+ def remember_cookie
37
+ @remember_cookie ||= cookies.signed[remember_key]
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)
@@ -0,0 +1,49 @@
1
+ require 'devise/strategies/base'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # Strategy for signing in a user, based on a authenticatable token. This works for both params
6
+ # and http. For the former, all you need to do is to pass the params in the URL:
7
+ #
8
+ # http://myapp.example.com/?user_token=SECRET
9
+ #
10
+ # For HTTP, you can pass the token as username and blank password. Since some clients may require
11
+ # a password, you can pass "X" as password and it will simply be ignored.
12
+ class TokenAuthenticatable < Authenticatable
13
+ def authenticate!
14
+ resource = mapping.to.find_for_token_authentication(authentication_hash)
15
+
16
+ if validate(resource)
17
+ resource.after_token_authentication
18
+ success!(resource)
19
+ else
20
+ fail(:invalid_token)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # TokenAuthenticatable request is valid for any controller and any verb.
27
+ def valid_request?
28
+ true
29
+ end
30
+
31
+ # Do not use remember_me behavir with token.
32
+ def remember_me?
33
+ false
34
+ end
35
+
36
+ # Try both scoped and non scoped keys.
37
+ def params_auth_hash
38
+ params[scope] || params
39
+ end
40
+
41
+ # Overwrite authentication keys to use token_authentication_key.
42
+ def authentication_keys
43
+ @authentication_keys ||= [mapping.to.token_authentication_key]
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ Warden::Strategies.add(:token_authenticatable, Devise::Strategies::TokenAuthenticatable)
@@ -0,0 +1,90 @@
1
+ module Devise
2
+ module TestHelpers
3
+ def self.included(base)
4
+ base.class_eval do
5
+ setup :setup_controller_for_warden, :warden if respond_to?(:setup)
6
+ end
7
+ end
8
+
9
+ # This is a Warden::Proxy customized for functional tests. It's meant to
10
+ # some of Warden::Manager responsibilities, as retrieving configuration
11
+ # options and calling the FailureApp.
12
+ class TestWarden < Warden::Proxy #:nodoc:
13
+ attr_reader :controller
14
+
15
+ def initialize(controller)
16
+ @controller = controller
17
+ manager = Warden::Manager.new(nil) do |config|
18
+ config.merge! Devise.warden_config
19
+ end
20
+ super(controller.request.env, manager)
21
+ end
22
+
23
+ def authenticate!(*args)
24
+ catch_with_redirect { super }
25
+ end
26
+
27
+ def user(*args)
28
+ catch_with_redirect { super }
29
+ end
30
+
31
+ def catch_with_redirect(&block)
32
+ result = catch(:warden, &block)
33
+
34
+ if result.is_a?(Hash) && !custom_failure? && !@controller.send(:performed?)
35
+ result[:action] ||= :unauthenticated
36
+
37
+ env = @controller.request.env
38
+ env["PATH_INFO"] = "/#{result[:action]}"
39
+ env["warden.options"] = result
40
+ Warden::Manager._before_failure.each{ |hook| hook.call(env, result) }
41
+
42
+ status, headers, body = Devise::FailureApp.call(env).to_a
43
+ @controller.send :render, :status => status, :text => body,
44
+ :content_type => headers["Content-Type"], :location => headers["Location"]
45
+
46
+ nil
47
+ else
48
+ result
49
+ end
50
+ end
51
+ end
52
+
53
+ # We need to setup the environment variables and the response in the controller.
54
+ def setup_controller_for_warden #:nodoc:
55
+ @request.env['action_controller.instance'] = @controller
56
+ end
57
+
58
+ # Quick access to Warden::Proxy.
59
+ def warden #:nodoc:
60
+ @warden ||= (@request.env['warden'] = TestWarden.new(@controller))
61
+ end
62
+
63
+ # sign_in a given resource by storing its keys in the session.
64
+ #
65
+ # Examples:
66
+ #
67
+ # sign_in :user, @user # sign_in(scope, resource)
68
+ # sign_in @user # sign_in(resource)
69
+ #
70
+ def sign_in(resource_or_scope, resource=nil)
71
+ scope ||= Devise::Mapping.find_scope!(resource_or_scope)
72
+ resource ||= resource_or_scope
73
+ warden.session_serializer.store(resource, scope)
74
+ end
75
+
76
+ # Sign out a given resource or scope by calling logout on Warden.
77
+ #
78
+ # Examples:
79
+ #
80
+ # sign_out :user # sign_out(scope)
81
+ # sign_out @user # sign_out(resource)
82
+ #
83
+ def sign_out(resource_or_scope)
84
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
85
+ @controller.instance_variable_set(:"@current_#{scope}", nil)
86
+ warden.logout(scope)
87
+ end
88
+
89
+ end
90
+ end