shingara-devise 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +119 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +253 -0
- data/Rakefile +45 -0
- data/TODO +5 -0
- data/app/controllers/confirmations_controller.rb +33 -0
- data/app/controllers/passwords_controller.rb +41 -0
- data/app/controllers/sessions_controller.rb +33 -0
- data/app/models/devise_mailer.rb +53 -0
- data/app/views/confirmations/new.html.erb +16 -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/passwords/edit.html.erb +20 -0
- data/app/views/passwords/new.html.erb +16 -0
- data/app/views/sessions/new.html.erb +23 -0
- data/generators/devise/USAGE +5 -0
- data/generators/devise/devise_generator.rb +25 -0
- data/generators/devise/lib/route_devise.rb +32 -0
- data/generators/devise/templates/README +22 -0
- data/generators/devise/templates/migration.rb +20 -0
- data/generators/devise/templates/model.rb +5 -0
- data/generators/devise_install/USAGE +3 -0
- data/generators/devise_install/devise_install_generator.rb +9 -0
- data/generators/devise_install/templates/devise.rb +47 -0
- data/generators/devise_views/USAGE +3 -0
- data/generators/devise_views/devise_views_generator.rb +24 -0
- data/init.rb +2 -0
- data/lib/devise/controllers/filters.rb +111 -0
- data/lib/devise/controllers/helpers.rb +130 -0
- data/lib/devise/controllers/url_helpers.rb +49 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +28 -0
- data/lib/devise/encryptors/clearance_sha1.rb +26 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +29 -0
- data/lib/devise/encryptors/sha1.rb +34 -0
- data/lib/devise/encryptors/sha512.rb +34 -0
- data/lib/devise/failure.rb +36 -0
- data/lib/devise/hooks/confirmable.rb +11 -0
- data/lib/devise/hooks/rememberable.rb +27 -0
- data/lib/devise/locales/en.yml +18 -0
- data/lib/devise/mapping.rb +120 -0
- data/lib/devise/migrations.rb +57 -0
- data/lib/devise/models/authenticatable.rb +87 -0
- data/lib/devise/models/confirmable.rb +156 -0
- data/lib/devise/models/recoverable.rb +88 -0
- data/lib/devise/models/rememberable.rb +95 -0
- data/lib/devise/models/validatable.rb +36 -0
- data/lib/devise/models.rb +110 -0
- data/lib/devise/orm/mongo_mapper.rb +26 -0
- data/lib/devise/rails/routes.rb +109 -0
- data/lib/devise/rails/warden_compat.rb +26 -0
- data/lib/devise/rails.rb +17 -0
- data/lib/devise/strategies/authenticatable.rb +46 -0
- data/lib/devise/strategies/base.rb +24 -0
- data/lib/devise/strategies/rememberable.rb +35 -0
- data/lib/devise/version.rb +3 -0
- data/lib/devise/warden.rb +20 -0
- data/lib/devise.rb +130 -0
- data/test/controllers/filters_test.rb +103 -0
- data/test/controllers/helpers_test.rb +55 -0
- data/test/controllers/url_helpers_test.rb +47 -0
- data/test/devise_test.rb +72 -0
- data/test/encryptors_test.rb +28 -0
- data/test/failure_test.rb +34 -0
- data/test/integration/authenticatable_test.rb +195 -0
- data/test/integration/confirmable_test.rb +89 -0
- data/test/integration/recoverable_test.rb +131 -0
- data/test/integration/rememberable_test.rb +65 -0
- data/test/mailers/confirmation_instructions_test.rb +59 -0
- data/test/mailers/reset_password_instructions_test.rb +62 -0
- data/test/mapping_test.rb +101 -0
- data/test/models/authenticatable_test.rb +130 -0
- data/test/models/confirmable_test.rb +237 -0
- data/test/models/recoverable_test.rb +141 -0
- data/test/models/rememberable_test.rb +130 -0
- data/test/models/validatable_test.rb +99 -0
- data/test/models_test.rb +111 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +10 -0
- data/test/rails_app/app/controllers/home_controller.rb +4 -0
- data/test/rails_app/app/controllers/users_controller.rb +7 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/models/account.rb +3 -0
- data/test/rails_app/app/models/admin.rb +3 -0
- data/test/rails_app/app/models/organizer.rb +3 -0
- data/test/rails_app/app/models/user.rb +3 -0
- data/test/rails_app/config/boot.rb +110 -0
- data/test/rails_app/config/environment.rb +41 -0
- data/test/rails_app/config/environments/development.rb +17 -0
- data/test/rails_app/config/environments/production.rb +28 -0
- data/test/rails_app/config/environments/test.rb +28 -0
- data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
- data/test/rails_app/config/initializers/session_store.rb +15 -0
- data/test/rails_app/config/routes.rb +18 -0
- data/test/routes_test.rb +79 -0
- data/test/support/assertions_helper.rb +22 -0
- data/test/support/integration_tests_helper.rb +66 -0
- data/test/support/model_tests_helper.rb +51 -0
- data/test/test_helper.rb +40 -0
- metadata +161 -0
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'devise/hooks/rememberable'
|
3
|
+
require 'devise/strategies/rememberable'
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Models
|
7
|
+
|
8
|
+
# Rememberable manages generating and clearing token for remember the user
|
9
|
+
# from a saved cookie. Rememberable also has utility methods for dealing
|
10
|
+
# with serializing the user into the cookie and back from the cookie, trying
|
11
|
+
# to lookup the record based on the saved information.
|
12
|
+
# You probably wouldn't use rememberable methods directly, they are used
|
13
|
+
# mostly internally for handling the remember token.
|
14
|
+
#
|
15
|
+
# Configuration:
|
16
|
+
#
|
17
|
+
# remember_for: the time you want the user will be remembered without
|
18
|
+
# asking for credentials. After this time the user will be
|
19
|
+
# blocked and will have to enter his credentials again.
|
20
|
+
# This configuration is also used to calculate the expires
|
21
|
+
# time for the cookie created to remember the user.
|
22
|
+
# By default remember_for is 2.weeks.
|
23
|
+
#
|
24
|
+
# Examples:
|
25
|
+
#
|
26
|
+
# User.find(1).remember_me! # regenerating the token
|
27
|
+
# User.find(1).forget_me! # clearing the token
|
28
|
+
#
|
29
|
+
# # generating info to put into cookies
|
30
|
+
# User.serialize_into_cookie(user)
|
31
|
+
#
|
32
|
+
# # lookup the user based on the incoming cookie information
|
33
|
+
# User.serialize_from_cookie(cookie_string)
|
34
|
+
module Rememberable
|
35
|
+
|
36
|
+
def self.included(base)
|
37
|
+
base.class_eval do
|
38
|
+
extend ClassMethods
|
39
|
+
|
40
|
+
# Remember me option available in after_authentication hook.
|
41
|
+
attr_accessor :remember_me
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Generate a new remember token and save the record without validations.
|
46
|
+
def remember_me!
|
47
|
+
self.remember_token = friendly_token
|
48
|
+
self.remember_created_at = Time.now.utc
|
49
|
+
save(false)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Removes the remember token only if it exists, and save the record
|
53
|
+
# without validations.
|
54
|
+
def forget_me!
|
55
|
+
if remember_token?
|
56
|
+
self.remember_token = nil
|
57
|
+
self.remember_created_at = nil
|
58
|
+
save(false)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Checks whether the incoming token matches or not with the record token.
|
63
|
+
def valid_remember_token?(token)
|
64
|
+
remember_token? && !remember_expired? && remember_token == token
|
65
|
+
end
|
66
|
+
|
67
|
+
# Remember token should be expired if expiration time not overpass now.
|
68
|
+
def remember_expired?
|
69
|
+
remember_expires_at <= Time.now.utc
|
70
|
+
end
|
71
|
+
|
72
|
+
# Remember token expires at created time + remember_for configuration
|
73
|
+
def remember_expires_at
|
74
|
+
remember_created_at + remember_for
|
75
|
+
end
|
76
|
+
|
77
|
+
module ClassMethods
|
78
|
+
|
79
|
+
# Create the cookie key using the record id and remember_token
|
80
|
+
def serialize_into_cookie(rememberable)
|
81
|
+
"#{rememberable.id}::#{rememberable.remember_token}"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Recreate the user based on the stored cookie
|
85
|
+
def serialize_from_cookie(cookie)
|
86
|
+
rememberable_id, remember_token = cookie.split('::')
|
87
|
+
rememberable = find_by_id(rememberable_id) if rememberable_id
|
88
|
+
rememberable if rememberable.try(:valid_remember_token?, remember_token)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
Devise::Models.config(self, :remember_for)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Validatable creates all needed validations for a user email and password.
|
5
|
+
# It's optional, given you may want to create the validations by yourself.
|
6
|
+
# Automatically validate if the email is present, unique and it's format is
|
7
|
+
# valid. Also tests presence of password, confirmation and length
|
8
|
+
module Validatable
|
9
|
+
|
10
|
+
# Email regex used to validate email formats. Retrieved from authlogic.
|
11
|
+
EMAIL_REGEX = /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)\z/i
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
|
16
|
+
validates_presence_of :email
|
17
|
+
validates_uniqueness_of :email, :allow_blank => true
|
18
|
+
validates_format_of :email, :with => EMAIL_REGEX, :allow_blank => true
|
19
|
+
|
20
|
+
validates_presence_of :password, :if => :password_required?
|
21
|
+
validates_confirmation_of :password, :if => :password_required?
|
22
|
+
validates_length_of :password, :within => 6..20, :allow_blank => true, :if => :password_required?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# Checks whether a password is needed or not. For validations only.
|
29
|
+
# Passwords are always required if it's a new record, or if the password
|
30
|
+
# or confirmation are being set somewhere.
|
31
|
+
def password_required?
|
32
|
+
new_record? || !password.nil? || !password_confirmation.nil?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
# Creates configuration values for Devise and for the given module.
|
4
|
+
#
|
5
|
+
# Devise::Models.config(Devise::Authenticable, :stretches, 10)
|
6
|
+
#
|
7
|
+
# The line above creates:
|
8
|
+
#
|
9
|
+
# 1) An accessor called Devise.stretches, which value is used by default;
|
10
|
+
#
|
11
|
+
# 2) Some class methods for your model Model.stretches and Model.stretches=
|
12
|
+
# which have higher priority than Devise.stretches;
|
13
|
+
#
|
14
|
+
# 3) And an instance method stretches.
|
15
|
+
#
|
16
|
+
# To add the class methods you need to have a module ClassMethods defined
|
17
|
+
# inside the given class.
|
18
|
+
#
|
19
|
+
def self.config(mod, accessor) #:nodoc:
|
20
|
+
mod.class_eval <<-METHOD, __FILE__, __LINE__
|
21
|
+
def #{accessor}
|
22
|
+
self.class.#{accessor}
|
23
|
+
end
|
24
|
+
METHOD
|
25
|
+
|
26
|
+
mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__
|
27
|
+
def #{accessor}
|
28
|
+
if defined?(@#{accessor})
|
29
|
+
@#{accessor}
|
30
|
+
elsif superclass.respond_to?(:#{accessor})
|
31
|
+
superclass.#{accessor}
|
32
|
+
else
|
33
|
+
Devise.#{accessor}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def #{accessor}=(value)
|
38
|
+
@#{accessor} = value
|
39
|
+
end
|
40
|
+
METHOD
|
41
|
+
end
|
42
|
+
|
43
|
+
# Shortcut method for including all devise modules inside your model.
|
44
|
+
# You can give some extra options while declaring devise in your model:
|
45
|
+
#
|
46
|
+
# * except: convenient option that allows you to add all devise modules,
|
47
|
+
# removing only the modules you setup here:
|
48
|
+
#
|
49
|
+
# devise :all, :except => :rememberable
|
50
|
+
#
|
51
|
+
# You can also give the following configuration values in a hash: :pepper,
|
52
|
+
# :stretches, :confirm_within and :remember_for. Please check your Devise
|
53
|
+
# initialiazer for a complete description on those values.
|
54
|
+
#
|
55
|
+
# Examples:
|
56
|
+
#
|
57
|
+
# # include only authenticatable module (default)
|
58
|
+
# devise
|
59
|
+
#
|
60
|
+
# # include authenticatable + confirmable modules
|
61
|
+
# devise :confirmable
|
62
|
+
#
|
63
|
+
# # include authenticatable + recoverable modules
|
64
|
+
# devise :recoverable
|
65
|
+
#
|
66
|
+
# # include authenticatable + rememberable modules
|
67
|
+
# devise :rememberable
|
68
|
+
#
|
69
|
+
# # include authenticatable + validatable modules
|
70
|
+
# devise :validatable
|
71
|
+
#
|
72
|
+
# # include authenticatable + confirmable + recoverable + rememberable + validatable
|
73
|
+
# devise :confirmable, :recoverable, :rememberable, :validatable
|
74
|
+
#
|
75
|
+
# # shortcut to include all modules (same as above)
|
76
|
+
# devise :all
|
77
|
+
#
|
78
|
+
# # include all except recoverable
|
79
|
+
# devise :all, :except => :recoverable
|
80
|
+
#
|
81
|
+
def devise(*modules)
|
82
|
+
options = modules.extract_options!
|
83
|
+
|
84
|
+
modules = Devise::ALL if modules.include?(:all)
|
85
|
+
modules -= Array(options.delete(:except))
|
86
|
+
modules = [:authenticatable] | modules
|
87
|
+
|
88
|
+
modules.each do |m|
|
89
|
+
devise_modules << m.to_sym
|
90
|
+
include Devise::Models.const_get(m.to_s.classify)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Convert new keys to methods which overwrites Devise defaults
|
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
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
# Stores all modules included inside the model, so we are able to verify
|
105
|
+
# which routes are needed.
|
106
|
+
def devise_modules
|
107
|
+
@devise_modules ||= []
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Devise
|
2
|
+
module Orm
|
3
|
+
module MongoMapper
|
4
|
+
def authenticatable
|
5
|
+
key :email, String
|
6
|
+
key :encrypted_password, String
|
7
|
+
key :password_salt, String
|
8
|
+
end
|
9
|
+
|
10
|
+
def confirmable
|
11
|
+
key :confirmation_token, String
|
12
|
+
key :confirmed_at, DateTime
|
13
|
+
key :confirmation_sent_at, DateTime
|
14
|
+
end
|
15
|
+
|
16
|
+
def recoverable
|
17
|
+
key :reset_password_token, String
|
18
|
+
end
|
19
|
+
|
20
|
+
def rememberable
|
21
|
+
key :remember_token, String
|
22
|
+
key :remember_created_at, DateTime
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module ActionController::Routing
|
2
|
+
class RouteSet #:nodoc:
|
3
|
+
|
4
|
+
# Ensure Devise modules are included only after loading routes, because we
|
5
|
+
# need devise_for mappings already declared to create magic filters and
|
6
|
+
# helpers.
|
7
|
+
def load_routes_with_devise!
|
8
|
+
load_routes_without_devise!
|
9
|
+
|
10
|
+
ActionController::Base.send :include, Devise::Controllers::Filters
|
11
|
+
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
12
|
+
|
13
|
+
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
14
|
+
end
|
15
|
+
alias_method_chain :load_routes!, :devise
|
16
|
+
|
17
|
+
class Mapper #:doc:
|
18
|
+
# Includes devise_for method for routes. This method is responsible to
|
19
|
+
# generate all needed routes for devise, based on what modules you have
|
20
|
+
# defined in your model.
|
21
|
+
# Examples: Let's say you have an User model configured to use
|
22
|
+
# authenticatable, confirmable and recoverable modules. After creating this
|
23
|
+
# inside your routes:
|
24
|
+
#
|
25
|
+
# map.devise_for :users
|
26
|
+
#
|
27
|
+
# this method is going to look inside your User model and create the
|
28
|
+
# needed routes:
|
29
|
+
#
|
30
|
+
# # Session routes for Authenticatable (default)
|
31
|
+
# new_user_session GET /users/sign_in {:controller=>"sessions", :action=>"new"}
|
32
|
+
# user_session POST /users/sign_in {:controller=>"sessions", :action=>"create"}
|
33
|
+
# destroy_user_session GET /users/sign_out {:controller=>"sessions", :action=>"destroy"}
|
34
|
+
#
|
35
|
+
# # Password routes for Recoverable, if User model has :recoverable configured
|
36
|
+
# new_user_password GET /users/password/new(.:format) {:controller=>"passwords", :action=>"new"}
|
37
|
+
# edit_user_password GET /users/password/edit(.:format) {:controller=>"passwords", :action=>"edit"}
|
38
|
+
# user_password PUT /users/password(.:format) {:controller=>"passwords", :action=>"update"}
|
39
|
+
# POST /users/password(.:format) {:controller=>"passwords", :action=>"create"}
|
40
|
+
#
|
41
|
+
# # Confirmation routes for Confirmable, if User model has :confirmable configured
|
42
|
+
# new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
|
43
|
+
# user_confirmation GET /users/confirmation(.:format) {:controller=>"confirmations", :action=>"show"}
|
44
|
+
# POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"}
|
45
|
+
#
|
46
|
+
# You can configure your routes with some options:
|
47
|
+
# * :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name.
|
48
|
+
#
|
49
|
+
# map.devise_for :users, :class_name => 'Account'
|
50
|
+
#
|
51
|
+
# * :as => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users:
|
52
|
+
#
|
53
|
+
# map.devise_for :users, :as => 'accounts'
|
54
|
+
#
|
55
|
+
# * :singular => setup the name used to create named routes. By default, for a :users key, it is going to be the singularized version, :user. To configure a named route like account_session_path instead of user_session_path just do:
|
56
|
+
#
|
57
|
+
# map.devise_for :users, :singular => :account
|
58
|
+
#
|
59
|
+
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation.
|
60
|
+
#
|
61
|
+
# map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
62
|
+
#
|
63
|
+
# * :path_prefix => the path prefix to be used in all routes.
|
64
|
+
#
|
65
|
+
# map.devise_for :users, :path_prefix => "/:locale"
|
66
|
+
#
|
67
|
+
# 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
|
+
#
|
69
|
+
# Devise.default_url_options do
|
70
|
+
# { :locale => I18n.locale }
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
def devise_for(*resources)
|
74
|
+
options = resources.extract_options!
|
75
|
+
|
76
|
+
resources.map!(&:to_sym)
|
77
|
+
resources.each do |resource|
|
78
|
+
mapping = Devise::Mapping.new(resource, options)
|
79
|
+
Devise.mappings[mapping.name] = mapping
|
80
|
+
|
81
|
+
with_options(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_") do |routes|
|
82
|
+
mapping.for.each do |strategy|
|
83
|
+
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
91
|
+
def authenticatable(routes, mapping)
|
92
|
+
routes.with_options(:controller => 'sessions', :name_prefix => nil) do |session|
|
93
|
+
session.send(:"new_#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'new', :conditions => { :method => :get })
|
94
|
+
session.send(:"#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'create', :conditions => { :method => :post })
|
95
|
+
session.send(:"destroy_#{mapping.name}_session", mapping.path_names[:sign_out], :action => 'destroy', :conditions => { :method => :get })
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def recoverable(routes, mapping)
|
100
|
+
routes.resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password]
|
101
|
+
end
|
102
|
+
|
103
|
+
def confirmable(routes, mapping)
|
104
|
+
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Taken from RailsWarden, thanks to Hassox. http://github.com/hassox/rails_warden
|
2
|
+
module Warden::Mixins::Common
|
3
|
+
# Gets the rails request object by default if it's available
|
4
|
+
def request
|
5
|
+
return @request if @request
|
6
|
+
if env['action_controller.rescue.request']
|
7
|
+
@request = env['action_controller.rescue.request']
|
8
|
+
else
|
9
|
+
Rack::Request.new(env)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def raw_session
|
14
|
+
request.session
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_session!
|
18
|
+
raw_session.inspect # why do I have to inspect it to get it to clear?
|
19
|
+
raw_session.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
# Proxy to request cookies
|
23
|
+
def cookies
|
24
|
+
request.cookies
|
25
|
+
end
|
26
|
+
end
|
data/lib/devise/rails.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'devise/rails/routes'
|
2
|
+
require 'devise/rails/warden_compat'
|
3
|
+
|
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
|
+
# Adds Warden Manager to Rails middleware stack, configuring default devise
|
11
|
+
# strategy and also the failure app.
|
12
|
+
Rails.configuration.middleware.use Warden::Manager do |manager|
|
13
|
+
Devise.configure_warden_manager(manager)
|
14
|
+
end
|
15
|
+
|
16
|
+
I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'locales', 'en.yml'))
|
17
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Devise
|
2
|
+
module Strategies
|
3
|
+
# Default strategy for signing in a user, based on his email and password.
|
4
|
+
# Redirects to sign_in page if it's not authenticated
|
5
|
+
class Authenticatable < Devise::Strategies::Base
|
6
|
+
|
7
|
+
# Authenticate a user based on email and password params, returning to warden
|
8
|
+
# success and the authenticated user if everything is okay. Otherwise redirect
|
9
|
+
# to sign in page.
|
10
|
+
#
|
11
|
+
# Please notice the semantic difference between calling fail! and throw :warden.
|
12
|
+
# The first does not perform any action when calling authenticate, just
|
13
|
+
# when authenticate! is invoked. The second always perform the action.
|
14
|
+
def authenticate!
|
15
|
+
if valid_attributes? && resource = mapping.to.authenticate(attributes)
|
16
|
+
success!(resource)
|
17
|
+
else
|
18
|
+
store_location
|
19
|
+
fail!(:unauthenticated)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Find the attributes for the current mapping.
|
26
|
+
def attributes
|
27
|
+
@attributes ||= params[scope]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Check for the right keys.
|
31
|
+
def valid_attributes?
|
32
|
+
attributes && attributes[:email].present? && attributes[:password].present?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Stores requested uri to redirect the user after signing in. We cannot use
|
36
|
+
# scoped session provided by warden here, since the user is not authenticated
|
37
|
+
# yet, but we still need to store the uri based on scope, so different scopes
|
38
|
+
# would never use the same uri to redirect.
|
39
|
+
def store_location
|
40
|
+
session[:"#{mapping.name}.return_to"] = request.request_uri if request.get?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Warden::Strategies.add(:authenticatable, Devise::Strategies::Authenticatable)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Devise
|
2
|
+
module Strategies
|
3
|
+
# Base strategy for Devise. Responsible for verifying correct scope and
|
4
|
+
# mapping.
|
5
|
+
class Base < Warden::Strategies::Base
|
6
|
+
|
7
|
+
# Validate strategy. By default will raise an error if no scope or an
|
8
|
+
# invalid mapping is found.
|
9
|
+
def valid?
|
10
|
+
mapping.for.include?(self.class.name.split("::").last.underscore.to_sym)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Checks if a valid scope was given for devise and find mapping based on
|
14
|
+
# this scope.
|
15
|
+
def mapping
|
16
|
+
@mapping ||= begin
|
17
|
+
raise "You need to give a scope for Devise authentication" unless scope
|
18
|
+
raise "You need to give a valid Devise mapping" unless mapping = Devise.mappings[scope]
|
19
|
+
mapping
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
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)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
begin
|
2
|
+
require 'warden'
|
3
|
+
rescue
|
4
|
+
gem 'warden'
|
5
|
+
require 'warden'
|
6
|
+
end
|
7
|
+
|
8
|
+
# Session Serialization in. This block determines how the user will be stored
|
9
|
+
# in the session. If you're using a complex object like an ActiveRecord model,
|
10
|
+
# it is not a good idea to store the complete object. An ID is sufficient.
|
11
|
+
Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
|
12
|
+
|
13
|
+
# Session Serialization out. This block gets the user out of the session.
|
14
|
+
# It should be the reverse of serializing the object into the session
|
15
|
+
Warden::Manager.serialize_from_session do |klass, id|
|
16
|
+
klass.find_by_id(id)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Setup devise strategies for Warden
|
20
|
+
require 'devise/strategies/base'
|
data/lib/devise.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
module Devise
|
2
|
+
ALL = [:authenticatable, :confirmable, :recoverable, :rememberable, :validatable].freeze
|
3
|
+
|
4
|
+
# Maps controller names to devise modules
|
5
|
+
CONTROLLERS = {
|
6
|
+
:sessions => :authenticatable,
|
7
|
+
:passwords => :recoverable,
|
8
|
+
:confirmations => :confirmable
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
STRATEGIES = [:rememberable, :authenticatable].freeze
|
12
|
+
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
|
13
|
+
|
14
|
+
# Maps the messages types that comes from warden to a flash type.
|
15
|
+
# This hash is not frozen, so you can add your messages as well.
|
16
|
+
FLASH_MESSAGES = {
|
17
|
+
:unauthenticated => :success,
|
18
|
+
:unconfirmed => :failure
|
19
|
+
}
|
20
|
+
|
21
|
+
# Declare encryptors length which are used in migrations.
|
22
|
+
ENCRYPTORS_LENGTH = {
|
23
|
+
:sha1 => 40,
|
24
|
+
:sha512 => 128,
|
25
|
+
:clearance_sha1 => 40,
|
26
|
+
:restful_authentication_sha1 => 40,
|
27
|
+
:authlogic_sha512 => 128
|
28
|
+
}
|
29
|
+
|
30
|
+
# Used to encrypt password. Please generate one with rake secret
|
31
|
+
mattr_accessor :pepper
|
32
|
+
@@pepper = nil
|
33
|
+
|
34
|
+
# The number of times to encrypt password.
|
35
|
+
mattr_accessor :stretches
|
36
|
+
@@stretches = 10
|
37
|
+
|
38
|
+
# Time interval where the remember me token is valid.
|
39
|
+
mattr_accessor :remember_for
|
40
|
+
@@remember_for = 2.weeks
|
41
|
+
|
42
|
+
# Time interval you can access your account before confirming your account.
|
43
|
+
mattr_accessor :confirm_within
|
44
|
+
@@confirm_within = 0.days
|
45
|
+
|
46
|
+
# Used to define the password encryption algorithm.
|
47
|
+
def self.encryptor=(value)
|
48
|
+
@@encryptor = if value.is_a?(Symbol)
|
49
|
+
::Devise::Encryptors.const_get(value.to_s.classify)
|
50
|
+
else
|
51
|
+
value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
mattr_reader :encryptor
|
55
|
+
@@encryptor = ::Devise::Encryptors::Sha1
|
56
|
+
|
57
|
+
# Store scopes mappings.
|
58
|
+
mattr_accessor :mappings
|
59
|
+
@@mappings = {}
|
60
|
+
|
61
|
+
@@orm = 'active_record'
|
62
|
+
|
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
|
+
class << self
|
79
|
+
# Default way to setup Devise. Run script/generate devise_install to create
|
80
|
+
# a fresh initializer with all configuration values.
|
81
|
+
def setup
|
82
|
+
yield self
|
83
|
+
end
|
84
|
+
|
85
|
+
def mail_sender=(value) #:nodoc:
|
86
|
+
ActiveSupport::Deprecation.warn "Devise.mail_sender= is deprecated, use Devise.mailer_sender instead"
|
87
|
+
DeviseMailer.sender = value
|
88
|
+
end
|
89
|
+
|
90
|
+
# Sets the sender in DeviseMailer.
|
91
|
+
def mailer_sender=(value)
|
92
|
+
DeviseMailer.sender = value
|
93
|
+
end
|
94
|
+
alias :sender= :mailer_sender=
|
95
|
+
|
96
|
+
# Sets warden configuration using a block that will be invoked on warden
|
97
|
+
# initialization.
|
98
|
+
#
|
99
|
+
# Devise.initialize do |config|
|
100
|
+
# config.confirm_within = 2.days
|
101
|
+
#
|
102
|
+
# config.warden do |manager|
|
103
|
+
# # Configure warden to use other strategies, like oauth.
|
104
|
+
# manager.oauth(:twitter)
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
def warden(&block)
|
108
|
+
@warden_config = block
|
109
|
+
end
|
110
|
+
|
111
|
+
# Configure default url options to be used within Devise and ActionController.
|
112
|
+
def default_url_options(&block)
|
113
|
+
Devise::Mapping.metaclass.send :define_method, :default_url_options, &block
|
114
|
+
end
|
115
|
+
|
116
|
+
# A method used internally to setup warden manager from the Rails initialize
|
117
|
+
# block.
|
118
|
+
def configure_warden_manager(manager) #:nodoc:
|
119
|
+
manager.default_strategies *Devise::STRATEGIES
|
120
|
+
manager.failure_app = Devise::Failure
|
121
|
+
manager.silence_missing_strategies!
|
122
|
+
|
123
|
+
# If the user provided a warden hook, call it now.
|
124
|
+
@warden_config.try :call, manager
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
require 'devise/warden'
|
130
|
+
require 'devise/rails'
|