-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
AccessToken.php
110 lines (97 loc) · 2.65 KB
/
AccessToken.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Bitrix24\SDK\Core\Credentials;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation;
/**
* Class AccessToken
*
* @package Bitrix24\SDK\Core\Credentials
*/
class AccessToken
{
protected string $accessToken;
protected string $refreshToken;
protected int $expires;
/**
* AccessToken constructor.
*
* @param string $accessToken
* @param string $refreshToken
* @param int $expires
*/
public function __construct(string $accessToken, string $refreshToken, int $expires)
{
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
$this->expires = $expires;
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}
/**
* @return string
*/
public function getRefreshToken(): string
{
return $this->refreshToken;
}
/**
* @return int
*/
public function getExpires(): int
{
return $this->expires;
}
/**
* @return bool
*/
public function hasExpired(): bool
{
return $this->getExpires() <= time();
}
/**
* @param array $request
*
* @return self
*/
public static function initFromArray(array $request): self
{
return new self(
(string)$request['access_token'],
(string)$request['refresh_token'],
(int)$request['expires']
);
}
public static function initFromRobotRequest(Request $request): self
{
$requestFields = $request->request->all();
return self::initFromArray($requestFields['auth']);
}
/**
* @throws InvalidArgumentException
*/
public static function initFromPlacementRequest(HttpFoundation\Request $request): self
{
$requestFields = $request->request->all();
if (!array_key_exists('AUTH_ID', $requestFields)) {
throw new InvalidArgumentException('field AUTH_ID not fount in request');
}
if (!array_key_exists('REFRESH_ID', $requestFields)) {
throw new InvalidArgumentException('field REFRESH_ID not fount in request');
}
if (!array_key_exists('AUTH_EXPIRES', $requestFields)) {
throw new InvalidArgumentException('field AUTH_EXPIRES not fount in request');
}
return new self(
(string)$request->request->get('AUTH_ID'),
(string)$request->request->get('REFRESH_ID'),
$request->request->getInt('AUTH_EXPIRES'),
);
}
}