1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise.rb

496 lines
15 KiB
Ruby
Raw Normal View History

require 'rails'
require 'active_support/core_ext/numeric/time'
require 'active_support/dependencies'
2010-10-10 17:51:12 +02:00
require 'orm_adapter'
require 'set'
require 'securerandom'
module Devise
autoload :Delegator, 'devise/delegator'
autoload :FailureApp, 'devise/failure_app'
autoload :OmniAuth, 'devise/omniauth'
autoload :ParameterFilter, 'devise/parameter_filter'
autoload :BaseSanitizer, 'devise/parameter_sanitizer'
autoload :ParameterSanitizer, 'devise/parameter_sanitizer'
autoload :TestHelpers, 'devise/test_helpers'
autoload :TimeInflector, 'devise/time_inflector'
2013-08-05 18:56:07 +02:00
autoload :TokenGenerator, 'devise/token_generator'
2009-12-12 22:52:48 -02:00
module Controllers
autoload :Helpers, 'devise/controllers/helpers'
2011-02-24 21:55:41 +01:00
autoload :Rememberable, 'devise/controllers/rememberable'
autoload :ScopedViews, 'devise/controllers/scoped_views'
autoload :SignInOut, 'devise/controllers/sign_in_out'
autoload :StoreLocation, 'devise/controllers/store_location'
2009-12-12 22:52:48 -02:00
autoload :UrlHelpers, 'devise/controllers/url_helpers'
end
module Hooks
autoload :Proxy, 'devise/hooks/proxy'
end
module Mailers
autoload :Helpers, 'devise/mailers/helpers'
end
module Strategies
autoload :Base, 'devise/strategies/base'
autoload :Authenticatable, 'devise/strategies/authenticatable'
end
# Constants which holds devise configuration for extensions. Those should
2010-07-13 12:17:25 +02:00
# not be modified by the "end user" (this is why they are constants).
ALL = []
CONTROLLERS = ActiveSupport::OrderedHash.new
ROUTES = ActiveSupport::OrderedHash.new
STRATEGIES = ActiveSupport::OrderedHash.new
2010-07-13 10:09:55 +02:00
URL_HELPERS = ActiveSupport::OrderedHash.new
# Strategies that do not require user input.
NO_INPUT = []
# True values used to check params
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
2013-08-05 11:47:36 +02:00
# Secret key used by the key generator
mattr_accessor :secret_key
@@secret_key = nil
2013-09-02 19:23:15 -03:00
[ :allow_insecure_token_lookup,
:allow_insecure_sign_in_after_confirmation,
:token_authentication_key ].each do |method|
class_eval <<-RUBY
def self.#{method}
ActiveSupport::Deprecation.warn "Devise.#{method} is deprecated " \
"and has no effect"
end
def self.#{method}=(val)
ActiveSupport::Deprecation.warn "Devise.#{method}= is deprecated " \
"and has no effect"
end
RUBY
end
2013-08-08 22:33:58 +02:00
# Custom domain or key for cookies. Not set by default
mattr_accessor :rememberable_options
@@rememberable_options = {}
# The number of times to encrypt password.
mattr_accessor :stretches
@@stretches = 10
# The default key used when authenticating over http auth.
2013-04-13 22:07:54 -07:00
mattr_accessor :http_authentication_key
@@http_authentication_key = nil
# Keys used when authenticating a user.
2009-11-15 03:31:13 -02:00
mattr_accessor :authentication_keys
@@authentication_keys = [ :email ]
# Request keys used when authenticating a user.
2010-09-21 11:45:44 +02:00
mattr_accessor :request_keys
@@request_keys = []
# Keys that should be case-insensitive.
2010-11-18 21:24:42 +01:00
mattr_accessor :case_insensitive_keys
2012-05-06 13:13:53 +02:00
@@case_insensitive_keys = [ :email ]
# Keys that should have whitespace stripped.
mattr_accessor :strip_whitespace_keys
2012-05-06 13:13:53 +02:00
@@strip_whitespace_keys = []
2010-11-18 21:24:42 +01:00
# If http authentication is enabled by default.
mattr_accessor :http_authenticatable
@@http_authenticatable = false
# If http headers should be returned for ajax requests. True by default.
mattr_accessor :http_authenticatable_on_xhr
@@http_authenticatable_on_xhr = true
# If params authenticatable is enabled by default.
mattr_accessor :params_authenticatable
@@params_authenticatable = true
# The realm used in Http Basic Authentication.
mattr_accessor :http_authentication_realm
@@http_authentication_realm = "Application"
# Email regex used to validate email formats. It simply asserts that
# an one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
2010-03-28 07:15:52 +02:00
mattr_accessor :email_regexp
2012-11-19 13:58:19 +02:00
@@email_regexp = /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
# Range validation for password length
mattr_accessor :password_length
@@password_length = 6..128
# The time the user will be remembered without asking for credentials again.
mattr_accessor :remember_for
@@remember_for = 2.weeks
# If true, extends the user's remember period when remembered via cookie.
mattr_accessor :extend_remember_period
@@extend_remember_period = false
# If true, all the remember me tokens are going to be invalidated when the user signs out.
mattr_accessor :expire_all_remember_me_on_sign_out
@@expire_all_remember_me_on_sign_out = true
# Time interval you can access your account before confirming your account.
# nil - allows unconfirmed access for unlimited time
mattr_accessor :allow_unconfirmed_access_for
@@allow_unconfirmed_access_for = 0.days
2011-02-06 19:23:36 -02:00
# Time interval the confirmation token is valid. nil = unlimited
mattr_accessor :confirm_within
@@confirm_within = nil
# Defines which key will be used when confirming an account.
mattr_accessor :confirmation_keys
@@confirmation_keys = [ :email ]
# Defines if email should be reconfirmable.
# False by default for backwards compatibility.
mattr_accessor :reconfirmable
@@reconfirmable = false
# Time interval to timeout the user session without activity.
mattr_accessor :timeout_in
@@timeout_in = 30.minutes
# Authentication token expiration on timeout
mattr_accessor :expire_auth_token_on_timeout
@@expire_auth_token_on_timeout = false
# Used to encrypt password. Please generate one with rake secret.
mattr_accessor :pepper
@@pepper = nil
# 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 11:54:11 +02: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 15:19:33 -02:00
# Defines which key will be used when locking and unlocking an account
mattr_accessor :unlock_keys
@@unlock_keys = [ :email ]
2009-12-30 15:19:33 -02: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 11:54:11 +02:00
# Number of authentication tries before locking an account
mattr_accessor :maximum_attempts
@@maximum_attempts = 20
2009-12-30 15:19:33 -02:00
# Time interval to unlock the account if :time is defined as unlock_strategy.
mattr_accessor :unlock_in
@@unlock_in = 1.hour
# Defines which key will be used when recovering the password for an account
mattr_accessor :reset_password_keys
@@reset_password_keys = [ :email ]
2011-04-18 21:39:29 +08:00
# Time interval you can reset your password with a reset password key
mattr_accessor :reset_password_within
2012-05-06 13:13:53 +02:00
@@reset_password_within = 6.hours
# The default scope which is used by warden.
2010-01-05 13:44:13 +01:00
mattr_accessor :default_scope
@@default_scope = nil
# Address which sends Devise e-mails.
mattr_accessor :mailer_sender
@@mailer_sender = nil
# Skip session storage for the following strategies
mattr_accessor :skip_session_storage
@@skip_session_storage = []
# Which formats should be treated as navigational.
mattr_accessor :navigational_formats
2012-01-02 21:00:55 +01:00
@@navigational_formats = ["*/*", :html]
# When set to true, signing out a user signs out all other scopes.
2010-07-12 18:56:27 +02:00
mattr_accessor :sign_out_all_scopes
@@sign_out_all_scopes = true
2010-07-12 18:56:27 +02:00
2010-08-23 09:05:40 -03:00
# The default method used while signing out
mattr_accessor :sign_out_via
@@sign_out_via = :get
# The parent controller all Devise controllers inherits from.
2012-01-02 21:00:55 +01:00
# Defaults to ApplicationController. This should be set early
# in the initialization process and should be set to a string.
mattr_accessor :parent_controller
@@parent_controller = "ApplicationController"
# The parent mailer all Devise mailers inherit from.
# Defaults to ActionMailer::Base. This should be set early
# in the initialization process and should be set to a string.
mattr_accessor :parent_mailer
@@parent_mailer = "ActionMailer::Base"
# The router Devise should use to generate routes. Defaults
2014-01-09 10:00:27 -06:00
# to :main_app. Should be overridden by engines in order
# to provide custom routes.
mattr_accessor :router_name
@@router_name = nil
2014-01-09 10:00:27 -06:00
# Set the omniauth path prefix so it can be overridden when
# Devise is used in a mountable engine
mattr_accessor :omniauth_path_prefix
@@omniauth_path_prefix = nil
# Set if we should clean up the CSRF Token on authentication
mattr_accessor :clean_up_csrf_token_on_authentication
@@clean_up_csrf_token_on_authentication = true
2010-07-12 18:56:27 +02:00
# PRIVATE CONFIGURATION
2010-07-13 12:17:25 +02:00
# Store scopes mappings.
mattr_reader :mappings
@@mappings = ActiveSupport::OrderedHash.new
2010-10-14 20:04:02 +02:00
# Omniauth configurations.
mattr_reader :omniauth_configs
@@omniauth_configs = ActiveSupport::OrderedHash.new
# Define a set of modules that are called when a mapping is added.
mattr_reader :helpers
@@helpers = Set.new
@@helpers << Devise::Controllers::Helpers
# Private methods to interface with Warden.
2010-03-31 11:54:11 +02:00
mattr_accessor :warden_config
@@warden_config = nil
@@warden_config_block = nil
2011-06-22 13:01:49 -03:00
# When true, enter in paranoid mode to avoid user enumeration.
2011-05-20 19:41:40 -03:00
mattr_accessor :paranoid
@@paranoid = false
2013-12-02 10:02:17 +01:00
# When true, warn user if they just used next-to-last attempt of authentication
mattr_accessor :last_attempt_warning
@@last_attempt_warning = false
# Stores the token generator
mattr_accessor :token_generator
@@token_generator = nil
# 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
class Getter
def initialize name
@name = name
end
def get
ActiveSupport::Dependencies.constantize(@name)
end
end
2011-03-24 20:25:54 +01:00
def self.ref(arg)
if defined?(ActiveSupport::Dependencies::ClassCache)
ActiveSupport::Dependencies::reference(arg)
Getter.new(arg)
else
ActiveSupport::Dependencies.ref(arg)
end
2011-03-24 20:25:54 +01:00
end
def self.available_router_name
router_name || :main_app
end
2010-10-14 20:04:02 +02:00
def self.omniauth_providers
omniauth_configs.keys
end
# Get the mailer class from the mailer reference object.
def self.mailer
@@mailer_ref.get
end
# Set the mailer reference object to access the mailer.
def self.mailer=(class_name)
2011-03-24 20:25:54 +01:00
@@mailer_ref = ref(class_name)
end
self.mailer = "Devise::Mailer"
# Small method that adds a mapping to Devise.
def self.add_mapping(resource, options)
mapping = Devise::Mapping.new(resource, options)
@@mappings[mapping.name] = mapping
@@default_scope ||= mapping.name
2010-07-15 18:13:55 +02:00
@@helpers.each { |h| h.define_helpers(mapping) }
mapping
end
2010-07-13 10:09:55 +02:00
# Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
#
# == Options:
#
# +model+ - String representing the load path to a custom *model* for this module (to autoload.)
2013-04-18 10:24:38 +05:30
# +controller+ - Symbol representing the name of an existing or custom *controller* for this module.
# +route+ - Symbol representing the named *route* helper for this module.
# +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.
#
# == Examples:
#
# Devise.add_module(:party_module)
2014-02-25 22:12:55 +05:30
# Devise.add_module(:party_module, strategy: true, controller: :sessions)
# Devise.add_module(:party_module, model: 'party_module/model')
#
def self.add_module(module_name, options = {})
ALL << module_name
options.assert_valid_keys(:strategy, :model, :controller, :route, :no_input)
2010-07-13 10:09:55 +02:00
if strategy = options[:strategy]
strategy = (strategy == true ? module_name : strategy)
STRATEGIES[module_name] = strategy
2010-07-13 10:09:55 +02:00
end
2010-07-13 10:09:55 +02:00
if controller = options[:controller]
controller = (controller == true ? module_name : controller)
CONTROLLERS[module_name] = controller
2010-07-13 10:09:55 +02:00
end
NO_INPUT << strategy if options[:no_input]
2010-07-13 10:09:55 +02:00
if route = options[:route]
case route
when TrueClass
key, value = module_name, []
when Symbol
key, value = route, []
when Hash
key, value = route.keys.first, route.values.flatten
else
2010-07-13 10:09:55 +02:00
raise ArgumentError, ":route should be true, a Symbol or a Hash"
end
2010-07-13 10:09:55 +02:00
URL_HELPERS[key] ||= []
URL_HELPERS[key].concat(value)
URL_HELPERS[key].uniq!
ROUTES[module_name] = key
end
if options[:model]
2010-07-12 18:56:27 +02:00
path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
2011-04-16 13:30:15 +02:00
camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
Devise::Models.send(:autoload, camelized.to_sym, path)
end
Devise::Mapping.add_module module_name
end
# Sets warden configuration using a block that will be invoked on warden
# initialization.
#
# Devise.initialize do |config|
# config.allow_unconfirmed_access_for = 2.days
#
# config.warden do |manager|
# # Configure warden to use other strategies, like oauth.
# manager.oauth(:twitter)
# end
# end
def self.warden(&block)
@@warden_config_block = block
end
2010-10-14 20:04:02 +02:00
# Specify an omniauth provider.
#
# config.omniauth :github, APP_ID, APP_SECRET
#
def self.omniauth(provider, *args)
@@helpers << Devise::OmniAuth::UrlHelpers
config = Devise::OmniAuth::Config.new(provider, args)
@@omniauth_configs[config.strategy_name.to_sym] = config
2010-10-14 20:04:02 +02:00
end
# Include helpers in the given scope to AC and AV.
def self.include_helpers(scope)
ActiveSupport.on_load(:action_controller) do
2010-10-14 20:04:02 +02:00
include scope::Helpers if defined?(scope::Helpers)
include scope::UrlHelpers
end
ActiveSupport.on_load(:action_view) do
include scope::UrlHelpers
end
end
2011-09-02 13:35:31 +03:00
# Regenerates url helpers considering Devise.mapping
def self.regenerate_helpers!
Devise::Controllers::UrlHelpers.remove_helpers!
Devise::Controllers::UrlHelpers.generate_helpers!
end
# A method used internally to setup warden manager from the Rails initialize
# block.
2010-03-31 22:04:48 +02:00
def self.configure_warden! #:nodoc:
@@warden_configured ||= begin
warden_config.failure_app = Devise::Delegator.new
warden_config.default_scope = Devise.default_scope
warden_config.intercept_401 = false
Devise.mappings.each_value do |mapping|
2014-02-25 22:12:55 +05:30
warden_config.scope_defaults mapping.name, strategies: mapping.strategies
warden_config.serialize_into_session(mapping.name) do |record|
mapping.to.serialize_into_session(record)
end
warden_config.serialize_from_session(mapping.name) do |key|
# Previous versions contained an additional entry at the beginning of
# key with the record's class name.
args = key[-2, 2]
mapping.to.serialize_from_session(*args)
end
end
@@warden_config_block.try :call, Devise.warden_config
true
end
end
2009-10-21 00:12:21 -02:00
2013-04-18 10:24:38 +05:30
# Generate a friendly string randomly to be used as token.
def self.friendly_token
SecureRandom.urlsafe_base64(15).tr('lIO0', 'sxyz')
end
2011-02-15 11:33:54 +01:00
# constant-time comparison algorithm to prevent timing attacks
def self.secure_compare(a, b)
return false if a.blank? || b.blank? || a.bytesize != b.bytesize
2011-02-15 11:33:54 +01:00
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
2009-11-15 00:13:43 -02:00
end
require 'warden'
require 'devise/mapping'
require 'devise/models'
require 'devise/modules'
require 'devise/rails'