Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
token.php
1<?php
2
4
10
11class Token extends Base
12{
13 protected const ERROR_RESTRICTED_BY_SIGN_CHECK = 'restricted_by_sign';
14
17
20
23
24 final public function __construct(\Closure $getEntityClosure)
25 {
26 $this->entityHeaderName = Service\Token::getEntityHeader();
27 $this->tokenHeaderName = Service\Token::getTokenHeader();
28 $this->getEntityClosure = $getEntityClosure;
29
30 parent::__construct();
31 }
32
33 final public function onBeforeAction(Event $event)
34 {
35 $entityValue = (string)Context::getCurrent()->getRequest()->getHeader($this->entityHeaderName);
36 $tokenValue = (string)Context::getCurrent()->getRequest()->getHeader($this->tokenHeaderName);
37
38 if (!$this->check($entityValue, $tokenValue))
39 {
40 Context::getCurrent()->getResponse()->setStatus(403);
41 $this->addError(new Error(
42 'Access restricted by sign check',
43 self::ERROR_RESTRICTED_BY_SIGN_CHECK
44 ));
45
46 return new EventResult(EventResult::ERROR, null, null, $this);
47 }
48
49 return null;
50 }
51
52 protected function check(string $entityValue = '', string $tokenValue = ''): bool
53 {
54 global $USER;
55
56 $result = false;
57 try
58 {
59 $result = ($entityValue === (new Service\Token($USER->getId()))->unsign($tokenValue, ($this->getEntityClosure)()));
60 }
61 catch (\Exception $e)
62 {
63 }
64
65 return $result;
66 }
67}
static getCurrent()
Definition context.php:241
unsign(string $signedValue='', string $payloadEntityValue='')
Definition token.php:68
__construct(\Closure $getEntityClosure)
Definition token.php:24
check(string $entityValue='', string $tokenValue='')
Definition token.php:52