Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
applicationpassword.php
1
<?php
8
namespace
Bitrix\Main\Authentication
;
9
10
use
Bitrix\Main
;
11
use
Bitrix\Main\ORM
;
12
use
Bitrix\Main\ORM\Data
;
13
use
Bitrix\Main\ORM\Fields
;
14
31
class
ApplicationPasswordTable
extends
Data\DataManager
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
}
Bitrix\Main\Authentication\ApplicationPasswordTable
Definition
applicationpassword.php:32
Bitrix\Main\Authentication\ApplicationPasswordTable\isPassword
static isPassword($password)
Definition
applicationpassword.php:132
Bitrix\Main\Authentication\ApplicationPasswordTable\getUserValidators
static getUserValidators()
Definition
applicationpassword.php:76
Bitrix\Main\Authentication\ApplicationPasswordTable\onDelete
static onDelete(ORM\Event $event)
Definition
applicationpassword.php:107
Bitrix\Main\Authentication\ApplicationPasswordTable\findPassword
static findPassword($userId, $password, $passwordOriginal=true)
Definition
applicationpassword.php:154
Bitrix\Main\Authentication\ApplicationPasswordTable\getMap
static getMap()
Definition
applicationpassword.php:43
Bitrix\Main\Authentication\ApplicationPasswordTable\onBeforeAdd
static onBeforeAdd(ORM\Event $event)
Definition
applicationpassword.php:83
Bitrix\Main\Authentication\ApplicationPasswordTable\findDigestPassword
static findDigestPassword($userId, array $digest)
Definition
applicationpassword.php:183
Bitrix\Main\Authentication\ApplicationPasswordTable\generatePassword
static generatePassword()
Definition
applicationpassword.php:122
Bitrix\Main\Authentication\ApplicationPasswordTable\PASSWORD_ALPHABET
const PASSWORD_ALPHABET
Definition
applicationpassword.php:35
Bitrix\Main\Authentication\ApplicationPasswordTable\PASSWORD_LENGTH
const PASSWORD_LENGTH
Definition
applicationpassword.php:36
Bitrix\Main\Authentication\ApplicationPasswordTable\getTableName
static getTableName()
Definition
applicationpassword.php:38
Bitrix\Main\Event
Definition
event.php:5
Bitrix\Main\EventResult
Definition
eventresult.php:5
Bitrix\Main\ORM\Data\DataManager
Definition
datamanager.php:33
Bitrix\Main\ORM\Data\DataManager\getEntity
static getEntity()
Definition
datamanager.php:78
Bitrix\Main\ORM\Fields\DatetimeField
Definition
datetimefield.php:22
Bitrix\Main\ORM\Fields\IntegerField
Definition
integerfield.php:20
Bitrix\Main\ORM\Fields\Relations\Reference
Definition
reference.php:26
Bitrix\Main\ORM\Fields\StringField
Definition
stringfield.php:20
Bitrix\Main\Security\Password\equals
static equals($hash, $password, $original=true)
Definition
password.php:20
Bitrix\Main\Authentication
Definition
application.php:9
Bitrix\Main\ORM\Data
Definition
addresult.php:9
Bitrix\Main\ORM\Fields
Definition
arrayfield.php:9
Bitrix\Main\ORM
Bitrix\Main
modules
main
lib
authentication
applicationpassword.php
Создано системой
1.10.0