2010-03-10 13:59:57 +00:00
|
|
|
require 'active_support/core_ext/numeric/time'
|
|
|
|
|
2009-10-18 15:30:32 +00:00
|
|
|
module Devise
|
2009-12-01 18:35:46 +00:00
|
|
|
autoload :FailureApp, 'devise/failure_app'
|
2009-12-13 00:52:48 +00:00
|
|
|
autoload :Schema, 'devise/schema'
|
|
|
|
autoload :TestHelpers, 'devise/test_helpers'
|
|
|
|
|
|
|
|
module Controllers
|
|
|
|
autoload :Helpers, 'devise/controllers/helpers'
|
2010-01-13 17:12:13 +00:00
|
|
|
autoload :InternalHelpers, 'devise/controllers/internal_helpers'
|
2010-02-17 11:25:20 +00:00
|
|
|
autoload :ScopedViews, 'devise/controllers/scoped_views'
|
2009-12-13 00:52:48 +00:00
|
|
|
autoload :UrlHelpers, 'devise/controllers/url_helpers'
|
|
|
|
end
|
|
|
|
|
|
|
|
module Encryptors
|
2010-01-08 22:19:57 +00:00
|
|
|
autoload :Base, 'devise/encryptors/base'
|
|
|
|
autoload :Bcrypt, 'devise/encryptors/bcrypt'
|
2009-12-13 00:52:48 +00:00
|
|
|
autoload :AuthlogicSha512, 'devise/encryptors/authlogic_sha512'
|
2010-02-12 15:00:58 +00:00
|
|
|
autoload :ClearanceSha1, 'devise/encryptors/clearance_sha1'
|
2009-12-13 00:52:48 +00:00
|
|
|
autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
|
|
|
|
autoload :Sha512, 'devise/encryptors/sha512'
|
|
|
|
autoload :Sha1, 'devise/encryptors/sha1'
|
|
|
|
end
|
|
|
|
|
2010-03-31 19:43:19 +00:00
|
|
|
module Strategies
|
|
|
|
autoload :Base, 'devise/strategies/base'
|
|
|
|
autoload :Authenticatable, 'devise/strategies/authenticatable'
|
|
|
|
end
|
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# Constants which holds devise configuration for extensions. Those should
|
|
|
|
# not be modified by the "end user".
|
2010-04-03 11:11:45 +00:00
|
|
|
ALL = []
|
|
|
|
CONTROLLERS = ActiveSupport::OrderedHash.new
|
|
|
|
ROUTES = ActiveSupport::OrderedHash.new
|
|
|
|
STRATEGIES = ActiveSupport::OrderedHash.new
|
2010-01-24 02:38:52 +00:00
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# True values used to check params
|
2009-12-08 20:29:00 +00:00
|
|
|
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
2009-10-18 15:30:32 +00:00
|
|
|
|
2009-11-10 20:55:13 +00:00
|
|
|
# Declare encryptors length which are used in migrations.
|
|
|
|
ENCRYPTORS_LENGTH = {
|
|
|
|
:sha1 => 40,
|
|
|
|
:sha512 => 128,
|
|
|
|
:clearance_sha1 => 40,
|
|
|
|
:restful_authentication_sha1 => 40,
|
2010-01-08 22:19:57 +00:00
|
|
|
:authlogic_sha512 => 128,
|
|
|
|
:bcrypt => 60
|
2009-11-10 20:55:13 +00:00
|
|
|
}
|
|
|
|
|
2009-11-18 11:41:42 +00:00
|
|
|
# Used to encrypt password. Please generate one with rake secret.
|
2009-11-10 00:00:44 +00:00
|
|
|
mattr_accessor :pepper
|
|
|
|
@@pepper = nil
|
2009-11-13 22:54:21 +00:00
|
|
|
|
2009-11-10 00:00:44 +00:00
|
|
|
# The number of times to encrypt password.
|
|
|
|
mattr_accessor :stretches
|
|
|
|
@@stretches = 10
|
2009-11-13 22:54:21 +00:00
|
|
|
|
2009-11-15 05:31:13 +00:00
|
|
|
# Keys used when authenticating an user.
|
|
|
|
mattr_accessor :authentication_keys
|
|
|
|
@@authentication_keys = [ :email ]
|
2010-03-29 18:52:34 +00:00
|
|
|
|
|
|
|
# If http authentication is enabled by default.
|
|
|
|
mattr_accessor :http_authenticatable
|
|
|
|
@@http_authenticatable = true
|
|
|
|
|
2010-04-01 17:09:33 +00:00
|
|
|
# If params authenticatable is enabled by default.
|
|
|
|
mattr_accessor :params_authenticatable
|
|
|
|
@@params_authenticatable = true
|
|
|
|
|
2010-03-29 18:52:34 +00:00
|
|
|
# The realm used in Http Basic Authentication.
|
|
|
|
mattr_accessor :http_authentication_realm
|
|
|
|
@@http_authentication_realm = "Application"
|
|
|
|
|
2010-03-26 20:52:12 +00:00
|
|
|
# Email regex used to validate email formats. Adapted from authlogic.
|
2010-03-28 05:15:52 +00:00
|
|
|
mattr_accessor :email_regexp
|
|
|
|
@@email_regexp = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
|
2010-03-29 18:52:34 +00:00
|
|
|
|
|
|
|
# Range validation for password length
|
|
|
|
mattr_accessor :password_length
|
|
|
|
@@password_length = 6..20
|
|
|
|
|
2009-11-10 00:00:44 +00:00
|
|
|
# Time interval where the remember me token is valid.
|
|
|
|
mattr_accessor :remember_for
|
|
|
|
@@remember_for = 2.weeks
|
2009-11-13 22:54:21 +00:00
|
|
|
|
2009-11-10 00:00:44 +00:00
|
|
|
# Time interval you can access your account before confirming your account.
|
|
|
|
mattr_accessor :confirm_within
|
|
|
|
@@confirm_within = 0.days
|
2009-11-04 00:34:37 +00:00
|
|
|
|
2009-11-23 00:19:29 +00:00
|
|
|
# Time interval to timeout the user session without activity.
|
2009-11-25 02:11:49 +00:00
|
|
|
mattr_accessor :timeout_in
|
|
|
|
@@timeout_in = 30.minutes
|
2009-11-23 00:19:29 +00:00
|
|
|
|
2009-11-10 20:55:13 +00:00
|
|
|
# Used to define the password encryption algorithm.
|
2009-11-23 00:32:54 +00:00
|
|
|
mattr_accessor :encryptor
|
|
|
|
@@encryptor = :sha1
|
2009-11-10 20:55:13 +00:00
|
|
|
|
2009-11-10 00:00:44 +00:00
|
|
|
# Store scopes mappings.
|
2009-11-06 16:27:27 +00:00
|
|
|
mattr_accessor :mappings
|
2010-01-16 13:32:52 +00:00
|
|
|
@@mappings = ActiveSupport::OrderedHash.new
|
2009-11-06 16:27:27 +00:00
|
|
|
|
2009-11-18 11:41:42 +00:00
|
|
|
# Tells if devise should apply the schema in ORMs where devise declaration
|
2010-03-26 19:25:12 +00:00
|
|
|
# and schema belongs to the same class (as Datamapper and Mongoid).
|
2009-11-18 11:41:42 +00:00
|
|
|
mattr_accessor :apply_schema
|
|
|
|
@@apply_schema = true
|
|
|
|
|
2009-11-21 22:07:37 +00:00
|
|
|
# Scoped views. Since it relies on fallbacks to render default views, it's
|
|
|
|
# turned off by default.
|
|
|
|
mattr_accessor :scoped_views
|
|
|
|
@@scoped_views = false
|
|
|
|
|
2010-03-31 09:54:11 +00:00
|
|
|
# Defines which strategy can be used to lock an account.
|
|
|
|
# Values: :failed_attempts, :none
|
|
|
|
mattr_accessor :lock_strategy
|
|
|
|
@@lock_strategy = :failed_attempts
|
2009-12-30 17:19:33 +00:00
|
|
|
|
|
|
|
# Defines which strategy can be used to unlock an account.
|
|
|
|
# Values: :email, :time, :both
|
|
|
|
mattr_accessor :unlock_strategy
|
|
|
|
@@unlock_strategy = :both
|
|
|
|
|
2010-03-31 09:54:11 +00:00
|
|
|
# Number of authentication tries before locking an account
|
|
|
|
mattr_accessor :maximum_attempts
|
|
|
|
@@maximum_attempts = 20
|
|
|
|
|
2009-12-30 17:19:33 +00:00
|
|
|
# Time interval to unlock the account if :time is defined as unlock_strategy.
|
|
|
|
mattr_accessor :unlock_in
|
|
|
|
@@unlock_in = 1.hour
|
|
|
|
|
2010-01-06 13:31:00 +00:00
|
|
|
# Tell when to use the default scope, if one cannot be found from routes.
|
|
|
|
mattr_accessor :use_default_scope
|
2010-02-08 16:33:22 +00:00
|
|
|
@@use_default_scope = false
|
2010-01-06 13:31:00 +00:00
|
|
|
|
|
|
|
# The default scope which is used by warden.
|
2010-01-05 12:44:13 +00:00
|
|
|
mattr_accessor :default_scope
|
|
|
|
@@default_scope = nil
|
|
|
|
|
2010-01-06 13:31:00 +00:00
|
|
|
# Address which sends Devise e-mails.
|
2010-01-05 15:01:16 +00:00
|
|
|
mattr_accessor :mailer_sender
|
2010-02-08 16:33:22 +00:00
|
|
|
@@mailer_sender = nil
|
2010-01-05 15:01:16 +00:00
|
|
|
|
2010-01-24 02:38:52 +00:00
|
|
|
# Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
|
2010-02-02 12:21:00 +00:00
|
|
|
mattr_accessor :token_authentication_key
|
|
|
|
@@token_authentication_key = :auth_token
|
2010-01-24 02:38:52 +00:00
|
|
|
|
2010-03-28 12:51:03 +00:00
|
|
|
# Private methods to interface with Warden.
|
2010-03-31 09:54:11 +00:00
|
|
|
mattr_accessor :warden_config
|
2010-03-28 12:51:03 +00:00
|
|
|
@@warden_config = nil
|
|
|
|
@@warden_config_block = nil
|
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# Default way to setup Devise. Run rails generate devise_install to create
|
|
|
|
# a fresh initializer with all configuration values.
|
|
|
|
def self.setup
|
|
|
|
yield self
|
|
|
|
end
|
2009-11-03 11:35:11 +00:00
|
|
|
|
2010-03-28 12:51:03 +00:00
|
|
|
# Register a model in Devise. You can call this manually if you don't want
|
|
|
|
# to use devise routes. Check devise_for in routes to know which options
|
|
|
|
# are available.
|
|
|
|
def self.register(resource, options)
|
|
|
|
mapping = Devise::Mapping.new(resource, options)
|
|
|
|
self.mappings[mapping.name] = mapping
|
|
|
|
self.default_scope ||= mapping.name
|
|
|
|
|
|
|
|
warden_config.default_scope ||= mapping.name
|
|
|
|
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
|
|
|
|
mapping
|
|
|
|
end
|
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# Make Devise aware of an 3rd party Devise-module. For convenience.
|
|
|
|
#
|
|
|
|
# == Options:
|
|
|
|
#
|
2010-03-28 12:51:03 +00:00
|
|
|
# +model+ - String representing the load path to a custom *model* for this module (to autoload.)
|
|
|
|
# +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
|
|
|
|
# +route+ - Symbol representing the named *route* helper for this module.
|
|
|
|
# +flash+ - Symbol representing the *flash messages* used by this helper.
|
|
|
|
# +strategy+ - Symbol representing if this module got a custom *strategy*.
|
|
|
|
#
|
|
|
|
# All values, except :model, accept also a boolean and will have the same name as the given module
|
|
|
|
# name.
|
2010-03-03 10:57:23 +00:00
|
|
|
#
|
|
|
|
# == Examples:
|
|
|
|
#
|
|
|
|
# Devise.add_module(:party_module)
|
|
|
|
# Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
|
2010-03-28 12:51:03 +00:00
|
|
|
# Devise.add_module(:party_module, :model => 'party_module/model')
|
2010-03-03 10:57:23 +00:00
|
|
|
#
|
|
|
|
def self.add_module(module_name, options = {})
|
|
|
|
ALL << module_name
|
2010-04-03 11:11:45 +00:00
|
|
|
options.assert_valid_keys(:strategy, :model, :controller, :route)
|
2010-03-28 12:51:03 +00:00
|
|
|
|
|
|
|
config = {
|
|
|
|
:strategy => STRATEGIES,
|
|
|
|
:route => ROUTES,
|
|
|
|
:controller => CONTROLLERS
|
|
|
|
}
|
2010-03-03 10:57:23 +00:00
|
|
|
|
2010-03-28 12:51:03 +00:00
|
|
|
config.each do |key, value|
|
2010-03-03 10:57:23 +00:00
|
|
|
next unless options[key]
|
|
|
|
name = (options[key] == true ? module_name : options[key])
|
2010-02-19 09:13:53 +00:00
|
|
|
|
2010-03-28 12:51:03 +00:00
|
|
|
if value.is_a?(Hash)
|
|
|
|
value[module_name] = name
|
|
|
|
else
|
|
|
|
value << name unless value.include?(name)
|
|
|
|
end
|
2010-02-19 08:26:17 +00:00
|
|
|
end
|
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
if options[:model]
|
|
|
|
model_path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
|
|
|
|
Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, model_path)
|
2009-11-03 11:35:11 +00:00
|
|
|
end
|
|
|
|
|
2010-03-28 12:51:03 +00:00
|
|
|
Devise::Mapping.add_module module_name
|
2010-03-03 10:57:23 +00:00
|
|
|
end
|
2009-11-03 11:35:11 +00:00
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# Sets warden configuration using a block that will be invoked on warden
|
|
|
|
# initialization.
|
|
|
|
#
|
|
|
|
# Devise.initialize do |config|
|
|
|
|
# config.confirm_within = 2.days
|
|
|
|
#
|
|
|
|
# config.warden do |manager|
|
|
|
|
# # Configure warden to use other strategies, like oauth.
|
|
|
|
# manager.oauth(:twitter)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def self.warden(&block)
|
2010-03-28 12:51:03 +00:00
|
|
|
@@warden_config_block = block
|
2010-03-03 10:57:23 +00:00
|
|
|
end
|
2009-11-13 22:54:21 +00:00
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# A method used internally to setup warden manager from the Rails initialize
|
|
|
|
# block.
|
2010-03-31 20:04:48 +00:00
|
|
|
def self.configure_warden! #:nodoc:
|
|
|
|
@@warden_config_block.try :call, Devise.warden_config
|
2009-11-03 11:35:11 +00:00
|
|
|
end
|
2009-10-21 02:12:21 +00:00
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
# Generate a friendly string randomically to be used as token.
|
|
|
|
def self.friendly_token
|
|
|
|
ActiveSupport::SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
|
|
|
|
end
|
2009-11-15 02:13:43 +00:00
|
|
|
end
|
|
|
|
|
2010-03-03 10:57:23 +00:00
|
|
|
require 'warden'
|
2010-01-21 08:15:07 +00:00
|
|
|
require 'devise/mapping'
|
2010-03-03 10:57:23 +00:00
|
|
|
require 'devise/models'
|
|
|
|
require 'devise/modules'
|
2010-01-24 00:26:06 +00:00
|
|
|
require 'devise/rails'
|