Apache2-AuthenNTLM-Cookie
view release on metacpan or search on metacpan
lib/Apache2/AuthenNTLM/Cookie.pm view on Meta::CPAN
secret => $r->dir_config('secret') || $class->default_secret,
refresh => $r->dir_config('refresh') || 14400, # in seconds
cookie_name => $r->dir_config('cookie_name') || 'NTLM_AUTHEN',
}, $class;
my $result;
# get the cookie
my $jar = Apache2::Cookie::Jar->new($r);
my $cookie = $jar->cookies($self->{cookie_name});
my $has_valid_cookie = $cookie && $self->validate_cookie($cookie->value);
# if cookie is present and valid
if ($has_valid_cookie) {
$result = Apache2::Const::OK;
# if MSIE "optimization" is activated, i.e. if this is a POST with an
# NTLM type1 message and without body ...
if ($r->method eq 'POST' && $self->has_empty_body && $self->is_NTLM_msg1) {
# ... then we must fake a type2 msg so that MSIE will post again
lib/Apache2/AuthenNTLM/Cookie.pm view on Meta::CPAN
# if no NTLM message, directly ask for authentication (avoid calling
# Apache2::AuthenNTLM because it pollutes the error log)
if (!$self->get_NTLM_msg && $self->is_ntlmauthoritative) {
$self->ask_for_authentication;
$result = Apache2::Const::HTTP_UNAUTHORIZED;
}
# else invoke Apache2::AuthenNTLM to go through the NTLM handshake
else {
my $msg = $cookie ? "cookie invalidated" : "no cookie";
$r->log->debug("AuthenNTLM::Cookie: $msg, calling Apache2::AuthenNTLM");
$result = Apache2::AuthenNTLM->handler($r); # will set $r->user
# create the cookie if NTLM succeeded
$self->set_cookie if $result == Apache2::Const::OK;
}
}
return $result;
}
sub validate_cookie {
my ($self, $cookie_val) = @_;
# unpack cookie information
my ($sha, $time_created, $username) = unpack COOKIE_FORMAT, $cookie_val;
# valid if not too old and matches the SHA1 digest
my $now = time;
my $is_valid
= ($now - $time_created) < $self->{refresh}
&& $sha eq sha1_hex($time_created, $username, $self->{secret});
lib/Apache2/AuthenNTLM/Cookie.pm view on Meta::CPAN
This is a secret phrase for generating a SHA1 digest that will be
incorporated into the cookie. The digest also incorporates the
username and cookie creation time, and is checked at each request :
therefore it is impossible to forge a fake cookie without knowing the
secret.
The default value for the secret is the concatenation of modification
time and inode of the F<httpd.conf> file on the server; therefore if
the configuration file changes, authentication cookies are
automatically invalidated.
=back
=head1 SPECIAL NOTE ABOUT INTERNET EXPLORER
Microsoft Internet Explorer (MSIE) has an "optimization" when sending
POST requests to an NTLM-secured site : the browser does not send the
request body because it expects to receive a 401 HTTP_UNAUTHORIZED
response, and then would send the body only at the second
request. This is a problem with the present module, because if
( run in 0.258 second using v1.01-cache-2.11-cpan-a5abf4f5562 )