230
|
1 package IMPL::Web::Handler::SecureCookie;
|
196
|
2 use strict;
|
|
3
|
230
|
4
|
196
|
5 use Digest::MD5 qw(md5_hex);
|
230
|
6 use IMPL::Const qw(:prop);
|
|
7 use IMPL::Security::Auth qw(:Const GenSSID);
|
|
8 use IMPL::declare {
|
|
9 require => {
|
|
10 SecurityContext => 'IMPL::Security::Context',
|
238
|
11 User => 'IMPL::Security::Principal',
|
230
|
12 AuthSimple => 'IMPL::Security::Auth::Simple',
|
231
|
13 Exception => 'IMPL::Exception',
|
|
14 OperationException => '-IMPL::InvalidOperationException',
|
|
15 HttpResponse => '-IMPL::Web::HttpResponse'
|
230
|
16 },
|
|
17 base => {
|
|
18 'IMPL::Object' => undef,
|
|
19 'IMPL::Object::Autofill' => '@_',
|
|
20 'IMPL::Object::Serializable' => undef
|
|
21 },
|
|
22 props => [
|
|
23 salt => PROP_RO,
|
238
|
24 _manager => PROP_RO,
|
230
|
25 _cookies => PROP_RW
|
|
26 ]
|
|
27 };
|
196
|
28
|
|
29 sub CTOR {
|
|
30 my ($this) = @_;
|
|
31
|
|
32 $this->salt('DeadBeef') unless $this->salt;
|
|
33 }
|
|
34
|
230
|
35 sub Invoke {
|
196
|
36 my ($this,$action,$nextHandler) = @_;
|
|
37
|
230
|
38 return unless $nextHandler;
|
|
39
|
|
40 my $context;
|
238
|
41 $this->_manager($action->application->security);
|
196
|
42
|
230
|
43
|
|
44 my $sid = $action->cookie('sid',qr/(\w+)/);
|
|
45 my $cookie = $action->cookie('sdata',qr/(\w+)/);
|
|
46 my $sign = $action->cookie('sign',qw/(\w+)/);
|
196
|
47
|
230
|
48 if (
|
|
49 $sid and
|
|
50 $cookie and
|
|
51 $sign and
|
|
52 $sign eq md5_hex(
|
|
53 $this->salt,
|
|
54 $sid,
|
|
55 $cookie,
|
|
56 $this->salt
|
|
57 )
|
|
58 ) {
|
|
59 # TODO: add a DefferedProxy to deffer a request to a data source
|
238
|
60 if ( $context = $this->_manager->GetSession($sid) ) {
|
196
|
61
|
230
|
62 if ( eval { $context->auth->isa(AuthSimple) } ) {
|
|
63 my ($result,$challenge) = $context->auth->DoAuth($cookie);
|
|
64
|
238
|
65 $action->_manager->SaveSession($context);
|
230
|
66
|
|
67 if ($result == AUTH_FAIL) {
|
|
68 $context = undef;
|
|
69 }
|
196
|
70 }
|
|
71 }
|
230
|
72
|
196
|
73 }
|
230
|
74
|
231
|
75 $context ||= SecurityContext->new(principal => User->nobody, authority => $this);
|
230
|
76
|
238
|
77 my $httpResponse = $context->Impersonate($nextHandler,$action);
|
230
|
78
|
231
|
79 die OperationException->new("A HttpResponse instance is expected")
|
|
80 unless ref $httpResponse && eval { $httpResponse->isa(HttpResponse) };
|
230
|
81
|
231
|
82 return $this->WriteResponse($httpResponse);
|
230
|
83 }
|
|
84
|
231
|
85 sub InitSession {
|
230
|
86 my ($this,$user,$auth,$roles) = @_;
|
|
87
|
|
88 my $sid = GenSSID();
|
|
89 my $cookie = GenSSID();
|
|
90
|
|
91 $this->_cookies({
|
|
92 sid => $sid,
|
|
93 sdata => $cookie
|
238
|
94 });
|
230
|
95
|
238
|
96 my $context = $this->_manager->CreateSession(
|
230
|
97 sessionId => $sid,
|
|
98 principal => $user,
|
233
|
99 auth => AuthSimple->Create(password => $cookie),
|
230
|
100 authority => $this,
|
|
101 assignedRoles => $roles
|
|
102 );
|
|
103
|
|
104 $context->Apply();
|
|
105
|
|
106 return $context;
|
196
|
107 }
|
|
108
|
|
109 sub WriteResponse {
|
230
|
110 my ($this,$response) = @_;
|
|
111
|
238
|
112 if (my $data = $this->_cookies) {
|
196
|
113
|
230
|
114 my $sign = md5_hex(
|
|
115 $this->salt,
|
|
116 $data->{sid},
|
|
117 $data->{sdata},
|
|
118 $this->salt
|
|
119 );
|
|
120
|
|
121 $response->cookies->{sid} = $data->{sid};
|
|
122 $response->cookies->{sdata} = $data->{sdata};
|
|
123 $response->cookies->{sign} = $sign;
|
|
124 }
|
231
|
125
|
|
126 return $response;
|
196
|
127 }
|
|
128
|
|
129 1;
|
|
130
|
|
131 __END__
|
|
132
|
|
133 =pod
|
|
134
|
|
135 =head1 NAME
|
|
136
|
230
|
137 C<IMPL::Web::Handler::SecureCookie>
|
196
|
138
|
|
139 =head1 DESCRIPTION
|
|
140
|
|
141 Возобновляет сессию пользователя на основе информации переданной через Cookie.
|
|
142
|
|
143 Использует механизм подписи информации для проверки верности входных данных перед
|
|
144 началом каких-либо действий.
|
|
145
|
|
146 Данный обработчик возвращает результат выполнения следдующего обработчика.
|
|
147
|
230
|
148
|
|
149
|
196
|
150 =head1 MEMBERS
|
|
151
|
231
|
152 =head2 C<[get,set] salt>
|
196
|
153
|
|
154 Скаляр, использующийся для подписи данных.
|
|
155
|
233
|
156
|
231
|
157 =head2 C<InitSession($user,$auth,$roles)>
|
196
|
158
|
|
159 =cut
|