|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ncryptf; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
final class Token |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The access token |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
public $accessToken; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The refresh token |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $refreshToken; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The 32 byte initial key material |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $ikm; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The 32 byte signature string |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $signature; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The token expiration time |
|
39
|
|
|
* |
|
40
|
|
|
* @var float |
|
41
|
|
|
*/ |
|
42
|
|
|
public $expiresAt; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $accessToken The access token |
|
48
|
|
|
* @param string $refreshToken The refresh token |
|
49
|
|
|
* @param string $ikm 32 byte initial key material as a byte array |
|
50
|
|
|
* @param string $signature 32 byte signature string as a byte array |
|
51
|
|
|
* @param float $expiresAt The expiration time of the token |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(string $accessToken, string $refreshToken, string $ikm, string $signature, float $expiresAt) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->accessToken = $accessToken; |
|
56
|
|
|
$this->refreshToken = $refreshToken; |
|
57
|
|
|
|
|
58
|
|
|
if (\strlen($ikm) !== 32) { |
|
59
|
|
|
throw new InvalidArgumentException(sprintf("Initial key material should be %d bytes.", 32)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->ikm = $ikm; |
|
63
|
|
|
|
|
64
|
|
|
if (\strlen($signature) !== 64) { |
|
65
|
|
|
throw new InvalidArgumentException(sprintf("Signature secret key should be %d bytes.", 64)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->signature = $signature; |
|
69
|
|
|
$this->expiresAt = $expiresAt; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Extracts the signature public key from the request |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getSignaturePublicKey() |
|
77
|
|
|
{ |
|
78
|
|
|
return \sodium_crypto_sign_publickey_from_secretkey($this->signature); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns true if the token is expired |
|
83
|
|
|
* |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isExpired() : bool |
|
87
|
|
|
{ |
|
88
|
|
|
return \time() > $this->expiresAt; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|