annotate Lib/IMPL/Web/Security.pm @ 400:ef37ec102dab

redirection support while fetching child resources
author cin
date Wed, 14 May 2014 17:27:32 +0400
parents 69a1f1508696
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
1 package IMPL::Web::Security;
81
077357224bec IMPL::Web::Security alpha version
Sergey
parents: 79
diff changeset
2 use strict;
73
wizard
parents: 66
diff changeset
3
81
077357224bec IMPL::Web::Security alpha version
Sergey
parents: 79
diff changeset
4 use IMPL::Security::Auth qw(:Const);
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
5 use IMPL::declare {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
6 require => {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
7 Exception => 'IMPL::Exception',
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
8 NotImplementedException => '-IMPL::NotImplementedException',
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
9 ArgException => '-IMPL::InvalidArgumentException',
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
10 SecurityContext => 'IMPL::Security::AbstractContext'
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
11 },
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
12 };
107
0e72ad99eef7 Updated Web::TT
wizard
parents: 97
diff changeset
13
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
14 use constant {
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
15 ERR_NO_SUCH_USER => -1,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
16 ERR_NO_SEC_DATA => -2,
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
17 ERR_NO_AUTHORITY => -3,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
18 ERR_NO_SEC_CONTEXT => -4,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
19 ERR_AUTH_FAIL => -5
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
20 };
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
21
393
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
22 sub interactiveAuthPackage {
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
23 die NotImplementedException->new();
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
24 }
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
25
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
26 sub users {
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
27 die NotImplementedException->new();
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
28 }
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
29
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
30 sub roles {
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
31 die die NotImplementedException->new();
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
32 }
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
33
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
34 sub sessions {
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
35 die NotImplementedException->new();
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
36 }
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
37
81
077357224bec IMPL::Web::Security alpha version
Sergey
parents: 79
diff changeset
38 sub AuthUser {
393
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
39 my ($this,$name,$challenge,$roles,$package) = @_;
391
2287c72f303a code cleanup
cin
parents: 328
diff changeset
40
2287c72f303a code cleanup
cin
parents: 328
diff changeset
41 $package ||= $this->interactiveAuthPackage;
393
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
42 $roles ||= [];
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
43
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
44 my $user = $this->users->GetById($name)
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
45 or return {
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
46 status => AUTH_FAIL,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
47 code => ERR_NO_SUCH_USER
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
48 };
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
49
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
50 my $auth;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
51 if ( my $secData = $user->GetSecData($package) ) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
52 $auth = $package->new($secData);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
53 } else {
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
54 return {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
55 status => AUTH_FAIL,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
56 code => ERR_NO_SEC_DATA,
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
57 user => $user
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
58 };
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
59 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
60
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
61 return {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
62 status => AUTH_FAIL,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
63 code => ERR_NO_SEC_CONTEXT
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
64 } unless SecurityContext->current;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
65
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
66 return {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
67 status => AUTH_FAIL,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
68 code => ERR_NO_AUTHORITY
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
69 } unless SecurityContext->current->authority;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
70
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
71 my $status = SecurityContext->current->authority->InitSession(
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
72 $user,
393
69a1f1508696 minor security refactoring
cin
parents: 391
diff changeset
73 $roles,
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
74 $auth,
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
75 $challenge
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
76 );
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
77
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
78 return {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
79 status => $status,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
80 code => ($status == AUTH_FAIL ? ERR_AUTH_FAIL : 0),
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
81 user => $user
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
82 };
73
wizard
parents: 66
diff changeset
83 }
wizard
parents: 66
diff changeset
84
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
85 sub Logout {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
86 my ($this) = @_;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
87
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
88 my $session = SecurityContext->current;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
89 if($session && $session->authority) {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
90 $session->authority->CloseSession($session);
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
91
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
92 $this->sessions->Delete($session);
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
93 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
94 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
95
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
96 sub CreateSecData {
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
97 my ($this,$package,$params) = @_;
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
98
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
99 die ArgException->new(params => 'A hash reference is required')
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
100 unless ref($params) eq 'HASH';
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
101
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
102 return $package->CreateSecData(%$params);
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
103 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
104
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
105 1;
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
106
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
107 __END__
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
108
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
109 =pod
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
110
73
wizard
parents: 66
diff changeset
111 =head1 NAME
wizard
parents: 66
diff changeset
112
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
113 C<IMPL::Web::Security> Модуль для аутентификации и авторизации веб запроса.
73
wizard
parents: 66
diff changeset
114
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
115 =head1 DESCRIPTION
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
116
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
117 Текущий модуль обеспечивает функции верхнего уровня для работы с системой
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
118 безопасности. Поскольку модуль является абстрактым, конкретные функции
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
119 хранения и реализацию объектов модели безопасности должно обеспечить само
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
120 приложение.
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
121
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
122 Сама система безопасности в веб приложении состоит из двух частей
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
123
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
124 =over
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
125
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
126 =item Модель системы безопасности
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
127
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
128 Предоставляет такие объкты безопасности, как пользователь, сессия роль,
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
129 определяет правила проверки прав доступа субъекта к объекту.
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
130
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
131 =item Модуль безопасности
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
132
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
133 Контекст безопасности создается именно этим модулем.
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
134
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
135 Как правило встраивается в транспортный уровеь в виде обработчика
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
136 C<IMPL::Web::Handler> и реализует непосредственно протокол аутентификации и
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
137 обмена с пользователем.
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
138
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
139 Также модуль безопасности использует модель для хранения сессий и данных
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
140 аутентификции.
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
141
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
142 =back
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
143
73
wizard
parents: 66
diff changeset
144 =head1 MEMBERS
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
145
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
146 =head2 C<AuthUser($name,$package,$challenge)>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
147
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
148 Инициирует создание новой сессии используя провайдера безопасности текущего
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
149 контекста безопасности.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
150
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
151 =over
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
152
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
153 =item C<$name>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
154
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
155 Имя пользователя, которое будет использоваться при поиске его в БД.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
156
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
157 =item C<$package>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
158
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
159 Имя модуля аутентификации, например, C<IMPL::Security::Auth::Simple>.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
160
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
161 =item C<$challenge>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
162
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
163 Данные, полученные от клиента, которые будут переданы модулю аутентификации для
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
164 начала процесса аутентификации и создания сессии.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
165
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
166 =back
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
167
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
168 Функция возвращает хеш с элементами
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
169
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
170 =over
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
171
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
172 =item C<status>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
173
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
174 Статус аутентификации - отражает общее состояние процесса ацтентификации,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
175
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
176 =over
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
177
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
178 =item C<AUTH_FAIL>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
179
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
180 Аутентификация неудачная, сессия не создана.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
181
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
182 =item C<AUTH_INCOMPLETE>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
183
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
184 Аутентификация требует дополнительных шагов, сессия создана, но еще не доверена.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
185
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
186 =item C<AUTH_SUCCESS>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
187
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
188 Аутентификация успешно проведена, сессия создана.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
189
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
190 =back
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
191
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
192 =item C<code>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
193
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
194 =back
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
195
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
196 =cut