Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rely on duck type instead of mappings settings.

  • Loading branch information...
commit 60fd9d26ea9072101474e70f40d1cb0dcae181a9 1 parent 1cf4dc7
@josevalim josevalim authored
View
3  CHANGELOG.rdoc
@@ -1,3 +1,6 @@
+* enhancements
+ * Added Http Basic Authentication support
+
== 0.9.2
* bug fix
View
1  TODO
@@ -1,4 +1,3 @@
* Make test run with DataMapper
* Add Registerable support
-* Add http authentication support
* Extract Activatable tests from Confirmable
View
5 lib/devise/mapping.rb
@@ -36,7 +36,10 @@ def self.find_by_path(path)
# Find a mapping by a given class. It takes into account single table inheritance as well.
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
# Receives an object and find a scope for it. If a scope cannot be found,
View
2  lib/devise/strategies/authenticatable.rb
@@ -6,7 +6,7 @@ module Strategies
# Redirects to sign_in page if it's not authenticated
class Authenticatable < Base
def valid?
- super && params[scope] && params[scope][:password].present?
+ params[scope] && params[scope][:password].present? && mapping.to.respond_to?(:authenticate)
end
# Authenticate a user based on email and password params, returning to warden
View
18 lib/devise/strategies/base.rb
@@ -2,22 +2,14 @@ module Devise
module Strategies
# Base strategy for Devise. Responsible for verifying correct scope and mapping.
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
# this scope.
def mapping
- Devise.mappings[scope]
- end
-
- # Store this class type.
- def klass_type
- @klass_type ||= self.class.name.split("::").last.underscore.to_sym
+ @mapping ||= begin
+ mapping = Devise.mappings[scope]
+ raise "Could not find mapping for #{scope}" unless mapping
+ mapping
+ end
end
end
end
View
2  lib/devise/strategies/http_authenticatable.rb
@@ -5,7 +5,7 @@ module Strategies
# Sign in an user using HTTP authentication.
class HttpAuthenticatable < Base
def valid?
- http_authentication?
+ http_authentication? && mapping.to.respond_to?(:authenticate_with_http)
end
def authenticate!
View
2  lib/devise/strategies/rememberable.rb
@@ -10,7 +10,7 @@ class Rememberable < Devise::Strategies::Base
# A valid strategy for rememberable needs a remember token in the cookies.
def valid?
- super && remember_me_cookie.present?
+ remember_me_cookie.present? && mapping.to.respond_to?(:serialize_from_cookie)
end
# To authenticate a user we deserialize the cookie and attempt finding
View
2  lib/devise/strategies/token_authenticatable.rb
@@ -6,7 +6,7 @@ module Strategies
# Redirects to sign_in page if it's not authenticated.
class TokenAuthenticatable < Base
def valid?
- super && authentication_token(scope).present?
+ mapping.to.respond_to?(:authenticate_with_token) && authentication_token(scope).present?
end
# Authenticate a user based on authenticatable token params, returning to warden
Please sign in to comment.
Something went wrong with that request. Please try again.