Rely on duck type instead of mappings settings.
This commit is contained in:
parent
1cf4dc798d
commit
60fd9d26ea
|
@ -1,3 +1,6 @@
|
||||||
|
* enhancements
|
||||||
|
* Added Http Basic Authentication support
|
||||||
|
|
||||||
== 0.9.2
|
== 0.9.2
|
||||||
|
|
||||||
* bug fix
|
* bug fix
|
||||||
|
|
1
TODO
1
TODO
|
@ -1,4 +1,3 @@
|
||||||
* Make test run with DataMapper
|
* Make test run with DataMapper
|
||||||
* Add Registerable support
|
* Add Registerable support
|
||||||
* Add http authentication support
|
|
||||||
* Extract Activatable tests from Confirmable
|
* Extract Activatable tests from Confirmable
|
|
@ -36,7 +36,10 @@ module Devise
|
||||||
|
|
||||||
# Find a mapping by a given class. It takes into account single table inheritance as well.
|
# Find a mapping by a given class. It takes into account single table inheritance as well.
|
||||||
def self.find_by_class(klass)
|
def self.find_by_class(klass)
|
||||||
Devise.mappings.values.find { |m| return m if klass <= m.to }
|
Devise.mappings.each_value do |mapping|
|
||||||
|
return mapping if klass <= mapping.to
|
||||||
|
end
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Receives an object and find a scope for it. If a scope cannot be found,
|
# Receives an object and find a scope for it. If a scope cannot be found,
|
||||||
|
|
|
@ -6,7 +6,7 @@ module Devise
|
||||||
# Redirects to sign_in page if it's not authenticated
|
# Redirects to sign_in page if it's not authenticated
|
||||||
class Authenticatable < Base
|
class Authenticatable < Base
|
||||||
def valid?
|
def valid?
|
||||||
super && params[scope] && params[scope][:password].present?
|
params[scope] && params[scope][:password].present? && mapping.to.respond_to?(:authenticate)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Authenticate a user based on email and password params, returning to warden
|
# Authenticate a user based on email and password params, returning to warden
|
||||||
|
|
|
@ -2,22 +2,14 @@ module Devise
|
||||||
module Strategies
|
module Strategies
|
||||||
# Base strategy for Devise. Responsible for verifying correct scope and mapping.
|
# Base strategy for Devise. Responsible for verifying correct scope and mapping.
|
||||||
class Base < ::Warden::Strategies::Base
|
class Base < ::Warden::Strategies::Base
|
||||||
# Validate strategy. By default will raise an error if no scope or an
|
|
||||||
# invalid mapping is found.
|
|
||||||
def valid?
|
|
||||||
raise "Could not find mapping for #{scope}" unless mapping
|
|
||||||
mapping.for.include?(klass_type)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Checks if a valid scope was given for devise and find mapping based on
|
# Checks if a valid scope was given for devise and find mapping based on
|
||||||
# this scope.
|
# this scope.
|
||||||
def mapping
|
def mapping
|
||||||
Devise.mappings[scope]
|
@mapping ||= begin
|
||||||
|
mapping = Devise.mappings[scope]
|
||||||
|
raise "Could not find mapping for #{scope}" unless mapping
|
||||||
|
mapping
|
||||||
end
|
end
|
||||||
|
|
||||||
# Store this class type.
|
|
||||||
def klass_type
|
|
||||||
@klass_type ||= self.class.name.split("::").last.underscore.to_sym
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Devise
|
||||||
# Sign in an user using HTTP authentication.
|
# Sign in an user using HTTP authentication.
|
||||||
class HttpAuthenticatable < Base
|
class HttpAuthenticatable < Base
|
||||||
def valid?
|
def valid?
|
||||||
http_authentication?
|
http_authentication? && mapping.to.respond_to?(:authenticate_with_http)
|
||||||
end
|
end
|
||||||
|
|
||||||
def authenticate!
|
def authenticate!
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Devise
|
||||||
|
|
||||||
# A valid strategy for rememberable needs a remember token in the cookies.
|
# A valid strategy for rememberable needs a remember token in the cookies.
|
||||||
def valid?
|
def valid?
|
||||||
super && remember_me_cookie.present?
|
remember_me_cookie.present? && mapping.to.respond_to?(:serialize_from_cookie)
|
||||||
end
|
end
|
||||||
|
|
||||||
# To authenticate a user we deserialize the cookie and attempt finding
|
# To authenticate a user we deserialize the cookie and attempt finding
|
||||||
|
|
|
@ -6,7 +6,7 @@ module Devise
|
||||||
# Redirects to sign_in page if it's not authenticated.
|
# Redirects to sign_in page if it's not authenticated.
|
||||||
class TokenAuthenticatable < Base
|
class TokenAuthenticatable < Base
|
||||||
def valid?
|
def valid?
|
||||||
super && authentication_token(scope).present?
|
mapping.to.respond_to?(:authenticate_with_token) && authentication_token(scope).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Authenticate a user based on authenticatable token params, returning to warden
|
# Authenticate a user based on authenticatable token params, returning to warden
|
||||||
|
|
Loading…
Reference in New Issue