annotate Lib/IMPL/Web/Security.pm @ 390:de1f875e8875

added reverse matching lookup to TypeKeyedCollection (find closest descendant)
author cin
date Wed, 12 Feb 2014 18:02:03 +0400
parents 63709a4e6da0
children 2287c72f303a
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
81
077357224bec IMPL::Web::Security alpha version
Sergey
parents: 79
diff changeset
22 sub AuthUser {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
23 my ($this,$name,$package,$challenge) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
24
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
25 my $user = $this->users->GetById($name)
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
26 or return {
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
27 status => AUTH_FAIL,
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
28 code => ERR_NO_SUCH_USER
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
29 };
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
30
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
31 my $auth;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
32 if ( my $secData = $user->GetSecData($package) ) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
33 $auth = $package->new($secData);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
34 } else {
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
35 return {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
36 status => AUTH_FAIL,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
37 code => ERR_NO_SEC_DATA,
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
38 user => $user
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
39 };
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
40 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
41
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
42 return {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
43 status => AUTH_FAIL,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
44 code => ERR_NO_SEC_CONTEXT
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
45 } unless SecurityContext->current;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
46
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
47 return {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
48 status => AUTH_FAIL,
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
49 code => ERR_NO_AUTHORITY
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
50 } unless SecurityContext->current->authority;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
51
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
52 my $status = SecurityContext->current->authority->InitSession(
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
53 $user,
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
54 [$user->roles],
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
55 $auth,
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
56 $challenge
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
57 );
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
58
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
59 return {
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
60 status => $status,
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
61 code => ($status == AUTH_FAIL ? ERR_AUTH_FAIL : 0),
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
62 user => $user
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 194
diff changeset
63 };
73
wizard
parents: 66
diff changeset
64 }
wizard
parents: 66
diff changeset
65
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
66 sub Logout {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
67 my ($this) = @_;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
68
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
69 my $session = SecurityContext->current;
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
70 if($session && $session->authority) {
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
71 $session->authority->CloseSession($session);
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
72
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
73 $this->sessions->Delete($session);
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
74 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
75 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
76
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
77 sub CreateSecData {
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
78 my ($this,$package,$params) = @_;
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
79
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
80 die ArgException->new(params => 'A hash reference is required')
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
81 unless ref($params) eq 'HASH';
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
82
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
83 return $package->CreateSecData(%$params);
239
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
84 }
23daf2fae33a *security subsytem bugfixes
sergey
parents: 233
diff changeset
85
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
86 1;
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
87
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
88 __END__
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
89
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
90 =pod
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
91
73
wizard
parents: 66
diff changeset
92 =head1 NAME
wizard
parents: 66
diff changeset
93
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
94 C<IMPL::Web::Security> Модуль для аутентификации и авторизации веб запроса.
73
wizard
parents: 66
diff changeset
95
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
96 =head1 DESCRIPTION
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
97
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
98 Текущий модуль обеспечивает функции верхнего уровня для работы с системой
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
99 безопасности. Поскольку модуль является абстрактым, конкретные функции
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
100 хранения и реализацию объектов модели безопасности должно обеспечить само
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
101 приложение.
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
102
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
103 Сама система безопасности в веб приложении состоит из двух частей
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
104
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
105 =over
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
106
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
107 =item Модель системы безопасности
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
108
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
109 Предоставляет такие объкты безопасности, как пользователь, сессия роль,
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
110 определяет правила проверки прав доступа субъекта к объекту.
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
111
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
112 =item Модуль безопасности
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
113
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
114 Контекст безопасности создается именно этим модулем.
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
115
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
116 Как правило встраивается в транспортный уровеь в виде обработчика
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
117 C<IMPL::Web::Handler> и реализует непосредственно протокол аутентификации и
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 Также модуль безопасности использует модель для хранения сессий и данных
328
63709a4e6da0 Security framework refactoring
cin
parents: 239
diff changeset
121 аутентификции.
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
122
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 231
diff changeset
123 =back
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
124
73
wizard
parents: 66
diff changeset
125 =head1 MEMBERS
52
15d720913562 security in work
wizard@linux-odin.local
parents:
diff changeset
126
233
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
127 =head2 C<AuthUser($name,$package,$challenge)>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
128
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
129 Инициирует создание новой сессии используя провайдера безопасности текущего
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
130 контекста безопасности.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
131
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
132 =over
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
133
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
134 =item C<$name>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
135
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
136 Имя пользователя, которое будет использоваться при поиске его в БД.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
137
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
138 =item C<$package>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
139
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
140 Имя модуля аутентификации, например, C<IMPL::Security::Auth::Simple>.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
141
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
142 =item C<$challenge>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
143
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
144 Данные, полученные от клиента, которые будут переданы модулю аутентификации для
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
145 начала процесса аутентификации и создания сессии.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
146
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
147 =back
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<status>
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 =over
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
158
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
159 =item C<AUTH_FAIL>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
160
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
161 Аутентификация неудачная, сессия не создана.
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
162
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
163 =item C<AUTH_INCOMPLETE>
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
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
167 =item C<AUTH_SUCCESS>
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
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
171 =back
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
172
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
173 =item C<code>
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
174
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
175 =back
3cebcf6fdb9b refactoring, cleaning code
sergey
parents: 232
diff changeset
176
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
177 =cut