Apache-AuthCookieNTLM

 view release on metacpan or  search on metacpan

lib/Apache/AuthCookieNTLM.pm  view on Meta::CPAN

	my %cookiejar = Apache::Cookie->new($t)->parse;
	
	 if (!defined $cookiejar{$cname}
	         or ($r->method eq 'POST' and $r->header_in('content-length') == 0)){
	
		# Don't have the cookie, try authenticate
		my $v = Apache::AuthenNTLM::handler ($self, $r);
				
		if ($v == 0 && $cookie_values ne {}) {	
			# Set the cookie as we have user details
			my $cookie = Apache::Cookie->new($r,
				-name		=> $cname,
				-value		=> $cookie_values,
				-path		=> $config{'path'}	|| "/",
				);
			$cookie->expires($config{'expires'}) if defined $config{'expires'};
			$cookie->domain($config{'domain'}) if defined $config{'domain'};
			$cookie->secure('1') if defined $config{'secure'};
			
			# Set the cookie to header
			$r->header_out('Set-Cookie' => $cookie->bake());

			if($debug > 0) {
				print STDERR "AuthCookieNTLM - Setting Cookie Expire: " . $config{'expires'} . "\n" if $debug > 0 && defined $config{'expires'};
				print STDERR "AuthCookieNTLM - Setting Cookie Domain: " . $config{'domain'} . "\n" if $debug > 0 && defined $config{'domain'};
				print STDERR "AuthCookieNTLM - Setting Cookie Secure: " . $config{'secure'} . "\n" if $debug > 1 && defined $config{'secure'};
				print STDERR "AuthCookieNTLM - Setting Cookie values: " . Dumper($cookie_values) . "\n" if $debug > 1;
			}			
		}
		# AuthenNTLM loops so have to behave like it does
		# and return $v
		return $v;
	} else {
		print STDERR "AuthCookieNTLM - Found Cookies for '$cname'\n" if $debug > 0;
		my %c = $cookiejar{$cname}->parse();
		if(defined $c{$cname}) {
			print STDERR "AuthCookieNTLM - Cookie Matched \n" if $debug > 1;
			my %v = $c{$cname}->value();
			print STDERR "AuthCookieNTLM - Cookie values " . Dumper(\%v) . "\n" if $debug > 1;
			if(defined $v{'username'} && defined $v{'userdomain'}) {
				my $user = lc($v{'userdomain'} . '\\' . $v{'username'});
		        $r ->user($user) if ref($r) eq 'Apache';
				print STDERR "AuthCookieNTLM - REMOVE_USER SET: " . $user . "\n" if $debug > 1;
			}
		}
	}

	return OK;
}

sub check_cookie {
	my $self = shift;
	return 1 if ( $cookie_values eq {} || $cookie_values->{username} ne $self->{username} );
	return undef;
}

# Private method to set the cookie
sub set_cookie {
	my ($self, $conf) = @_;
	
	# Must have the user name to validate check_cookie()
	$cookie_values->{'username'} = $self->{'username'};
	$cookie_values->{'userdomain'} = $self->{'userdomain'};

	while( my ($name, $value) = each %{$conf}) {
		$cookie_values->{$name} = $value;
	}
};

# This is the method which others could overload to
# set what ever values they want.
sub choose_cookie_values {
	my ($self,$r) = @_;
	
	# Save
	if ($self->check_cookie()) {
		$self->set_cookie();
	}
}

# Overloaded to allow us to call choose_cookie_values
# and get access to the object.
sub map_user {
    my ($self, $r) = @_ ;
	
    $self->choose_cookie_values($r);

    return lc("$self->{userdomain}\\$self->{username}");
}


1;

__END__

=head1 NAME

Apache::AuthCookieNTLM - NTLM (Windows domain) authentication with cookies

=head1 SYNOPSIS

'WhatEver' should be replaced with the AuthName you choose
for this location's authentication.

    <Location />
        PerlAuthenHandler Apache::AuthCookieNTLM

        # NTLM CONFIG
        AuthType ntlm,basic
        AuthName WhatEver
        require valid-user

        #                   domain          pdc               bdc
        PerlAddVar ntdomain "name_domain1   name_of_pdc1"
        PerlAddVar ntdomain "other_domain   pdc_for_domain    bdc_for_domain"

        PerlSetVar defaultdomain default_domain
        PerlSetVar ntlmdebug 1

        # COOKIE CONFIG - all are optional and have defaults
        PerlSetVar WhatEverName cookie_name



( run in 1.738 second using v1.01-cache-2.11-cpan-d8267643d1d )