Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
token.php
1
<?php
2
3
namespace
Bitrix\Main\Engine\ActionFilter\Service
;
4
5
use
Bitrix\Main\Security
;
6
use
Bitrix\Main\ArgumentOutOfRangeException
;
7
13
class
Token
14
{
15
protected
const
SALT_PREFIX
=
'token_actionfilter'
;
16
protected
const
TTL
= 60 * 60 * 3;
17
18
protected
const
HEADER_ENTITY
=
'X-Bitrix-Sign-Entity'
;
19
protected
const
HEADER_TOKEN
=
'X-Bitrix-Sign-Token'
;
20
22
protected
$signer
;
24
protected
$userId
;
25
26
public
static
function
getEntityHeader
(): string
27
{
28
return
self::HEADER_ENTITY
;
29
}
30
31
public
static
function
getTokenHeader
(): string
32
{
33
return
self::HEADER_TOKEN
;
34
}
35
36
public
function
__construct
(
int
$userId
= 0)
37
{
38
static
$signerInstance =
null
;
39
40
if
(
$userId
<= 0)
41
{
42
throw
new
ArgumentOutOfRangeException
(
'Invalid user ID'
);
43
}
44
45
if
($signerInstance ===
null
)
46
{
47
$signerInstance =
new
Security\Sign\TimeSigner
();
48
}
49
50
$this->signer = $signerInstance;
51
$this->userId =
$userId
;
52
}
53
58
public
function
generate
(
string
$value =
''
): string
59
{
60
return
$this->
getSigner
()->sign($value, (time() + self::TTL), $this->
getSalt
($value));
61
}
62
68
public
function
unsign
(
string
$signedValue =
''
,
string
$payloadEntityValue =
''
): string
69
{
70
return
$this->
getSigner
()->unsign($signedValue, $this->
getSalt
($payloadEntityValue));
71
}
72
73
protected
function
getSigner
(): Security\Sign\TimeSigner
74
{
75
return
$this->signer
;
76
}
77
78
protected
function
getSalt
(
string
$value =
''
): string
79
{
80
return
mb_substr(self::SALT_PREFIX .
'_'
. $this->
getCurrentUserId
() .
'_'
. $value, -50);
81
}
82
83
protected
function
getCurrentUserId
(): int
84
{
85
return
$this->userId
;
86
}
87
}
Bitrix\Main\ArgumentOutOfRangeException
Definition
exception.php:67
Bitrix\Main\Engine\ActionFilter\Service\Token
Definition
token.php:14
Bitrix\Main\Engine\ActionFilter\Service\Token\generate
generate(string $value='')
Definition
token.php:58
Bitrix\Main\Engine\ActionFilter\Service\Token\__construct
__construct(int $userId=0)
Definition
token.php:36
Bitrix\Main\Engine\ActionFilter\Service\Token\getEntityHeader
static getEntityHeader()
Definition
token.php:26
Bitrix\Main\Engine\ActionFilter\Service\Token\getSigner
getSigner()
Definition
token.php:73
Bitrix\Main\Engine\ActionFilter\Service\Token\getSalt
getSalt(string $value='')
Definition
token.php:78
Bitrix\Main\Engine\ActionFilter\Service\Token\TTL
const TTL
Definition
token.php:16
Bitrix\Main\Engine\ActionFilter\Service\Token\getTokenHeader
static getTokenHeader()
Definition
token.php:31
Bitrix\Main\Engine\ActionFilter\Service\Token\HEADER_ENTITY
const HEADER_ENTITY
Definition
token.php:18
Bitrix\Main\Engine\ActionFilter\Service\Token\$userId
$userId
Definition
token.php:24
Bitrix\Main\Engine\ActionFilter\Service\Token\$signer
$signer
Definition
token.php:22
Bitrix\Main\Engine\ActionFilter\Service\Token\SALT_PREFIX
const SALT_PREFIX
Definition
token.php:15
Bitrix\Main\Engine\ActionFilter\Service\Token\getCurrentUserId
getCurrentUserId()
Definition
token.php:83
Bitrix\Main\Engine\ActionFilter\Service\Token\HEADER_TOKEN
const HEADER_TOKEN
Definition
token.php:19
Bitrix\Main\Engine\ActionFilter\Service\Token\unsign
unsign(string $signedValue='', string $payloadEntityValue='')
Definition
token.php:68
Bitrix\Main\Security\Sign\TimeSigner
Definition
timesigner.php:13
Bitrix\Main\Engine\ActionFilter\Service
Definition
token.php:3
Bitrix\Main\Security
Definition
asymmetriccipher.php:8
modules
main
lib
engine
actionfilter
service
token.php
Создано системой
1.10.0