Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
applicationpassword.php
1<?php
9
10use Bitrix\Main;
14
32{
33 use Data\Internal\DeleteByFilterTrait;
34
35 protected const PASSWORD_ALPHABET = "qwertyuiopasdfghjklzxcvbnm";
36 protected const PASSWORD_LENGTH = 16;
37
38 public static function getTableName()
39 {
40 return "b_app_password";
41 }
42
43 public static function getMap()
44 {
45 return array(
46 new Fields\IntegerField('ID', array(
47 'primary' => true,
48 'autocomplete' => true
49 )),
50 new Fields\IntegerField('USER_ID', array(
51 'required' => true,
52 'validation' => '\Bitrix\Main\Authentication\ApplicationPasswordTable::getUserValidators',
53 )),
54 new Fields\StringField('APPLICATION_ID', array(
55 'required' => true,
56 )),
57 new Fields\StringField('PASSWORD', array(
58 'required' => true,
59 )),
60 new Fields\StringField('DIGEST_PASSWORD'),
61 new Fields\DatetimeField('DATE_CREATE'),
62 new Fields\DatetimeField('DATE_LOGIN'),
63 new Fields\StringField('LAST_IP'),
64 new Fields\StringField('COMMENT'),
65 new Fields\StringField('SYSCOMMENT'),
66 new Fields\StringField('CODE'),
67 new Fields\Relations\Reference(
68 'USER',
69 'Bitrix\Main\User',
70 array('=this.USER_ID' => 'ref.ID'),
71 array('join_type' => 'INNER')
72 ),
73 );
74 }
75
76 public static function getUserValidators()
77 {
78 return array(
79 new Fields\Validators\ForeignValidator(Main\UserTable::getEntity()->getField('ID')),
80 );
81 }
82
83 public static function onBeforeAdd(ORM\Event $event)
84 {
85 $result = new ORM\EventResult;
86 $data = $event->getParameter("fields");
87
88 if(isset($data["USER_ID"]) && isset($data['PASSWORD']))
89 {
90 $modified = [
91 'PASSWORD' => Main\Security\Password::hash($data['PASSWORD']),
92 ];
93
94 $user = Main\UserTable::getRowById($data["USER_ID"]);
95 if($user !== null)
96 {
97 $realm = (defined('BX_HTTP_AUTH_REALM')? BX_HTTP_AUTH_REALM : "Bitrix Site Manager");
98 $digest = md5($user["LOGIN"].':'.$realm.':'.$data['PASSWORD']);
99 $modified['DIGEST_PASSWORD'] = $digest;
100 }
101
102 $result->modifyFields($modified);
103 }
104 return $result;
105 }
106
107 public static function onDelete(ORM\Event $event)
108 {
109 $id = $event->getParameter("id");
110
111 $row = static::getRowById($id);
112 if($row)
113 {
114 Main\UserAuthActionTable::addLogoutAction($row["USER_ID"], $row["APPLICATION_ID"]);
115 }
116 }
117
122 public static function generatePassword()
123 {
124 return Main\Security\Random::getStringByCharsets(static::PASSWORD_LENGTH, static::PASSWORD_ALPHABET);
125 }
126
132 public static function isPassword($password)
133 {
134 if (is_string($password))
135 {
136 $password = str_replace(' ', '', $password);
137
138 if(strlen($password) === static::PASSWORD_LENGTH)
139 {
140 return (!preg_match("/[^".static::PASSWORD_ALPHABET."]/", $password));
141 }
142 }
143 return false;
144 }
145
154 public static function findPassword($userId, $password, $passwordOriginal = true)
155 {
156 if($passwordOriginal)
157 {
158 $password = str_replace(' ', '', $password);
159 }
160
161 $appPasswords = static::getList(array(
162 'select' => array('ID', 'PASSWORD', 'APPLICATION_ID'),
163 'filter' => array('=USER_ID' => $userId),
164 ));
165 while(($appPassword = $appPasswords->fetch()))
166 {
167 if(Main\Security\Password::equals($appPassword["PASSWORD"], $password, $passwordOriginal))
168 {
169 //bingo, application password
170 return $appPassword;
171 }
172 }
173 return false;
174 }
175
183 public static function findDigestPassword($userId, array $digest)
184 {
185 $appPasswords = static::getList(array(
186 'select' => array('PASSWORD', 'DIGEST_PASSWORD', 'APPLICATION_ID'),
187 'filter' => array('=USER_ID' => $userId),
188 ));
189
190 $server = Main\Context::getCurrent()->getServer();
191 $method = ($server['REDIRECT_REQUEST_METHOD'] !== null? $server['REDIRECT_REQUEST_METHOD'] : $server['REQUEST_METHOD']);
192 $HA2 = md5($method.':'.$digest['uri']);
193
194 while(($appPassword = $appPasswords->fetch()))
195 {
196 $HA1 = $appPassword["DIGEST_PASSWORD"];
197 $valid_response = md5($HA1.':'.$digest['nonce'].':'.$HA2);
198
199 if($digest["response"] === $valid_response)
200 {
201 //application password
202 return $appPassword;
203 }
204 }
205 return false;
206 }
207}
static findPassword($userId, $password, $passwordOriginal=true)
static equals($hash, $password, $original=true)
Definition password.php:20