| 1 | package Authen::Simple::Apache; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Authen::Simple::Adapter; | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | |||
| 9 | unless ( $INC{'mod_perl.pm'} ) { | ||
| 10 | |||
| 11 | my $class = 'mod_perl'; | ||
| 12 | |||
| 13 | if ( exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2 ) { | ||
| 14 | $class = 'mod_perl2'; | ||
| 15 | } | ||
| 16 | |||
| 17 | eval "require $class"; | ||
| 18 | } | ||
| 19 | |||
| 20 | my @import = qw( OK HTTP_UNAUTHORIZED SERVER_ERROR AUTH_REQUIRED ); | ||
| 21 | |||
| 22 | if ( $mod_perl::VERSION >= 1.999022 ) { # mod_perl 2.0.0 RC5 | ||
| 23 | require Apache2::RequestRec; | ||
| 24 | require Apache2::RequestUtil; | ||
| 25 | require Apache2::RequestIO; | ||
| 26 | require Apache2::Log; | ||
| 27 | require Apache2::Connection; | ||
| 28 | require Apache2::Const; | ||
| 29 | require Apache2::Access; | ||
| 30 | Apache2::Const->import(@import); | ||
| 31 | } | ||
| 32 | elsif ( $mod_perl::VERSION >= 1.99 ) { | ||
| 33 | require Apache::RequestRec; | ||
| 34 | require Apache::RequestUtil; | ||
| 35 | require Apache::RequestIO; | ||
| 36 | require Apache::Log; | ||
| 37 | require Apache::Connection; | ||
| 38 | require Apache::Const; | ||
| 39 | require Apache::Access; | ||
| 40 | Apache::Const->import(@import); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | require Apache; | ||
| 44 | require Apache::Log; | ||
| 45 | require Apache::Constants; | ||
| 46 | Apache::Constants->import(@import); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | use constant MP2 => $mod_perl::VERSION >= 1.99 ? 1 : 0; | ||
| 51 | |||
| 52 | sub handler_mp1 ($$) { &handle; } | ||
| 53 | sub handler_mp2 : method { &handle; } | ||
| 54 | |||
| 55 | *Authen::Simple::Adapter::handler = MP2 ? \&handler_mp2 : \&handler_mp1; | ||
| 56 | |||
| 57 | sub handle { | ||
| 58 | my ( $class, $r ) = @_; | ||
| 59 | |||
| 60 | my( $rc, $password ) = $r->get_basic_auth_pw; | ||
| 61 | |||
| 62 | unless ( $rc == OK ) { | ||
| 63 | return $rc; | ||
| 64 | } | ||
| 65 | |||
| 66 | my $username = MP2 ? $r->user : $r->connection->user; | ||
| 67 | |||
| 68 | unless ( defined($username) && length($username) ) { | ||
| 69 | $r->note_basic_auth_failure; | ||
| 70 | $r->log->error("PerlAuthenHandler $class - No username was given."); | ||
| 71 | return HTTP_UNAUTHORIZED; | ||
| 72 | } | ||
| 73 | |||
| 74 | unless ( defined($password) && length($password) ) { | ||
| 75 | $r->note_basic_auth_failure; | ||
| 76 | $r->log->error("PerlAuthenHandler $class - No password was given."); | ||
| 77 | return HTTP_UNAUTHORIZED; | ||
| 78 | } | ||
| 79 | |||
| 80 | ( my $prefix = $class ) =~ s/://g; | ||
| 81 | |||
| 82 | my %params = ( | ||
| 83 | log => $r->log | ||
| 84 | ); | ||
| 85 | |||
| 86 | while ( my ( $option, $spec ) = each( %{ $class->options } ) ) { | ||
| 87 | |||
| 88 | next if $option =~ /^(cache|callback|log)$/; | ||
| 89 | |||
| 90 | my $required = $spec->{default} ? 0 : $spec->{optional} ? 0 : 1; | ||
| 91 | my $config = $prefix . '_' . $option; | ||
| 92 | my $value = $r->dir_config($config); | ||
| 93 | |||
| 94 | if ( $required && !defined($value) ) { | ||
| 95 | $r->log->error( "PerlAuthenHandler $class - Required parameter '$config' is not set." ); | ||
| 96 | return SERVER_ERROR; | ||
| 97 | } | ||
| 98 | |||
| 99 | $params{ $option } = $value if defined($value); | ||
| 100 | } | ||
| 101 | |||
| 102 | my ( $self, $success ); | ||
| 103 | |||
| 104 | eval { $self = $class->new(%params); }; | ||
| 105 | |||
| 106 | if ( $@ ) { | ||
| 107 | $r->log->error( "PerlAuthenHandler $class - Couldn't create a new instance. Reason: '$@'" ); | ||
| 108 | return SERVER_ERROR; | ||
| 109 | } | ||
| 110 | |||
| 111 | eval { $success = $self->authenticate( $username, $password ); }; | ||
| 112 | |||
| 113 | if ( $@ ) { | ||
| 114 | $r->log->error( "PerlAuthenHandler $class - Couldn't authenticate. Reason: '$@'" ); | ||
| 115 | return SERVER_ERROR; | ||
| 116 | } | ||
| 117 | |||
| 118 | if (!$success) { | ||
| 119 | $r->note_basic_auth_failure; | ||
| 120 | return AUTH_REQUIRED; | ||
| 121 | } | ||
| 122 | |||
| 123 | return OK; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ |