view Lib/IMPL/Web/QueryHandler/SecureCookie.pm @ 95:67eb8eaec3d4

Added a security authority property to the Context and Security classes Added a WriteResponse method to the SecureCookie class Added a setCookie method to the Response class
author wizard
date Thu, 29 Apr 2010 02:21:27 +0400
parents 3d1f584aea60
children 964587c5183c
line wrap: on
line source

package IMPL::Web::QueryHandler::SecureCookie;
use strict;

use base qw(IMPL::Web::QueryHandler);
use Digest::MD5 qw(md5_hex);

use IMPL::Class::Property;
use IMPL::Security::Auth qw(:Const);
use IMPL::Security;

BEGIN {
	public property salt => prop_all;
}

sub CTOR {
	my ($this) = @_;
	
	$this->salt('DeadBeef') unless $this->salt;
}

sub Process {
	my ($this,$action,$nextHandler) = @_;
	
	return undef unless $nextHandler;
	
	my $method = $action->query->cookie('method') || 'simple';
	
	if ($method eq 'simple') {
		
		my $sid = $action->query->cookie('sid'); 
		my $cookie = $action->query->cookie('sdata');
		my $sign = $action->query->cookie('sign'); 
		
		if (
			$sid and
			$cookie and
			$sign and
			$sign eq md5_hex(
				$this->salt,
				$sid,
				$cookie,
				$this->salt
			)
		) {
			local $IMPL::Security::authority = $this;
			
			my $context = $action->application->security->sourceSession->find(
				{ id => $sid }
			) or return $nextHandler->();
			
			my ($result,$challenge) = $context->auth->ValidateSession($cookie);
			
			if ($result == AUTH_SUCCESS) {
				$context->authority($this);
				return $context->Impersonate($nextHandler);				
			} else {
				return $nextHandler->();
			}
		}
	} else {
		die new IMPL::Exception("Unknown auth method",$method);
	}
}

sub WriteResponse {
	my ($this,$response,$sid,$cookie,$method) = @_;

	my $sign = md5_hex(
		$this->salt,
		$sid,
		$cookie,
		$this->salt
	);
	
	$this->setCookie(sid => $sid);
	$this->setCookie(sdata => $cookie);
	$this->setCookie(sign => $sign);
	$this->setCookie(method => $method) if $method;
}

1;

__END__

=pod

=head1 NAME

C<IMPL::Web::QueryHandler::SecureCookie>

=head1 DESCRIPTION

C<use base qw(IMPL::Web::QueryHandler)>

Возобновляет сессию пользователя на основе информации переданной через Cookie.

Использует механизм подписи информации для проверки верности входных данных перед
началом каких-либо действий.

Данный обработчик возвращает результат выполнения следдующего обработчика.

=head1 MEMBERS

=over

=item C<[get,set] salt>

Скаляр, использующийся для подписи данных.

=back

=cut