2012-03-31 07:17:16 -04:00
|
|
|
require 'devise/strategies/authenticatable'
|
2010-01-14 09:47:14 -05:00
|
|
|
|
|
|
|
module Devise
|
|
|
|
module Strategies
|
|
|
|
# Remember the user through the remember token. This strategy is responsible
|
|
|
|
# to verify whether there is a cookie with the remember token, and to
|
2010-03-31 15:43:19 -04:00
|
|
|
# recreate the user from this cookie if it exists. Must be called *before*
|
2010-01-14 09:47:14 -05:00
|
|
|
# authenticatable.
|
2010-07-23 17:57:31 -04:00
|
|
|
class Rememberable < Authenticatable
|
2010-01-14 09:47:14 -05:00
|
|
|
# A valid strategy for rememberable needs a remember token in the cookies.
|
|
|
|
def valid?
|
2011-06-30 07:03:24 -04:00
|
|
|
@remember_cookie = nil
|
|
|
|
remember_cookie.present?
|
2010-01-14 09:47:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# To authenticate a user we deserialize the cookie and attempt finding
|
|
|
|
# the record in the database. If the attempt fails, we pass to another
|
|
|
|
# strategy handle the authentication.
|
|
|
|
def authenticate!
|
2011-06-30 07:03:24 -04:00
|
|
|
resource = mapping.to.serialize_from_cookie(*remember_cookie)
|
2010-04-06 10:34:22 -04:00
|
|
|
|
2012-05-22 08:10:06 -04:00
|
|
|
unless resource
|
|
|
|
cookies.delete(remember_key)
|
|
|
|
return pass
|
|
|
|
end
|
|
|
|
|
2010-04-06 10:34:22 -04:00
|
|
|
if validate(resource)
|
2010-01-14 09:47:14 -05:00
|
|
|
success!(resource)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-02-24 15:55:41 -05:00
|
|
|
def decorate(resource)
|
|
|
|
super
|
|
|
|
resource.extend_remember_period = mapping.to.extend_remember_period if resource.respond_to?(:extend_remember_period=)
|
|
|
|
end
|
|
|
|
|
2010-07-23 10:31:42 -04:00
|
|
|
def remember_me?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-03-31 07:31:45 -04:00
|
|
|
def remember_key
|
2013-01-20 14:07:51 -05:00
|
|
|
mapping.to.rememberable_options.fetch(:key, "remember_#{scope}_token")
|
2010-03-31 07:31:45 -04:00
|
|
|
end
|
2011-06-30 07:03:24 -04:00
|
|
|
|
|
|
|
def remember_cookie
|
|
|
|
@remember_cookie ||= cookies.signed[remember_key]
|
|
|
|
end
|
|
|
|
|
2010-01-14 09:47:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-20 14:07:51 -05:00
|
|
|
Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)
|