| 1 | # $Id: AuthenSecurID.pm,v 1.7 2007/12/08 03:20:58 atobey Exp $ | ||
| 2 | |||
| 3 | package Apache2::AuthenSecurID; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Apache2::Const qw(OK AUTH_REQUIRED DECLINED REDIRECT SERVER_ERROR); | ||
| 7 | use Apache2::RequestUtil (); | ||
| 8 | use Apache2::RequestRec (); | ||
| 9 | use Apache2::Cookie; | ||
| 10 | use Crypt::CBC; | ||
| 11 | use CGI::Carp; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | |||
| 14 | $VERSION = '0.5'; | ||
| 15 | |||
| 16 | sub handler { | ||
| 17 | my $r = shift; | ||
| 18 | |||
| 19 | |||
| 20 | # get configuration directives | ||
| 21 | my $auth_cookie = $r->dir_config("AuthCookie") || "SecurID"; | ||
| 22 | my $auth_user_cookie = $r->dir_config("AuthUserCookie")||"SecurID_User"; | ||
| 23 | |||
| 24 | my $crypt_key = $r->dir_config("AuthCryptKey") || "my secret"; | ||
| 25 | |||
| 26 | my $cookie_timeout = $r->dir_config("AuthCookieTimeOut") || 30; | ||
| 27 | my $cookie_path = $r->dir_config("AuthCookiePath") || "/"; | ||
| 28 | |||
| 29 | my $auth_handler = $r->dir_config("Auth_Handler") || "/ace_init"; | ||
| 30 | |||
| 31 | # get cookies | ||
| 32 | my ( $session_key ) = ( ($r->headers_in->{Cookie} || "") =~ | ||
| 33 | /${auth_cookie}=([^;]+)/); | ||
| 34 | my ( $session_user ) = ( ($r->headers_in->{Cookie} || "") =~ | ||
| 35 | /${auth_user_cookie}=([^;]+)/); | ||
| 36 | |||
| 37 | |||
| 38 | my $username; | ||
| 39 | my $session_time; | ||
| 40 | |||
| 41 | # decrypt cookie | ||
| 42 | my $cipher = new Crypt::CBC($crypt_key,"Blowfish") || warn ( $! ); | ||
| 43 | if ( $session_key ) { | ||
| 44 | my $plaintext_cookie = $cipher->decrypt_hex($session_key); | ||
| 45 | ( $session_time, $username ) = split /\:/, $plaintext_cookie; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $time = time(); | ||
| 49 | my $timeout = $time - 60 * $cookie_timeout; | ||
| 50 | my $uri = $r->uri; | ||
| 51 | |||
| 52 | # check cookie | ||
| 53 | if ( $session_key && $username eq $session_user && | ||
| 54 | $timeout <= $session_time ) { | ||
| 55 | $r->no_cache(1); | ||
| 56 | $r->err_headers_out->add("Pragma" => "no-cache" ); | ||
| 57 | #reset timestamp | ||
| 58 | my $crypt_cookie = $cipher->encrypt_hex ("$time:$username"); | ||
| 59 | $r->err_headers_out->add("Set-Cookie" => $auth_cookie . "=" . | ||
| 60 | $crypt_cookie . "; path=" . $cookie_path ); | ||
| 61 | return OK; | ||
| 62 | } else { | ||
| 63 | # redirect to authentication handler | ||
| 64 | my $uri = $cipher->encrypt_hex ( $uri ); | ||
| 65 | $r->no_cache(1); | ||
| 66 | $r->err_headers_out->add("Pragma" => "no-cache"); | ||
| 67 | $r->headers_out->add("Location" => "$auth_handler?a=" . $uri ); | ||
| 68 | return REDIRECT; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ |