namxam-devise 1.1.0.win
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +455 -0
- data/Gemfile +23 -0
- data/Gemfile.lock +118 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +311 -0
- data/Rakefile +55 -0
- data/TODO +3 -0
- data/app/controllers/devise/confirmations_controller.rb +33 -0
- data/app/controllers/devise/passwords_controller.rb +41 -0
- data/app/controllers/devise/registrations_controller.rb +57 -0
- data/app/controllers/devise/sessions_controller.rb +23 -0
- data/app/controllers/devise/unlocks_controller.rb +34 -0
- data/app/helpers/devise_helper.rb +17 -0
- data/app/mailers/devise/mailer.rb +71 -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 +19 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +39 -0
- data/lib/devise.rb +290 -0
- data/lib/devise/controllers/helpers.rb +231 -0
- data/lib/devise/controllers/internal_helpers.rb +98 -0
- data/lib/devise/controllers/scoped_views.rb +35 -0
- data/lib/devise/controllers/url_helpers.rb +41 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/bcrypt.rb +19 -0
- data/lib/devise/encryptors/clearance_sha1.rb +17 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +25 -0
- data/lib/devise/encryptors/sha512.rb +25 -0
- data/lib/devise/failure_app.rb +107 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +11 -0
- data/lib/devise/hooks/rememberable.rb +35 -0
- data/lib/devise/hooks/timeoutable.rb +22 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mapping.rb +103 -0
- data/lib/devise/models.rb +80 -0
- data/lib/devise/models/authenticatable.rb +126 -0
- data/lib/devise/models/confirmable.rb +164 -0
- data/lib/devise/models/database_authenticatable.rb +110 -0
- data/lib/devise/models/lockable.rb +165 -0
- data/lib/devise/models/recoverable.rb +81 -0
- data/lib/devise/models/registerable.rb +8 -0
- data/lib/devise/models/rememberable.rb +104 -0
- data/lib/devise/models/timeoutable.rb +26 -0
- data/lib/devise/models/token_authenticatable.rb +60 -0
- data/lib/devise/models/trackable.rb +30 -0
- data/lib/devise/models/validatable.rb +53 -0
- data/lib/devise/modules.rb +23 -0
- data/lib/devise/orm/active_record.rb +36 -0
- data/lib/devise/orm/mongoid.rb +29 -0
- data/lib/devise/path_checker.rb +18 -0
- data/lib/devise/rails.rb +69 -0
- data/lib/devise/rails/routes.rb +248 -0
- data/lib/devise/rails/warden_compat.rb +39 -0
- data/lib/devise/schema.rb +97 -0
- data/lib/devise/strategies/authenticatable.rb +111 -0
- data/lib/devise/strategies/base.rb +33 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +43 -0
- data/lib/devise/strategies/token_authenticatable.rb +49 -0
- data/lib/devise/test_helpers.rb +90 -0
- data/lib/devise/version.rb +3 -0
- data/lib/generators/active_record/devise_generator.rb +28 -0
- data/lib/generators/active_record/templates/migration.rb +29 -0
- data/lib/generators/devise/devise_generator.rb +17 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +23 -0
- data/lib/generators/devise/templates/README +25 -0
- data/lib/generators/devise/templates/devise.rb +139 -0
- data/lib/generators/devise/views_generator.rb +63 -0
- data/lib/generators/devise_install_generator.rb +4 -0
- data/lib/generators/devise_views_generator.rb +4 -0
- data/lib/generators/mongoid/devise_generator.rb +17 -0
- data/test/controllers/helpers_test.rb +213 -0
- data/test/controllers/internal_helpers_test.rb +51 -0
- data/test/controllers/url_helpers_test.rb +58 -0
- data/test/devise_test.rb +65 -0
- data/test/encryptors_test.rb +30 -0
- data/test/failure_app_test.rb +123 -0
- data/test/integration/authenticatable_test.rb +344 -0
- data/test/integration/confirmable_test.rb +104 -0
- data/test/integration/database_authenticatable_test.rb +38 -0
- data/test/integration/http_authenticatable_test.rb +49 -0
- data/test/integration/lockable_test.rb +109 -0
- data/test/integration/recoverable_test.rb +141 -0
- data/test/integration/registerable_test.rb +153 -0
- data/test/integration/rememberable_test.rb +91 -0
- data/test/integration/timeoutable_test.rb +80 -0
- data/test/integration/token_authenticatable_test.rb +88 -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 +85 -0
- data/test/models/confirmable_test.rb +221 -0
- data/test/models/database_authenticatable_test.rb +148 -0
- data/test/models/lockable_test.rb +188 -0
- data/test/models/recoverable_test.rb +138 -0
- data/test/models/rememberable_test.rb +176 -0
- data/test/models/timeoutable_test.rb +28 -0
- data/test/models/token_authenticatable_test.rb +37 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +99 -0
- data/test/models_test.rb +77 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +10 -0
- data/test/rails_app/app/active_record/admin.rb +3 -0
- data/test/rails_app/app/active_record/shim.rb +2 -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 +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +7 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
- data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/users_controller.rb +18 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mongoid/admin.rb +6 -0
- data/test/rails_app/app/mongoid/shim.rb +16 -0
- data/test/rails_app/app/mongoid/user.rb +10 -0
- data/test/rails_app/config/application.rb +35 -0
- data/test/rails_app/config/boot.rb +13 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +33 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +136 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/secret_token.rb +2 -0
- data/test/rails_app/config/routes.rb +47 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
- data/test/rails_app/db/schema.rb +86 -0
- data/test/routes_test.rb +146 -0
- data/test/support/assertions.rb +24 -0
- data/test/support/helpers.rb +54 -0
- data/test/support/integration.rb +88 -0
- data/test/support/test_silencer.rb +5 -0
- data/test/support/webrat/integrations/rails.rb +32 -0
- data/test/test_helper.rb +21 -0
- data/test/test_helpers_test.rb +72 -0
- metadata +230 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Controllers
|
|
3
|
+
# Those helpers are used only inside Devise controllers and should not be
|
|
4
|
+
# included in ApplicationController since they all depend on the url being
|
|
5
|
+
# accessed.
|
|
6
|
+
module InternalHelpers #:nodoc:
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
include Devise::Controllers::ScopedViews
|
|
9
|
+
|
|
10
|
+
included do
|
|
11
|
+
helper DeviseHelper
|
|
12
|
+
|
|
13
|
+
helpers = %w(resource scope_name resource_name
|
|
14
|
+
resource_class devise_mapping devise_controller?)
|
|
15
|
+
hide_action *helpers
|
|
16
|
+
helper_method *helpers
|
|
17
|
+
|
|
18
|
+
prepend_before_filter :is_devise_resource?
|
|
19
|
+
skip_before_filter *Devise.mappings.keys.map { |m| :"authenticate_#{m}!" }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Gets the actual resource stored in the instance variable
|
|
23
|
+
def resource
|
|
24
|
+
instance_variable_get(:"@#{resource_name}")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Proxy to devise map name
|
|
28
|
+
def resource_name
|
|
29
|
+
devise_mapping.name
|
|
30
|
+
end
|
|
31
|
+
alias :scope_name :resource_name
|
|
32
|
+
|
|
33
|
+
# Proxy to devise map class
|
|
34
|
+
def resource_class
|
|
35
|
+
devise_mapping.to
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Attempt to find the mapped route for devise based on request path
|
|
39
|
+
def devise_mapping
|
|
40
|
+
@devise_mapping ||= request.env["devise.mapping"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Overwrites devise_controller? to return true
|
|
44
|
+
def devise_controller?
|
|
45
|
+
true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
protected
|
|
49
|
+
|
|
50
|
+
# Checks whether it's a devise mapped resource or not.
|
|
51
|
+
def is_devise_resource? #:nodoc:
|
|
52
|
+
raise ActionController::UnknownAction unless devise_mapping
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Sets the resource creating an instance variable
|
|
56
|
+
def resource=(new_resource)
|
|
57
|
+
instance_variable_set(:"@#{resource_name}", new_resource)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Build a devise resource.
|
|
61
|
+
def build_resource(hash=nil)
|
|
62
|
+
hash ||= params[resource_name] || {}
|
|
63
|
+
self.resource = resource_class.new(hash)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Helper for use in before_filters where no authentication is required.
|
|
67
|
+
#
|
|
68
|
+
# Example:
|
|
69
|
+
# before_filter :require_no_authentication, :only => :new
|
|
70
|
+
def require_no_authentication
|
|
71
|
+
redirect_to after_sign_in_path_for(resource_name) if warden.authenticated?(resource_name)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Sets the flash message with :key, using I18n. By default you are able
|
|
75
|
+
# to setup your messages using specific resource scope, and if no one is
|
|
76
|
+
# found we look to default scope.
|
|
77
|
+
# Example (i18n locale file):
|
|
78
|
+
#
|
|
79
|
+
# en:
|
|
80
|
+
# devise:
|
|
81
|
+
# passwords:
|
|
82
|
+
# #default_scope_messages - only if resource_scope is not found
|
|
83
|
+
# user:
|
|
84
|
+
# #resource_scope_messages
|
|
85
|
+
#
|
|
86
|
+
# Please refer to README or en.yml locale file to check what messages are
|
|
87
|
+
# available.
|
|
88
|
+
def set_flash_message(key, kind)
|
|
89
|
+
flash[key] = I18n.t(:"#{resource_name}.#{kind}", :resource_name => resource_name,
|
|
90
|
+
:scope => [:devise, controller_name.to_sym], :default => kind)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def clean_up_passwords(object)
|
|
94
|
+
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Controllers
|
|
3
|
+
module ScopedViews
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
module ClassMethods
|
|
7
|
+
def scoped_views?
|
|
8
|
+
defined?(@scoped_views) ? @scoped_views : Devise.scoped_views
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def scoped_views=(value)
|
|
12
|
+
@scoped_views = value
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
protected
|
|
17
|
+
|
|
18
|
+
# Render a view for the specified scope. Turned off by default.
|
|
19
|
+
# Accepts just :controller as option.
|
|
20
|
+
def render_with_scope(action, options={})
|
|
21
|
+
controller_name = options.delete(:controller) || self.controller_name
|
|
22
|
+
|
|
23
|
+
if self.class.scoped_views?
|
|
24
|
+
begin
|
|
25
|
+
render :template => "#{devise_mapping.plural}/#{controller_name}/#{action}"
|
|
26
|
+
rescue ActionView::MissingTemplate
|
|
27
|
+
render :template => "#{controller_path}/#{action}"
|
|
28
|
+
end
|
|
29
|
+
else
|
|
30
|
+
render :template => "#{controller_path}/#{action}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Controllers
|
|
3
|
+
# Create url helpers to be used with resource/scope configuration. Acts as
|
|
4
|
+
# proxies to the generated routes created by devise.
|
|
5
|
+
# Resource param can be a string or symbol, a class, or an instance object.
|
|
6
|
+
# Example using a :user resource:
|
|
7
|
+
#
|
|
8
|
+
# new_session_path(:user) => new_user_session_path
|
|
9
|
+
# session_path(:user) => user_session_path
|
|
10
|
+
# destroy_session_path(:user) => destroy_user_session_path
|
|
11
|
+
#
|
|
12
|
+
# new_password_path(:user) => new_user_password_path
|
|
13
|
+
# password_path(:user) => user_password_path
|
|
14
|
+
# edit_password_path(:user) => edit_user_password_path
|
|
15
|
+
#
|
|
16
|
+
# new_confirmation_path(:user) => new_user_confirmation_path
|
|
17
|
+
# confirmation_path(:user) => user_confirmation_path
|
|
18
|
+
#
|
|
19
|
+
# Those helpers are added to your ApplicationController.
|
|
20
|
+
module UrlHelpers
|
|
21
|
+
|
|
22
|
+
Devise::ROUTES.values.uniq.each do |module_name|
|
|
23
|
+
[:path, :url].each do |path_or_url|
|
|
24
|
+
actions = [ nil, :new_ ]
|
|
25
|
+
actions << :edit_ if [:password, :registration].include?(module_name)
|
|
26
|
+
actions << :destroy_ if [:session].include?(module_name)
|
|
27
|
+
|
|
28
|
+
actions.each do |action|
|
|
29
|
+
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
|
30
|
+
def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args)
|
|
31
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
32
|
+
send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
|
|
33
|
+
end
|
|
34
|
+
URL_HELPERS
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require "digest/sha2"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = AuthlogicSha512
|
|
6
|
+
# Simulates Authlogic's default encryption mechanism.
|
|
7
|
+
# Warning: it uses Devise's stretches configuration to port Authlogic's one. Should be set to 20 in the initializer to silumate
|
|
8
|
+
# the default behavior.
|
|
9
|
+
class AuthlogicSha512 < Base
|
|
10
|
+
# Gererates a default password digest based on salt, pepper and the
|
|
11
|
+
# incoming password.
|
|
12
|
+
def self.digest(password, stretches, salt, pepper)
|
|
13
|
+
digest = [password, salt].flatten.join('')
|
|
14
|
+
stretches.times { digest = Digest::SHA512.hexdigest(digest) }
|
|
15
|
+
digest
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
# Implements a way of adding different encryptions.
|
|
3
|
+
# The class should implement a self.digest method that taks the following params:
|
|
4
|
+
# - password
|
|
5
|
+
# - stretches: the number of times the encryption will be applied
|
|
6
|
+
# - salt: the password salt as defined by devise
|
|
7
|
+
# - pepper: Devise config option
|
|
8
|
+
#
|
|
9
|
+
module Encryptors
|
|
10
|
+
class Base
|
|
11
|
+
def self.digest
|
|
12
|
+
raise NotImplemented
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.salt(stretches)
|
|
16
|
+
Devise.friendly_token
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require "bcrypt"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = BCrypt
|
|
6
|
+
# Uses the BCrypt hash algorithm to encrypt passwords.
|
|
7
|
+
class Bcrypt < Base
|
|
8
|
+
# Gererates a default password digest based on stretches, salt, pepper and the
|
|
9
|
+
# incoming password. We don't strech it ourselves since BCrypt does so internally.
|
|
10
|
+
def self.digest(password, stretches, salt, pepper)
|
|
11
|
+
::BCrypt::Engine.hash_secret([password, pepper].join, salt, stretches)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.salt(stretches)
|
|
15
|
+
::BCrypt::Engine.generate_salt(stretches)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "digest/sha1"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = ClearanceSha1
|
|
6
|
+
# Simulates Clearance's default encryption mechanism.
|
|
7
|
+
# Warning: it uses Devise's pepper to port the concept of REST_AUTH_SITE_KEY
|
|
8
|
+
# Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES
|
|
9
|
+
class ClearanceSha1 < Base
|
|
10
|
+
# Gererates a default password digest based on salt, pepper and the
|
|
11
|
+
# incoming password.
|
|
12
|
+
def self.digest(password, stretches, salt, pepper)
|
|
13
|
+
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "digest/sha1"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = RestfulAuthenticationSha1
|
|
6
|
+
# Simulates Restful Authentication's default encryption mechanism.
|
|
7
|
+
# Warning: it uses Devise's pepper to port the concept of REST_AUTH_SITE_KEY
|
|
8
|
+
# Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES. Should be set to 10 in
|
|
9
|
+
# the initializer to silumate the default behavior.
|
|
10
|
+
class RestfulAuthenticationSha1 < Base
|
|
11
|
+
|
|
12
|
+
# Gererates a default password digest based on salt, pepper and the
|
|
13
|
+
# incoming password.
|
|
14
|
+
def self.digest(password, stretches, salt, pepper)
|
|
15
|
+
digest = pepper
|
|
16
|
+
stretches.times { digest = Digest::SHA1.hexdigest([digest, salt, password, pepper].flatten.join('--')) }
|
|
17
|
+
digest
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "digest/sha1"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = Sha1
|
|
6
|
+
# Uses the Sha1 hash algorithm to encrypt passwords.
|
|
7
|
+
class Sha1 < Base
|
|
8
|
+
# Gererates a default password digest based on stretches, salt, pepper and the
|
|
9
|
+
# incoming password.
|
|
10
|
+
def self.digest(password, stretches, salt, pepper)
|
|
11
|
+
digest = pepper
|
|
12
|
+
stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
|
|
13
|
+
digest
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
# Generate a SHA1 digest joining args. Generated token is something like
|
|
19
|
+
# --arg1--arg2--arg3--argN--
|
|
20
|
+
def self.secure_digest(*tokens)
|
|
21
|
+
::Digest::SHA1.hexdigest('--' << tokens.flatten.join('--') << '--')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "digest/sha2"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Encryptors
|
|
5
|
+
# = Sha512
|
|
6
|
+
# Uses the Sha512 hash algorithm to encrypt passwords.
|
|
7
|
+
class Sha512 < Base
|
|
8
|
+
# Gererates a default password digest based on salt, pepper and the
|
|
9
|
+
# incoming password.
|
|
10
|
+
def self.digest(password, stretches, salt, pepper)
|
|
11
|
+
digest = pepper
|
|
12
|
+
stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
|
|
13
|
+
digest
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
# Generate a Sha512 digest joining args. Generated token is something like
|
|
19
|
+
# --arg1--arg2--arg3--argN--
|
|
20
|
+
def self.secure_digest(*tokens)
|
|
21
|
+
::Digest::SHA512.hexdigest('--' << tokens.flatten.join('--') << '--')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require "action_controller/metal"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
# Failure application that will be called every time :warden is thrown from
|
|
5
|
+
# any strategy or hook. Responsible for redirect the user to the sign in
|
|
6
|
+
# page based on current scope and mapping. If no scope is given, redirect
|
|
7
|
+
# to the default_url.
|
|
8
|
+
class FailureApp < ActionController::Metal
|
|
9
|
+
include ActionController::RackDelegation
|
|
10
|
+
include ActionController::UrlFor
|
|
11
|
+
include ActionController::Redirecting
|
|
12
|
+
include Rails.application.routes.url_helpers
|
|
13
|
+
|
|
14
|
+
delegate :flash, :to => :request
|
|
15
|
+
|
|
16
|
+
def self.call(env)
|
|
17
|
+
action(:respond).call(env)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.default_url_options(*args)
|
|
21
|
+
ApplicationController.default_url_options(*args)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def respond
|
|
25
|
+
if http_auth?
|
|
26
|
+
http_auth
|
|
27
|
+
elsif warden_options[:recall]
|
|
28
|
+
recall
|
|
29
|
+
else
|
|
30
|
+
redirect
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def http_auth
|
|
35
|
+
self.status = 401
|
|
36
|
+
self.headers["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect})
|
|
37
|
+
self.content_type = request.format.to_s
|
|
38
|
+
self.response_body = http_auth_body
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def recall
|
|
42
|
+
env["PATH_INFO"] = attempted_path
|
|
43
|
+
flash.now[:alert] = i18n_message(:invalid)
|
|
44
|
+
self.response = recall_controller.action(warden_options[:recall]).call(env)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def redirect
|
|
48
|
+
store_location!
|
|
49
|
+
flash[:alert] = i18n_message unless flash[:notice]
|
|
50
|
+
redirect_to redirect_url
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
protected
|
|
54
|
+
|
|
55
|
+
def i18n_message(default = nil)
|
|
56
|
+
message = warden.message || warden_options[:message] || default || :unauthenticated
|
|
57
|
+
|
|
58
|
+
if message.is_a?(Symbol)
|
|
59
|
+
I18n.t(:"#{scope}.#{message}", :resource_name => scope,
|
|
60
|
+
:scope => "devise.failure", :default => [message, message.to_s])
|
|
61
|
+
else
|
|
62
|
+
message.to_s
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def redirect_url
|
|
67
|
+
send(:"new_#{scope}_session_path")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def http_auth?
|
|
71
|
+
!Devise.navigational_formats.include?(request.format.to_sym) || (request.xhr? && Devise.http_authenticatable_on_xhr)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def http_auth_body
|
|
75
|
+
method = :"to_#{request.format.to_sym}"
|
|
76
|
+
{}.respond_to?(method) ? { :error => i18n_message }.send(method) : i18n_message
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def recall_controller
|
|
80
|
+
"#{params[:controller].camelize}Controller".constantize
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def warden
|
|
84
|
+
env['warden']
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def warden_options
|
|
88
|
+
env['warden.options']
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def scope
|
|
92
|
+
@scope ||= warden_options[:scope]
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def attempted_path
|
|
96
|
+
warden_options[:attempted_path]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Stores requested uri to redirect the user after signing in. We cannot use
|
|
100
|
+
# scoped session provided by warden here, since the user is not authenticated
|
|
101
|
+
# yet, but we still need to store the uri based on scope, so different scopes
|
|
102
|
+
# would never use the same uri to redirect.
|
|
103
|
+
def store_location!
|
|
104
|
+
session[:"#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Deny user access whenever his account is not active yet. All strategies that inherits from
|
|
2
|
+
# Devise::Strategies::Authenticatable and uses the validate already check if the user is active?
|
|
3
|
+
# before actively signing him in. However, we need this as hook to validate the user activity
|
|
4
|
+
# in each request and in case the user is using other strategies beside Devise ones.
|
|
5
|
+
Warden::Manager.after_set_user do |record, warden, options|
|
|
6
|
+
if record && record.respond_to?(:active?) && !record.active?
|
|
7
|
+
scope = options[:scope]
|
|
8
|
+
warden.logout(scope)
|
|
9
|
+
throw :warden, :scope => scope, :message => record.inactive_message
|
|
10
|
+
end
|
|
11
|
+
end
|