mongoid-devise 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +333 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +260 -0
- data/Rakefile +53 -0
- data/TODO +2 -0
- data/app/controllers/confirmations_controller.rb +33 -0
- data/app/controllers/passwords_controller.rb +42 -0
- data/app/controllers/registrations_controller.rb +55 -0
- data/app/controllers/sessions_controller.rb +45 -0
- data/app/controllers/unlocks_controller.rb +33 -0
- data/app/models/devise_mailer.rb +68 -0
- data/app/views/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/passwords/edit.html.erb +16 -0
- data/app/views/passwords/new.html.erb +12 -0
- data/app/views/registrations/edit.html.erb +25 -0
- data/app/views/registrations/new.html.erb +17 -0
- data/app/views/sessions/new.html.erb +17 -0
- data/app/views/shared/_devise_links.erb +19 -0
- data/app/views/unlocks/new.html.erb +12 -0
- data/generators/devise/USAGE +5 -0
- data/generators/devise/devise_generator.rb +15 -0
- data/generators/devise/lib/route_devise.rb +32 -0
- data/generators/devise/templates/migration.rb +23 -0
- data/generators/devise/templates/model.rb +9 -0
- data/generators/devise_install/USAGE +3 -0
- data/generators/devise_install/devise_install_generator.rb +15 -0
- data/generators/devise_install/templates/README +18 -0
- data/generators/devise_install/templates/devise.rb +102 -0
- data/generators/devise_views/USAGE +3 -0
- data/generators/devise_views/devise_views_generator.rb +21 -0
- data/init.rb +2 -0
- data/lib/devise.rb +253 -0
- data/lib/devise/controllers/helpers.rb +200 -0
- data/lib/devise/controllers/internal_helpers.rb +129 -0
- data/lib/devise/controllers/url_helpers.rb +41 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +21 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/bcrypt.rb +21 -0
- data/lib/devise/encryptors/clearance_sha1.rb +19 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +27 -0
- data/lib/devise/encryptors/sha512.rb +27 -0
- data/lib/devise/failure_app.rb +65 -0
- data/lib/devise/hooks/activatable.rb +15 -0
- data/lib/devise/hooks/rememberable.rb +30 -0
- data/lib/devise/hooks/timeoutable.rb +18 -0
- data/lib/devise/hooks/trackable.rb +18 -0
- data/lib/devise/locales/en.yml +35 -0
- data/lib/devise/mapping.rb +131 -0
- data/lib/devise/models.rb +112 -0
- data/lib/devise/models/activatable.rb +16 -0
- data/lib/devise/models/authenticatable.rb +146 -0
- data/lib/devise/models/confirmable.rb +172 -0
- data/lib/devise/models/http_authenticatable.rb +21 -0
- data/lib/devise/models/lockable.rb +160 -0
- data/lib/devise/models/recoverable.rb +80 -0
- data/lib/devise/models/registerable.rb +8 -0
- data/lib/devise/models/rememberable.rb +94 -0
- data/lib/devise/models/timeoutable.rb +28 -0
- data/lib/devise/models/token_authenticatable.rb +89 -0
- data/lib/devise/models/trackable.rb +16 -0
- data/lib/devise/models/validatable.rb +48 -0
- data/lib/devise/orm/active_record.rb +41 -0
- data/lib/devise/orm/data_mapper.rb +83 -0
- data/lib/devise/orm/mongo_mapper.rb +51 -0
- data/lib/devise/orm/mongoid.rb +60 -0
- data/lib/devise/rails.rb +14 -0
- data/lib/devise/rails/routes.rb +125 -0
- data/lib/devise/rails/warden_compat.rb +25 -0
- data/lib/devise/schema.rb +65 -0
- data/lib/devise/strategies/authenticatable.rb +36 -0
- data/lib/devise/strategies/base.rb +16 -0
- data/lib/devise/strategies/http_authenticatable.rb +49 -0
- data/lib/devise/strategies/rememberable.rb +37 -0
- data/lib/devise/strategies/token_authenticatable.rb +37 -0
- data/lib/devise/test_helpers.rb +86 -0
- data/lib/devise/version.rb +3 -0
- data/test/controllers/helpers_test.rb +177 -0
- data/test/controllers/internal_helpers_test.rb +55 -0
- data/test/controllers/url_helpers_test.rb +47 -0
- data/test/devise_test.rb +69 -0
- data/test/encryptors_test.rb +31 -0
- data/test/failure_app_test.rb +44 -0
- data/test/integration/authenticatable_test.rb +271 -0
- data/test/integration/confirmable_test.rb +97 -0
- data/test/integration/http_authenticatable_test.rb +44 -0
- data/test/integration/lockable_test.rb +83 -0
- data/test/integration/recoverable_test.rb +141 -0
- data/test/integration/registerable_test.rb +130 -0
- data/test/integration/rememberable_test.rb +63 -0
- data/test/integration/timeoutable_test.rb +68 -0
- data/test/integration/token_authenticatable_test.rb +55 -0
- data/test/integration/trackable_test.rb +64 -0
- data/test/mailers/confirmation_instructions_test.rb +80 -0
- data/test/mailers/reset_password_instructions_test.rb +68 -0
- data/test/mailers/unlock_instructions_test.rb +62 -0
- data/test/mapping_test.rb +153 -0
- data/test/models/authenticatable_test.rb +180 -0
- data/test/models/confirmable_test.rb +228 -0
- data/test/models/lockable_test.rb +202 -0
- data/test/models/recoverable_test.rb +138 -0
- data/test/models/rememberable_test.rb +135 -0
- data/test/models/timeoutable_test.rb +28 -0
- data/test/models/token_authenticatable_test.rb +51 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +106 -0
- data/test/models_test.rb +56 -0
- data/test/orm/active_record.rb +31 -0
- data/test/orm/mongo_mapper.rb +20 -0
- data/test/orm/mongoid.rb +22 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/user.rb +7 -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 +16 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mongo_mapper/admin.rb +9 -0
- data/test/rails_app/app/mongo_mapper/user.rb +8 -0
- data/test/rails_app/app/mongoid/admin.rb +9 -0
- data/test/rails_app/app/mongoid/user.rb +8 -0
- data/test/rails_app/config/boot.rb +110 -0
- data/test/rails_app/config/environment.rb +42 -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/devise.rb +79 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/new_rails_defaults.rb +24 -0
- data/test/rails_app/config/initializers/session_store.rb +15 -0
- data/test/rails_app/config/routes.rb +21 -0
- data/test/routes_test.rb +110 -0
- data/test/support/assertions_helper.rb +37 -0
- data/test/support/integration_tests_helper.rb +71 -0
- data/test/support/test_silencer.rb +5 -0
- data/test/support/tests_helper.rb +39 -0
- data/test/test_helper.rb +21 -0
- data/test/test_helpers_test.rb +57 -0
- metadata +216 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
module Devise
|
2
|
+
module Orm
|
3
|
+
module MongoMapper
|
4
|
+
module InstanceMethods
|
5
|
+
def save(options={})
|
6
|
+
if options == false
|
7
|
+
super(:validate => false)
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included_modules_hook(klass)
|
15
|
+
klass.send :extend, self
|
16
|
+
klass.send :include, InstanceMethods
|
17
|
+
|
18
|
+
yield
|
19
|
+
|
20
|
+
klass.devise_modules.each do |mod|
|
21
|
+
klass.send(mod) if klass.respond_to?(mod)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def find(*args)
|
26
|
+
options = args.extract_options!
|
27
|
+
case args.first
|
28
|
+
when :first
|
29
|
+
first(options)
|
30
|
+
when :all
|
31
|
+
all(options)
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
include Devise::Schema
|
38
|
+
|
39
|
+
# Tell how to apply schema methods. This automatically converts DateTime
|
40
|
+
# to Time, since MongoMapper does not recognize the former.
|
41
|
+
def apply_schema(name, type, options={})
|
42
|
+
return unless Devise.apply_schema
|
43
|
+
type = Time if type == DateTime
|
44
|
+
key name, type, options
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
MongoMapper::Document::ClassMethods.send(:include, Devise::Models)
|
51
|
+
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Devise::Models)
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesUniquenessOf < Validatable::ValidationBase
|
3
|
+
# with devise, we need scope.
|
4
|
+
# This little hack no usefull with activeModel and Rails 3.
|
5
|
+
# So delete it with devise 1.1
|
6
|
+
def scope
|
7
|
+
if @scope == []
|
8
|
+
nil
|
9
|
+
else
|
10
|
+
@scope
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def message(instance)
|
15
|
+
"has already been taken"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Devise
|
21
|
+
module Orm
|
22
|
+
module Mongoid
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
|
26
|
+
def reload
|
27
|
+
super
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.included_modules_hook(klass)
|
33
|
+
klass.send :extend, self
|
34
|
+
# TODO: it's a little hack. Patch pull on master
|
35
|
+
klass.send :include, InstanceMethods
|
36
|
+
klass.send :include, ::Mongoid::Timestamps
|
37
|
+
|
38
|
+
yield
|
39
|
+
|
40
|
+
klass.devise_modules.each do |mod|
|
41
|
+
klass.send(mod) if klass.respond_to?(mod)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
include Devise::Schema
|
47
|
+
|
48
|
+
# Tell how to apply schema methods. This automatically converts DateTime
|
49
|
+
# to Time, since MongoMapper does not recognize the former.
|
50
|
+
def apply_schema(name, type, options={})
|
51
|
+
return unless Devise.apply_schema
|
52
|
+
type = Time if type == DateTime
|
53
|
+
field name, {:type => type}.merge(options)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Mongoid::Document::ClassMethods.send(:include, Devise::Models)
|
data/lib/devise/rails.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'devise/rails/routes'
|
2
|
+
require 'devise/rails/warden_compat'
|
3
|
+
|
4
|
+
Rails.configuration.after_initialize do
|
5
|
+
require "devise/orm/#{Devise.orm}"
|
6
|
+
|
7
|
+
# Adds Warden Manager to Rails middleware stack, configuring default devise
|
8
|
+
# strategy and also the failure app.
|
9
|
+
Rails.configuration.middleware.use Warden::Manager do |config|
|
10
|
+
Devise.configure_warden(config)
|
11
|
+
end
|
12
|
+
|
13
|
+
I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'locales', 'en.yml'))
|
14
|
+
end
|
@@ -0,0 +1,125 @@
|
|
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
|
+
return if Devise.mappings.empty?
|
10
|
+
|
11
|
+
ActionController::Base.send :include, Devise::Controllers::Helpers
|
12
|
+
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
13
|
+
|
14
|
+
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
15
|
+
end
|
16
|
+
alias_method_chain :load_routes!, :devise
|
17
|
+
|
18
|
+
class Mapper #:doc:
|
19
|
+
# Includes devise_for method for routes. This method is responsible to
|
20
|
+
# generate all needed routes for devise, based on what modules you have
|
21
|
+
# defined in your model.
|
22
|
+
# Examples: Let's say you have an User model configured to use
|
23
|
+
# authenticatable, confirmable and recoverable modules. After creating this
|
24
|
+
# inside your routes:
|
25
|
+
#
|
26
|
+
# map.devise_for :users
|
27
|
+
#
|
28
|
+
# this method is going to look inside your User model and create the
|
29
|
+
# needed routes:
|
30
|
+
#
|
31
|
+
# # Session routes for Authenticatable (default)
|
32
|
+
# new_user_session GET /users/sign_in {:controller=>"sessions", :action=>"new"}
|
33
|
+
# user_session POST /users/sign_in {:controller=>"sessions", :action=>"create"}
|
34
|
+
# destroy_user_session GET /users/sign_out {:controller=>"sessions", :action=>"destroy"}
|
35
|
+
#
|
36
|
+
# # Password routes for Recoverable, if User model has :recoverable configured
|
37
|
+
# new_user_password GET /users/password/new(.:format) {:controller=>"passwords", :action=>"new"}
|
38
|
+
# edit_user_password GET /users/password/edit(.:format) {:controller=>"passwords", :action=>"edit"}
|
39
|
+
# user_password PUT /users/password(.:format) {:controller=>"passwords", :action=>"update"}
|
40
|
+
# POST /users/password(.:format) {:controller=>"passwords", :action=>"create"}
|
41
|
+
#
|
42
|
+
# # Confirmation routes for Confirmable, if User model has :confirmable configured
|
43
|
+
# new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
|
44
|
+
# user_confirmation GET /users/confirmation(.:format) {:controller=>"confirmations", :action=>"show"}
|
45
|
+
# POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"}
|
46
|
+
#
|
47
|
+
# You can configure your routes with some options:
|
48
|
+
#
|
49
|
+
# * :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name.
|
50
|
+
#
|
51
|
+
# map.devise_for :users, :class_name => 'Account'
|
52
|
+
#
|
53
|
+
# * :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:
|
54
|
+
#
|
55
|
+
# map.devise_for :users, :as => 'accounts'
|
56
|
+
#
|
57
|
+
# * :scope => setup the scope name. This is used as the instance variable name in controller, as the name in routes and the scope given to warden. Defaults to the singular of the given name:
|
58
|
+
#
|
59
|
+
# map.devise_for :users, :scope => :account
|
60
|
+
#
|
61
|
+
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation.
|
62
|
+
#
|
63
|
+
# map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
64
|
+
#
|
65
|
+
# * :path_prefix => the path prefix to be used in all routes.
|
66
|
+
#
|
67
|
+
# map.devise_for :users, :path_prefix => "/:locale"
|
68
|
+
#
|
69
|
+
# Any other options will be passed to route definition. If you need conditions for your routes, just map:
|
70
|
+
#
|
71
|
+
# map.devise_for :users, :conditions => { :subdomain => /.+/ }
|
72
|
+
#
|
73
|
+
# 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:
|
74
|
+
#
|
75
|
+
# Devise.default_url_options do
|
76
|
+
# { :locale => I18n.locale }
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
def devise_for(*resources)
|
80
|
+
options = resources.extract_options!
|
81
|
+
|
82
|
+
resources.map!(&:to_sym)
|
83
|
+
resources.each do |resource|
|
84
|
+
mapping = Devise::Mapping.new(resource, options.dup)
|
85
|
+
Devise.default_scope ||= mapping.name
|
86
|
+
Devise.mappings[mapping.name] = mapping
|
87
|
+
|
88
|
+
route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
|
89
|
+
|
90
|
+
with_options(route_options) do |routes|
|
91
|
+
mapping.for.each do |mod|
|
92
|
+
send(mod, routes, mapping) if self.respond_to?(mod, true)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
def authenticatable(routes, mapping)
|
101
|
+
routes.with_options(:controller => 'sessions', :name_prefix => nil) do |session|
|
102
|
+
session.send(:"new_#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'new', :conditions => { :method => :get })
|
103
|
+
session.send(:"#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'create', :conditions => { :method => :post })
|
104
|
+
session.send(:"destroy_#{mapping.name}_session", mapping.path_names[:sign_out], :action => 'destroy', :conditions => { :method => :get })
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def confirmable(routes, mapping)
|
109
|
+
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
110
|
+
end
|
111
|
+
|
112
|
+
def lockable(routes, mapping)
|
113
|
+
routes.resource :unlock, :only => [:new, :create, :show], :as => mapping.path_names[:unlock]
|
114
|
+
end
|
115
|
+
|
116
|
+
def recoverable(routes, mapping)
|
117
|
+
routes.resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password]
|
118
|
+
end
|
119
|
+
|
120
|
+
def registerable(routes, mapping)
|
121
|
+
routes.resource :registration, :only => [:new, :create, :edit, :update, :destroy], :as => mapping.raw_path[1..-1], :path_prefix => nil, :path_names => { :new => mapping.path_names[:sign_up] }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Warden::Mixins::Common
|
2
|
+
def request
|
3
|
+
@request ||= env['action_controller.rescue.request']
|
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 response
|
12
|
+
@response ||= env['action_controller.rescue.response']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Warden::SessionSerializer
|
17
|
+
def serialize(record)
|
18
|
+
[record.class, record.id]
|
19
|
+
end
|
20
|
+
|
21
|
+
def deserialize(keys)
|
22
|
+
klass, id = keys
|
23
|
+
klass.find(:first, :conditions => { :id => id })
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,65 @@
|
|
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
|
+
# Creates email, encrypted_password and password_salt.
|
7
|
+
#
|
8
|
+
# == Options
|
9
|
+
# * :null - When true, allow columns to be null.
|
10
|
+
# * :encryptor - The encryptor going to be used, necessary for setting the proper encrypter password length.
|
11
|
+
def authenticatable(options={})
|
12
|
+
null = options[:null] || false
|
13
|
+
encryptor = options[:encryptor] || (respond_to?(:encryptor) ? self.encryptor : :sha1)
|
14
|
+
|
15
|
+
apply_schema :email, String, :null => null
|
16
|
+
apply_schema :encrypted_password, String, :null => null, :limit => Devise::ENCRYPTORS_LENGTH[encryptor]
|
17
|
+
apply_schema :password_salt, String, :null => null
|
18
|
+
end
|
19
|
+
|
20
|
+
# Creates authentication_token.
|
21
|
+
def token_authenticatable
|
22
|
+
apply_schema :authentication_token, String, :limit => 20
|
23
|
+
end
|
24
|
+
|
25
|
+
# Creates confirmation_token, confirmed_at and confirmation_sent_at.
|
26
|
+
def confirmable
|
27
|
+
apply_schema :confirmation_token, String, :limit => 20
|
28
|
+
apply_schema :confirmed_at, DateTime
|
29
|
+
apply_schema :confirmation_sent_at, DateTime
|
30
|
+
end
|
31
|
+
|
32
|
+
# Creates reset_password_token.
|
33
|
+
def recoverable
|
34
|
+
apply_schema :reset_password_token, String, :limit => 20
|
35
|
+
end
|
36
|
+
|
37
|
+
# Creates remember_token and remember_created_at.
|
38
|
+
def rememberable
|
39
|
+
apply_schema :remember_token, String, :limit => 20
|
40
|
+
apply_schema :remember_created_at, DateTime
|
41
|
+
end
|
42
|
+
|
43
|
+
# Creates sign_in_count, current_sign_in_at, last_sign_in_at,
|
44
|
+
# current_sign_in_ip, last_sign_in_ip.
|
45
|
+
def trackable
|
46
|
+
apply_schema :sign_in_count, Integer
|
47
|
+
apply_schema :current_sign_in_at, DateTime
|
48
|
+
apply_schema :last_sign_in_at, DateTime
|
49
|
+
apply_schema :current_sign_in_ip, String
|
50
|
+
apply_schema :last_sign_in_ip, String
|
51
|
+
end
|
52
|
+
|
53
|
+
# Creates failed_attempts, unlock_token and locked_at
|
54
|
+
def lockable
|
55
|
+
apply_schema :failed_attempts, Integer, :default => 0
|
56
|
+
apply_schema :unlock_token, String, :limit => 20
|
57
|
+
apply_schema :locked_at, DateTime
|
58
|
+
end
|
59
|
+
|
60
|
+
# Overwrite with specific modification to create your own schema.
|
61
|
+
def apply_schema(name, type, options={})
|
62
|
+
raise NotImplementedError
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'devise/strategies/base'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Strategies
|
5
|
+
# Default strategy for signing in a user, based on his email and password.
|
6
|
+
# Redirects to sign_in page if it's not authenticated
|
7
|
+
class Authenticatable < Base
|
8
|
+
def valid?
|
9
|
+
valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Authenticate a user based on email and password params, returning to warden
|
13
|
+
# success and the authenticated user if everything is okay. Otherwise redirect
|
14
|
+
# to sign in page.
|
15
|
+
def authenticate!
|
16
|
+
if resource = mapping.to.authenticate(params[scope])
|
17
|
+
success!(resource)
|
18
|
+
else
|
19
|
+
fail!(:invalid)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def valid_controller?
|
26
|
+
params[:controller] == 'sessions'
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid_params?
|
30
|
+
params[scope] && params[scope][:password].present?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Warden::Strategies.add(:authenticatable, Devise::Strategies::Authenticatable)
|
@@ -0,0 +1,16 @@
|
|
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
|
6
|
+
# this scope.
|
7
|
+
def mapping
|
8
|
+
@mapping ||= begin
|
9
|
+
mapping = Devise.mappings[scope]
|
10
|
+
raise "Could not find mapping for #{scope}" unless mapping
|
11
|
+
mapping
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'devise/strategies/base'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Strategies
|
5
|
+
# Sign in an user using HTTP authentication.
|
6
|
+
class HttpAuthenticatable < Base
|
7
|
+
def valid?
|
8
|
+
http_authentication? && mapping.to.respond_to?(:authenticate_with_http)
|
9
|
+
end
|
10
|
+
|
11
|
+
def authenticate!
|
12
|
+
username, password = username_and_password
|
13
|
+
|
14
|
+
if resource = mapping.to.authenticate_with_http(username, password)
|
15
|
+
success!(resource)
|
16
|
+
else
|
17
|
+
custom!([401, custom_headers, ["HTTP Basic: Access denied.\n"]])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def username_and_password
|
24
|
+
decode_credentials(request).split(/:/, 2)
|
25
|
+
end
|
26
|
+
|
27
|
+
def http_authentication
|
28
|
+
request.env['HTTP_AUTHORIZATION'] ||
|
29
|
+
request.env['X-HTTP_AUTHORIZATION'] ||
|
30
|
+
request.env['X_HTTP_AUTHORIZATION'] ||
|
31
|
+
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
|
32
|
+
end
|
33
|
+
alias :http_authentication? :http_authentication
|
34
|
+
|
35
|
+
def decode_credentials(request)
|
36
|
+
ActiveSupport::Base64.decode64(http_authentication.split(' ', 2).last || '')
|
37
|
+
end
|
38
|
+
|
39
|
+
def custom_headers
|
40
|
+
{
|
41
|
+
"Content-Type" => "text/plain",
|
42
|
+
"WWW-Authenticate" => %(Basic realm="#{Devise.http_authentication_realm.gsub(/"/, "")}")
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Warden::Strategies.add(:http_authenticatable, Devise::Strategies::HttpAuthenticatable)
|