Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
token.php
1<?php
2
4
7
13class 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 {
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}
unsign(string $signedValue='', string $payloadEntityValue='')
Definition token.php:68