yibbiy has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a custom auth handler for apache using Apache2::Access. I had to use Net::SSH::Perl for authentication. It takes a while to authenticate with Net::SSH::Perl so I was hoping to authenticate just once instead of every time.

I have tried using $r->notes->set(Key=>Value) at the beginning of the PerlAthenHandler but there is no data in notes. However other pages can get the hash data after the authentication.

I also tried $r->headers_out->add(Key=Value) and there is also no data. Like above pages after authentication can access this data. Looking at the http headers after auth shows the headers_out I created during the auth stage.

What am I missing to get PerlAutenHandler to read $r->notes or $r->headers_out->get??

Snipplet of code:
sub handler { my $r = shift; my($res, $password) = $r->get_basic_auth_pw; return $res if $res != OK; my $user = $r->user; #EMPTY my $remote_user = $r->notes->get("User"); #EMPTY my $test = $r->headers_out->get("User"); if ($remote_user ne "" || $test ne ""){ return Apache2::Const::OK; } #SSH VALIDATE (NOT ALL THE CODE HERE) eval { $ssh->login($user,$password); }; if ($@){ $r->note_basic_auth_failure; return Apache2::Const::AUTH_REQUIRED; } $r->err_headers_out->add('User') = $user; $r->notes->add("User" => $user); return Apache2::Const::OK; } 1; __END__

Replies are listed 'Best First'.
Re: PerlAuthenHandler Problems
by xicheng (Sexton) on Jul 18, 2007 at 21:37 UTC
    OP> What am I missing to get PerlAutenHandler to read $r->notes or $r->headers_out->get??

    Becouse they live only in the duration of the current request. you can not get the same infor across different requests.

    You probably can use something like the following though:
    my %auth; sub handler { my $r = shift; my($res, $password) = $r->get_basic_auth_pw; return $res if $res != OK; my $user = $r->user; if (exists $auth{$user}){ return Apache2::Const::OK; } ..... $auth{$user} = $user; return Apache2::Const::OK; } 1;
    (untested)
    Regards,
    Xicheng
      Works like a charm. Thank your from saving me from the evil cookie monsters!!!! Sometimes you just think to hard about the situation at hand and make it to complex. Thanks for simplifying it! Yibbiy