annotate lib/IMPL/Security/Auth/Simple.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Security::Auth::Simple;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Digest::MD5 qw(md5_hex);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use Encode qw(encode);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Security::Auth qw(:Const);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 WrongDataException => '-IMPL::WrongDataException'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 'IMPL::Security::Auth' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 _stage => PROP_ALL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 _salt => PROP_ALL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 _image => PROP_ALL
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 use constant {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 STAGE_INIT => 1,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 STAGE_DONE => 2
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ($this,$secData) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my ($stage,$salt,$img) = split /\|/,$secData;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 die WrongDataException->new() unless grep $_ == $stage, (STAGE_INIT, STAGE_DONE);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 $this->_stage($stage);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 $this->_salt($salt);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 $this->_image($img);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 sub secData {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 return join ('|',$this->_stage, $this->_salt , $this->_image );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 sub isTrusted {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $this->_stage == STAGE_DONE ? 1 : 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 sub DoAuth {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my ($this,$challenge) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 my $salt = $this->_salt;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 if (md5_hex($salt,encode('utf-8', $challenge), $salt) eq $this->_image) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 if ($this->_stage == STAGE_INIT) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 $this->_stage(STAGE_DONE);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 return (AUTH_SUCCESS, undef);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 return (AUTH_FAIL, undef);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 sub CreateSecData {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 my ($self,%args) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 die new IMPL::InvalidArgumentException("The parameter is required",'password') unless $args{password};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 my $salt = $self->GenSSID();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 return return join ('|',STAGE_INIT, $salt, md5_hex($salt,encode('utf-8', $args{password}),$salt));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 sub SecDataArgs {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 password => 'SCALAR'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 C<IMPL::Security::Auth::Simple> Модуль простой авторизации.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 Использует алгоритм MD5 для хранения образа пароля.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 =head2 C<CTOR($secData)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 Создает объект аутентификации, передавая ему данные для инициализации.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 =head2 C<[get]secData>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 Возвращает данные безопасности, которые можно использовать для восстановления
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 состояния объекта.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 =head2 C<[get]isTrusted>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 Является ли объект доверенным для аутентификации сессии (тоесть хранит данные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 для аутентификации сессии).
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 =head2 C<DoAuth($challenge)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 Аутентифицирует пользователя. Используется один этап. C<$challenge>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 открытый пароль пользователя или cookie сессии.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 Возвращает C<($status,$challenge)>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 =item C<$status>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 Результат либо C<AUTH_SUCCESS>, либо C<AUTH_FAIL>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 =item C<$challenge>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 В случае успеха возвращает cookie (уникальный номер) сессии, либо C<undef>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =cut