loyal_devise 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.rdoc +881 -0
- data/CONTRIBUTING.md +12 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +154 -0
- data/MIT-LICENSE +20 -0
- data/README.md +388 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +44 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
- data/app/controllers/devise/passwords_controller.rb +57 -0
- data/app/controllers/devise/registrations_controller.rb +120 -0
- data/app/controllers/devise/sessions_controller.rb +51 -0
- data/app/controllers/devise/unlocks_controller.rb +45 -0
- data/app/controllers/devise_controller.rb +193 -0
- data/app/helpers/devise_helper.rb +26 -0
- data/app/mailers/devise/mailer.rb +16 -0
- data/app/views/devise/_links.erb +3 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +59 -0
- data/devise.gemspec +26 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise/controllers/helpers.rb +273 -0
- data/lib/devise/controllers/rememberable.rb +53 -0
- data/lib/devise/controllers/scoped_views.rb +18 -0
- data/lib/devise/controllers/url_helpers.rb +68 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/failure_app.rb +188 -0
- data/lib/devise/hooks/activatable.rb +12 -0
- data/lib/devise/hooks/forgetable.rb +10 -0
- data/lib/devise/hooks/lockable.rb +8 -0
- data/lib/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/hooks/timeoutable.rb +26 -0
- data/lib/devise/hooks/trackable.rb +10 -0
- data/lib/devise/mailers/helpers.rb +92 -0
- data/lib/devise/mapping.rb +173 -0
- data/lib/devise/models/authenticatable.rb +269 -0
- data/lib/devise/models/confirmable.rb +271 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +194 -0
- data/lib/devise/models/omniauthable.rb +28 -0
- data/lib/devise/models/recoverable.rb +141 -0
- data/lib/devise/models/registerable.rb +26 -0
- data/lib/devise/models/rememberable.rb +126 -0
- data/lib/devise/models/timeoutable.rb +50 -0
- data/lib/devise/models/token_authenticatable.rb +90 -0
- data/lib/devise/models/trackable.rb +36 -0
- data/lib/devise/models/validatable.rb +67 -0
- data/lib/devise/models.rb +129 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +46 -0
- data/lib/devise/omniauth/url_helpers.rb +19 -0
- data/lib/devise/omniauth.rb +29 -0
- data/lib/devise/orm/active_record.rb +4 -0
- data/lib/devise/orm/mongoid.rb +4 -0
- data/lib/devise/param_filter.rb +42 -0
- data/lib/devise/rails/routes.rb +447 -0
- data/lib/devise/rails/warden_compat.rb +44 -0
- data/lib/devise/rails.rb +55 -0
- data/lib/devise/strategies/authenticatable.rb +177 -0
- data/lib/devise/strategies/base.rb +21 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +56 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +132 -0
- data/lib/devise/time_inflector.rb +15 -0
- data/lib/devise/version.rb +4 -0
- data/lib/devise.rb +445 -0
- data/lib/generators/active_record/devise_generator.rb +80 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/active_record/templates/migration_existing.rb +27 -0
- data/lib/generators/devise/devise_generator.rb +25 -0
- data/lib/generators/devise/install_generator.rb +25 -0
- data/lib/generators/devise/orm_helpers.rb +33 -0
- data/lib/generators/devise/views_generator.rb +117 -0
- data/lib/generators/mongoid/devise_generator.rb +58 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +241 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/custom_strategy_test.rb +63 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +111 -0
- data/test/controllers/sessions_controller_test.rb +58 -0
- data/test/controllers/url_helpers_test.rb +60 -0
- data/test/delegator_test.rb +20 -0
- data/test/devise_test.rb +73 -0
- data/test/failure_app_test.rb +222 -0
- data/test/generators/active_record_generator_test.rb +76 -0
- data/test/generators/devise_generator_test.rb +40 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/generators/mongoid_generator_test.rb +24 -0
- data/test/generators/views_generator_test.rb +53 -0
- data/test/helpers/devise_helper_test.rb +52 -0
- data/test/indifferent_hash.rb +34 -0
- data/test/integration/authenticatable_test.rb +634 -0
- data/test/integration/confirmable_test.rb +299 -0
- data/test/integration/database_authenticatable_test.rb +83 -0
- data/test/integration/http_authenticatable_test.rb +98 -0
- data/test/integration/lockable_test.rb +243 -0
- data/test/integration/omniauthable_test.rb +134 -0
- data/test/integration/recoverable_test.rb +307 -0
- data/test/integration/registerable_test.rb +346 -0
- data/test/integration/rememberable_test.rb +159 -0
- data/test/integration/timeoutable_test.rb +141 -0
- data/test/integration/token_authenticatable_test.rb +162 -0
- data/test/integration/trackable_test.rb +93 -0
- data/test/mailers/confirmation_instructions_test.rb +103 -0
- data/test/mailers/reset_password_instructions_test.rb +84 -0
- data/test/mailers/unlock_instructions_test.rb +78 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/authenticatable_test.rb +8 -0
- data/test/models/confirmable_test.rb +392 -0
- data/test/models/database_authenticatable_test.rb +190 -0
- data/test/models/lockable_test.rb +274 -0
- data/test/models/omniauthable_test.rb +8 -0
- data/test/models/recoverable_test.rb +206 -0
- data/test/models/registerable_test.rb +8 -0
- data/test/models/rememberable_test.rb +175 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +47 -0
- data/test/models/token_authenticatable_test.rb +56 -0
- data/test/models/trackable_test.rb +14 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +180 -0
- data/test/omniauth/config_test.rb +58 -0
- data/test/omniauth/url_helpers_test.rb +52 -0
- data/test/orm/active_record.rb +10 -0
- data/test/orm/mongoid.rb +15 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/shim.rb +3 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +12 -0
- data/test/rails_app/app/controllers/application_controller.rb +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +26 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
- data/test/rails_app/app/controllers/users_controller.rb +24 -0
- data/test/rails_app/app/helpers/application_helper.rb +4 -0
- data/test/rails_app/app/mailers/users/mailer.rb +9 -0
- data/test/rails_app/app/mongoid/admin.rb +28 -0
- data/test/rails_app/app/mongoid/shim.rb +25 -0
- data/test/rails_app/app/mongoid/user.rb +43 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config/application.rb +42 -0
- data/test/rails_app/config/boot.rb +9 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +6 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +34 -0
- data/test/rails_app/config/environments/test.rb +34 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
- data/test/rails_app/config/initializers/devise.rb +179 -0
- data/test/rails_app/config/initializers/inflections.rb +3 -0
- data/test/rails_app/config/initializers/secret_token.rb +3 -0
- data/test/rails_app/config/routes.rb +101 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
- data/test/rails_app/db/schema.rb +53 -0
- data/test/rails_app/lib/shared_admin.rb +15 -0
- data/test/rails_app/lib/shared_user.rb +27 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +249 -0
- data/test/support/assertions.rb +41 -0
- data/test/support/helpers.rb +92 -0
- data/test/support/integration.rb +93 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +25 -0
- data/test/test_helper.rb +28 -0
- data/test/test_helpers_test.rb +152 -0
- metadata +407 -0
@@ -0,0 +1,177 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/strategies/base'
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
module Strategies
|
6
|
+
# This strategy should be used as basis for authentication strategies. It retrieves
|
7
|
+
# parameters both from params or from http authorization headers. See database_authenticatable
|
8
|
+
# for an example.
|
9
|
+
class Authenticatable < Base
|
10
|
+
attr_accessor :authentication_hash, :authentication_type, :password
|
11
|
+
|
12
|
+
def store?
|
13
|
+
super && !mapping.to.skip_session_storage.include?(authentication_type)
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
valid_for_params_auth? || valid_for_http_auth?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Receives a resource and check if it is valid by calling valid_for_authentication?
|
23
|
+
# An optional block that will be triggered while validating can be optionally
|
24
|
+
# given as parameter. Check Devise::Models::Authenticable.valid_for_authentication?
|
25
|
+
# for more information.
|
26
|
+
#
|
27
|
+
# In case the resource can't be validated, it will fail with the given
|
28
|
+
# unauthenticated_message.
|
29
|
+
def validate(resource, &block)
|
30
|
+
unless resource
|
31
|
+
ActiveSupport::Deprecation.warn "an empty resource was given to #{self.class.name}#validate. " \
|
32
|
+
"Please ensure the resource is not nil", caller
|
33
|
+
end
|
34
|
+
|
35
|
+
result = resource && resource.valid_for_authentication?(&block)
|
36
|
+
|
37
|
+
case result
|
38
|
+
when Symbol, String
|
39
|
+
ActiveSupport::Deprecation.warn "valid_for_authentication? should return a boolean value"
|
40
|
+
fail!(result)
|
41
|
+
return false
|
42
|
+
end
|
43
|
+
|
44
|
+
if result
|
45
|
+
decorate(resource)
|
46
|
+
true
|
47
|
+
else
|
48
|
+
if resource
|
49
|
+
fail!(resource.unauthenticated_message)
|
50
|
+
end
|
51
|
+
false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get values from params and set in the resource.
|
56
|
+
def decorate(resource)
|
57
|
+
resource.remember_me = remember_me? if resource.respond_to?(:remember_me=)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Should this resource be marked to be remembered?
|
61
|
+
def remember_me?
|
62
|
+
valid_params? && Devise::TRUE_VALUES.include?(params_auth_hash[:remember_me])
|
63
|
+
end
|
64
|
+
|
65
|
+
# Check if this is strategy is valid for http authentication by:
|
66
|
+
#
|
67
|
+
# * Validating if the model allows params authentication;
|
68
|
+
# * If any of the authorization headers were sent;
|
69
|
+
# * If all authentication keys are present;
|
70
|
+
#
|
71
|
+
def valid_for_http_auth?
|
72
|
+
http_authenticatable? && request.authorization && with_authentication_hash(:http_auth, http_auth_hash)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Check if this is strategy is valid for params authentication by:
|
76
|
+
#
|
77
|
+
# * Validating if the model allows params authentication;
|
78
|
+
# * If the request hits the sessions controller through POST;
|
79
|
+
# * If the params[scope] returns a hash with credentials;
|
80
|
+
# * If all authentication keys are present;
|
81
|
+
#
|
82
|
+
def valid_for_params_auth?
|
83
|
+
params_authenticatable? && valid_params_request? &&
|
84
|
+
valid_params? && with_authentication_hash(:params_auth, params_auth_hash)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Check if the model accepts this strategy as http authenticatable.
|
88
|
+
def http_authenticatable?
|
89
|
+
mapping.to.http_authenticatable?(authenticatable_name)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Check if the model accepts this strategy as params authenticatable.
|
93
|
+
def params_authenticatable?
|
94
|
+
mapping.to.params_authenticatable?(authenticatable_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Extract the appropriate subhash for authentication from params.
|
98
|
+
def params_auth_hash
|
99
|
+
params[scope]
|
100
|
+
end
|
101
|
+
|
102
|
+
# Extract a hash with attributes:values from the http params.
|
103
|
+
def http_auth_hash
|
104
|
+
keys = [authentication_keys.first, :password]
|
105
|
+
Hash[*keys.zip(decode_credentials).flatten]
|
106
|
+
end
|
107
|
+
|
108
|
+
# By default, a request is valid if the controller set the proper env variable.
|
109
|
+
def valid_params_request?
|
110
|
+
!!env["devise.allow_params_authentication"]
|
111
|
+
end
|
112
|
+
|
113
|
+
# If the request is valid, finally check if params_auth_hash returns a hash.
|
114
|
+
def valid_params?
|
115
|
+
params_auth_hash.is_a?(Hash)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Check if password is present and is not equal to "X" (default value for token).
|
119
|
+
def valid_password?
|
120
|
+
password.present? && password != "X"
|
121
|
+
end
|
122
|
+
|
123
|
+
# Helper to decode credentials from HTTP.
|
124
|
+
def decode_credentials
|
125
|
+
return [] unless request.authorization && request.authorization =~ /^Basic (.*)/m
|
126
|
+
Base64.decode64($1).split(/:/, 2)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Sets the authentication hash and the password from params_auth_hash or http_auth_hash.
|
130
|
+
def with_authentication_hash(auth_type, auth_values)
|
131
|
+
self.authentication_hash, self.authentication_type = {}, auth_type
|
132
|
+
self.password = auth_values[:password]
|
133
|
+
|
134
|
+
parse_authentication_key_values(auth_values, authentication_keys) &&
|
135
|
+
parse_authentication_key_values(request_values, request_keys)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Holds the authentication keys.
|
139
|
+
def authentication_keys
|
140
|
+
@authentication_keys ||= mapping.to.authentication_keys
|
141
|
+
end
|
142
|
+
|
143
|
+
# Holds request keys.
|
144
|
+
def request_keys
|
145
|
+
@request_keys ||= mapping.to.request_keys
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns values from the request object.
|
149
|
+
def request_values
|
150
|
+
keys = request_keys.respond_to?(:keys) ? request_keys.keys : request_keys
|
151
|
+
values = keys.map { |k| self.request.send(k) }
|
152
|
+
Hash[keys.zip(values)]
|
153
|
+
end
|
154
|
+
|
155
|
+
# Parse authentication keys considering if they should be enforced or not.
|
156
|
+
def parse_authentication_key_values(hash, keys)
|
157
|
+
keys.each do |key, enforce|
|
158
|
+
value = hash[key].presence
|
159
|
+
if value
|
160
|
+
self.authentication_hash[key] = value
|
161
|
+
else
|
162
|
+
return false unless enforce == false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
true
|
166
|
+
end
|
167
|
+
|
168
|
+
# Holds the authenticatable name for this class. Devise::Strategies::DatabaseAuthenticatable
|
169
|
+
# becomes simply :database.
|
170
|
+
def authenticatable_name
|
171
|
+
@authenticatable_name ||=
|
172
|
+
ActiveSupport::Inflector.underscore(self.class.name.split("::").last).
|
173
|
+
sub("_authenticatable", "").to_sym
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Devise
|
3
|
+
module Strategies
|
4
|
+
# Base strategy for Devise. Responsible for verifying correct scope and mapping.
|
5
|
+
class Base < ::Warden::Strategies::Base
|
6
|
+
# Whenever CSRF cannot be verified, we turn off any kind of storage
|
7
|
+
def store?
|
8
|
+
!env["devise.skip_storage"]
|
9
|
+
end
|
10
|
+
|
11
|
+
# Checks if a valid scope was given for devise and find mapping based on this scope.
|
12
|
+
def mapping
|
13
|
+
@mapping ||= begin
|
14
|
+
mapping = Devise.mappings[scope]
|
15
|
+
raise "Could not find mapping for #{scope}" unless mapping
|
16
|
+
mapping
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/strategies/authenticatable'
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
module Strategies
|
6
|
+
# Default strategy for signing in a user, based on his email and password in the database.
|
7
|
+
class DatabaseAuthenticatable < Authenticatable
|
8
|
+
def authenticate!
|
9
|
+
resource = valid_password? && mapping.to.find_for_database_authentication(authentication_hash)
|
10
|
+
return fail(:invalid) unless resource
|
11
|
+
|
12
|
+
if validate(resource){ resource.valid_password?(password) }
|
13
|
+
resource.after_database_authentication
|
14
|
+
success!(resource)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Warden::Strategies.add(:database_authenticatable, Devise::Strategies::DatabaseAuthenticatable)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/strategies/authenticatable'
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
module Strategies
|
6
|
+
# Remember the user through the remember token. This strategy is responsible
|
7
|
+
# to verify whether there is a cookie with the remember token, and to
|
8
|
+
# recreate the user from this cookie if it exists. Must be called *before*
|
9
|
+
# authenticatable.
|
10
|
+
class Rememberable < Authenticatable
|
11
|
+
# A valid strategy for rememberable needs a remember token in the cookies.
|
12
|
+
def valid?
|
13
|
+
@remember_cookie = nil
|
14
|
+
remember_cookie.present?
|
15
|
+
end
|
16
|
+
|
17
|
+
# To authenticate a user we deserialize the cookie and attempt finding
|
18
|
+
# the record in the database. If the attempt fails, we pass to another
|
19
|
+
# strategy handle the authentication.
|
20
|
+
def authenticate!
|
21
|
+
resource = mapping.to.serialize_from_cookie(*remember_cookie)
|
22
|
+
|
23
|
+
unless resource
|
24
|
+
cookies.delete(remember_key)
|
25
|
+
return pass
|
26
|
+
end
|
27
|
+
|
28
|
+
if validate(resource)
|
29
|
+
success!(resource)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def decorate(resource)
|
36
|
+
super
|
37
|
+
resource.extend_remember_period = mapping.to.extend_remember_period if resource.respond_to?(:extend_remember_period=)
|
38
|
+
end
|
39
|
+
|
40
|
+
def remember_me?
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
def remember_key
|
45
|
+
"remember_#{scope}_token"
|
46
|
+
end
|
47
|
+
|
48
|
+
def remember_cookie
|
49
|
+
@remember_cookie ||= cookies.signed[remember_key]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/strategies/base'
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
module Strategies
|
6
|
+
# Strategy for signing in a user, based on a authenticatable token. This works for both params
|
7
|
+
# and http. For the former, all you need to do is to pass the params in the URL:
|
8
|
+
#
|
9
|
+
# http://myapp.example.com/?user_token=SECRET
|
10
|
+
#
|
11
|
+
# For HTTP, you can pass the token as username and blank password. Since some clients may require
|
12
|
+
# a password, you can pass "X" as password and it will simply be ignored.
|
13
|
+
class TokenAuthenticatable < Authenticatable
|
14
|
+
def store?
|
15
|
+
super && !mapping.to.skip_session_storage.include?(:token_auth)
|
16
|
+
end
|
17
|
+
|
18
|
+
def authenticate!
|
19
|
+
resource = mapping.to.find_for_token_authentication(authentication_hash)
|
20
|
+
return fail(:invalid_token) unless resource
|
21
|
+
|
22
|
+
if validate(resource)
|
23
|
+
resource.after_token_authentication
|
24
|
+
success!(resource)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Token Authenticatable can be authenticated with params in any controller and any verb.
|
31
|
+
def valid_params_request?
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
# Do not use remember_me behavior with token.
|
36
|
+
def remember_me?
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
# Try both scoped and non scoped keys.
|
41
|
+
def params_auth_hash
|
42
|
+
if params[scope].kind_of?(Hash) && params[scope].has_key?(authentication_keys.first)
|
43
|
+
params[scope]
|
44
|
+
else
|
45
|
+
params
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Overwrite authentication keys to use token_authentication_key.
|
50
|
+
def authentication_keys
|
51
|
+
@authentication_keys ||= [mapping.to.token_authentication_key]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
Warden::Strategies.add(:token_authenticatable, Devise::Strategies::TokenAuthenticatable)
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Devise
|
3
|
+
# Devise::TestHelpers provides a facility to test controllers in isolation
|
4
|
+
# when using ActionController::TestCase allowing you to quickly sign_in or
|
5
|
+
# sign_out a user. Do not use Devise::TestHelpers in integration tests.
|
6
|
+
#
|
7
|
+
# Notice you should not test Warden specific behavior (like Warden callbacks)
|
8
|
+
# using Devise::TestHelpers since it is a stub of the actual behavior. Such
|
9
|
+
# callbacks should be tested in your integration suite instead.
|
10
|
+
module TestHelpers
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
setup :setup_controller_for_warden, :warden if respond_to?(:setup)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Override process to consider warden.
|
18
|
+
def process(*)
|
19
|
+
# Make sure we always return @response, a la ActionController::TestCase::Behaviour#process, even if warden interrupts
|
20
|
+
_catch_warden { super } || @response
|
21
|
+
end
|
22
|
+
|
23
|
+
# We need to setup the environment variables and the response in the controller.
|
24
|
+
def setup_controller_for_warden #:nodoc:
|
25
|
+
@request.env['action_controller.instance'] = @controller
|
26
|
+
end
|
27
|
+
|
28
|
+
# Quick access to Warden::Proxy.
|
29
|
+
def warden #:nodoc:
|
30
|
+
@warden ||= begin
|
31
|
+
manager = Warden::Manager.new(nil) do |config|
|
32
|
+
config.merge! Devise.warden_config
|
33
|
+
end
|
34
|
+
@request.env['warden'] = Warden::Proxy.new(@request.env, manager)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# sign_in a given resource by storing its keys in the session.
|
39
|
+
# This method bypass any warden authentication callback.
|
40
|
+
#
|
41
|
+
# Examples:
|
42
|
+
#
|
43
|
+
# sign_in :user, @user # sign_in(scope, resource)
|
44
|
+
# sign_in @user # sign_in(resource)
|
45
|
+
#
|
46
|
+
def sign_in(resource_or_scope, resource=nil)
|
47
|
+
scope ||= Devise::Mapping.find_scope!(resource_or_scope)
|
48
|
+
resource ||= resource_or_scope
|
49
|
+
warden.instance_variable_get(:@users).delete(scope)
|
50
|
+
warden.session_serializer.store(resource, scope)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Sign out a given resource or scope by calling logout on Warden.
|
54
|
+
# This method bypass any warden logout callback.
|
55
|
+
#
|
56
|
+
# Examples:
|
57
|
+
#
|
58
|
+
# sign_out :user # sign_out(scope)
|
59
|
+
# sign_out @user # sign_out(resource)
|
60
|
+
#
|
61
|
+
def sign_out(resource_or_scope)
|
62
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
63
|
+
@controller.instance_variable_set(:"@current_#{scope}", nil)
|
64
|
+
user = warden.instance_variable_get(:@users).delete(scope)
|
65
|
+
warden.session_serializer.delete(scope, user)
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
# Catch warden continuations and handle like the middleware would.
|
71
|
+
# Returns nil when interrupted, otherwise the normal result of the block.
|
72
|
+
def _catch_warden(&block)
|
73
|
+
result = catch(:warden, &block)
|
74
|
+
|
75
|
+
env = @controller.request.env
|
76
|
+
|
77
|
+
result ||= {}
|
78
|
+
|
79
|
+
# Set the response. In production, the rack result is returned
|
80
|
+
# from Warden::Manager#call, which the following is modelled on.
|
81
|
+
case result
|
82
|
+
when Array
|
83
|
+
if result.first == 401 && intercept_401?(env) # does this happen during testing?
|
84
|
+
_process_unauthenticated(env)
|
85
|
+
else
|
86
|
+
result
|
87
|
+
end
|
88
|
+
when Hash
|
89
|
+
_process_unauthenticated(env, result)
|
90
|
+
else
|
91
|
+
result
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def _process_unauthenticated(env, options = {})
|
96
|
+
options[:action] ||= :unauthenticated
|
97
|
+
proxy = env['warden']
|
98
|
+
result = options[:result] || proxy.result
|
99
|
+
|
100
|
+
ret = case result
|
101
|
+
when :redirect
|
102
|
+
body = proxy.message || "You are being redirected to #{proxy.headers['Location']}"
|
103
|
+
[proxy.status, proxy.headers, [body]]
|
104
|
+
when :custom
|
105
|
+
proxy.custom_response
|
106
|
+
else
|
107
|
+
env["PATH_INFO"] = "/#{options[:action]}"
|
108
|
+
env["warden.options"] = options
|
109
|
+
Warden::Manager._run_callbacks(:before_failure, env, options)
|
110
|
+
|
111
|
+
status, headers, response = Devise.warden_config[:failure_app].call(env).to_a
|
112
|
+
@controller.send :render, :status => status, :text => response.body,
|
113
|
+
:content_type => headers["Content-Type"], :location => headers["Location"]
|
114
|
+
nil # causes process return @response
|
115
|
+
end
|
116
|
+
|
117
|
+
# ensure that the controller response is set up. In production, this is
|
118
|
+
# not necessary since warden returns the results to rack. However, at
|
119
|
+
# testing time, we want the response to be available to the testing
|
120
|
+
# framework to verify what would be returned to rack.
|
121
|
+
if ret.is_a?(Array)
|
122
|
+
# ensure the controller response is set to our response.
|
123
|
+
@controller.response ||= @response
|
124
|
+
@response.status = ret.first
|
125
|
+
@response.headers = ret.second
|
126
|
+
@response.body = ret.third
|
127
|
+
end
|
128
|
+
|
129
|
+
ret
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "active_support/core_ext/module/delegation"
|
3
|
+
|
4
|
+
module Devise
|
5
|
+
class TimeInflector
|
6
|
+
include ActionView::Helpers::DateHelper
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_reader :instance
|
10
|
+
delegate :time_ago_in_words, :to => :instance
|
11
|
+
end
|
12
|
+
|
13
|
+
@instance = new
|
14
|
+
end
|
15
|
+
end
|