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