Changeset 1754

Show
Ignore:
Timestamp:
01/15/06 12:19:51
Author:
miyagawa
Message:

Now it works with subclasses that overrides handle_request; API change for conditional authentication

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • HTTP-Server-Simple-Authen/trunk/Changes

    r1753 r1754  
    11Revision history for Perl extension HTTP::Server::Simple::Authen 
     2 
     30.03  2006-01-15T03:17:31Z 
     4        - Oops, handle_request() is typically subclassed, so prior 
     5          versions are not subject to work. 
     6        - Now authenticate() should be called explicitly from 
     7          subclass. Hence some methods like needs_authen() are removed 
     8          to be more DIY. 
    29 
    3100.02  2006-01-14T21:34:25Z 
  • HTTP-Server-Simple-Authen/trunk/Makefile.PL

    r1752 r1754  
    77        Authen::Simple => 0.04, 
    88        HTTP::Server::Simple => 0.16, 
     9        NEXT => 0, 
    910    }, 
    1011); 
  • HTTP-Server-Simple-Authen/trunk/lib/HTTP/Server/Simple/Authen.pm

    r1753 r1754  
    22 
    33use strict; 
    4 our $VERSION = '0.02'; 
     4our $VERSION = '0.03'; 
    55 
    66use Carp; 
     
    88use NEXT; 
    99 
    10 sub authenticate { 
     10sub do_authenticate { 
    1111    my $self = shift; 
    12     my($cgi) = @_; 
    1312    if (($ENV{HTTP_AUTHORIZATION} || '') =~ /^Basic (.*?)$/) { 
    1413        my($user, $pass) = split /:/, (MIME::Base64::decode($1) || ':'); 
    15         return $self->authen_handler->authenticate($user, $pass) 
    16             && $self->authorize_user($user); 
     14        if ($self->authen_handler->authenticate($user, $pass)) { 
     15            return $user; 
     16        } 
    1717    } 
    1818 
     
    2020} 
    2121 
    22 sub needs_authen { 1 } 
    2322sub authen_realm { "Authorized area" } 
    24 sub authorize_user { 1 } 
    2523 
    2624sub authen_handler { 
     
    2927} 
    3028 
    31 sub handle_request
     29sub authenticate
    3230    my $self = shift; 
    33     if ($self->needs_authen(@_) && ! $self->authenticate(@_)) { 
     31    my $user = $self->do_authenticate(); 
     32    unless (defined $user) { 
    3433        my $realm = $self->authen_realm(); 
    3534        print "HTTP/1.0 401\r\n"; 
    3635        print qq(WWW-Authenticate: Basic realm="$realm"\r\n\r\n); 
    3736        print "Authentication required."; 
    38     } else { 
    39         $self->NEXT::handle_request(@_); 
     37        return; 
    4038    } 
     39    return $user; 
    4140} 
    4241 
     
    5857  } 
    5958 
     59  sub handle_request { 
     60      my($self, $cgi) = @_; 
     61      my $user = $self->authenticate or return; 
     62      ... 
     63  } 
     64 
    6065  MyServer->new->run(); 
    6166 
     
    6570allow HTTP authentication. Authentication scheme is pluggable and you 
    6671can use whatever Authentication protocol that Authen::Simple supports. 
     72 
     73You can use C<authenticate> method whatever you want to authenticate 
     74the request. The method returns C<$username> taken from the request if 
     75the authentication is successful, and C<undef> otherwise. The code in 
     76L</SYNOPSIS> requires authentication for all the requests and behaves 
     77just the same as Apache's C<Require valid-user>. 
     78 
     79The following code will explain more about conditioning. 
     80 
     81  sub handle_request { 
     82      my($self, $cgi) = @_; 
     83      if ($cgi->path_info =~ m!/foo/!) { 
     84          my $user = $self->authenticate; 
     85          return unless defined($user) && length($user) == 8; 
     86      } 
     87      ... 
     88  } 
     89 
     90This means all the requests to URL C</foo/> require to be 
     91authenticated, and usernames with 8 chars long are authorized. 
    6792 
    6893=head1 METHODS 
     
    83108dialog box. Defaults to 'Authorized area'. 
    84109 
    85 =item needs_authen 
    86  
    87 Returns true if the request needs authentication. Takes C<$cgi> as a 
    88 parameter. Default to return 1 (which means all the requests should be 
    89 authenticated). 
    90  
    91 For example, you can use the following code to authenticate URL under 
    92 C</foo/>. 
    93  
    94   sub needs_authen { 
    95       my($self, $cgi) = @_; 
    96       return $cgi->path_info =~ m!/foo/!; 
    97   } 
    98  
    99 =item authorize_user 
    100  
    101 Returns true if you allow authenticated user to access the 
    102 content. Takes username as a parameter. By default it always returns 
    103 true, which means the same thing with Apache's C<Require valid-user>. 
    104  
    105 The following code means it only authorizes usernames with 8 chars 
    106 long. 
    107  
    108   sub authorize_user { 
    109       my($self, $username) = @_; 
    110       return length($username) == 8; 
    111   } 
    112  
    113110=back 
    114111 
  • HTTP-Server-Simple-Authen/trunk/server.pl

    r1752 r1754  
    1111} 
    1212 
     13sub handle_request { 
     14    my $self = shift; 
     15    my $user = $self->authenticate(); 
     16    return unless defined $user; 
     17    print "Hello World"; 
     18} 
     19 
    1320MyServer->new->run; 
    1421