0
@@ -48,21 +48,30 @@ module Halcyon
0
# Ensures that the HTTP Authentication header is included, the Basic
0
- # scheme is being used, and the credentials pass the
0
- # +basic_authentication+ test. If any of these fail, an Unauthorized
0
- # exception is raised (except for non-Basic schemes), otherwise the
0
- # +route+ is +run+ normally.
0
+ # scheme is being used, and the credentials pass the +basic_auth+
0
+ # test. If any of these fail, an Unauthorized exception is raised
0
+ # (except for non-Basic schemes), otherwise the +route+ is +run+
0
+ # See the documentation for the +basic_auth+ class method for details
0
+ # concerning the credentials and action inclusion/exclusion.
0
- # make sure there's an authorization header
0
- raise Base::Exceptions::Unauthorized.new unless !authorization_key.nil?
0
- # make sure the request is via the Basic protocol
0
- scheme = @env[authorization_key].split.first.downcase.to_sym
0
- raise Base::Exceptions::BadRequest.new unless scheme == :basic
0
- # make sure the credentials pass the test
0
- credentials = @env[authorization_key].split.last.unpack("m*").first.split(/:/, 2)
0
- raise Base::Exceptions::Unauthorized.new unless basic_authentication(*credentials)
0
+ # test credentials if the action is one specified to be tested
0
+ if ((@@auth[:except].nil? && @@auth[:only].nil?) || # the default is to test if no restrictions
0
+ (!@@auth[:only].nil? && @@auth[:only].include?(route[:action].to_sym)) || # but if the action is in the :only directive, test
0
+ (!@@auth[:except].nil? && !@@auth[:except].include?(route[:action].to_sym))) # or if the action is not in the :except directive, test
0
+ # make sure there's an authorization header
0
+ raise Base::Exceptions::Unauthorized.new unless !authorization_key.nil?
0
+ # make sure the request is via the Basic protocol
0
+ scheme = @env[authorization_key].split.first.downcase.to_sym
0
+ raise Base::Exceptions::BadRequest.new unless scheme == :basic
0
+ # make sure the credentials pass the test
0
+ credentials = @env[authorization_key].split.last.unpack("m*").first.split(':', 2)
0
+ raise Base::Exceptions::Unauthorized.new unless @@auth[:method].call(*credentials)
0
# success, so run the route normally
0
@@ -73,6 +82,24 @@ module Halcyon
0
{:status => e.status, :body => e.error}
0
+ # Provides a way to define a test as well as set limits on what is
0
+ # tested for Basic Authorization. This method should be called in the
0
+ # definition of the server. A simple example would look like:
0
+ # class Servr < Halcyon::Server::Auth::Basic
0
+ # basic_auth :only => [:grant] do |user, pass|
0
+ # # routes and actions follow...
0
+ # Two acceptable options include <tt>:only</tt> and <tt>:except</tt>.
0
+ def self.basic_auth(options={}, &proc)
0
+ @@auth = options.merge(:method => proc)
Comments
No one has commented yet.