annotate Lib/IMPL/Web/Security.pm @ 391:2287c72f303a

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