|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\Paseto; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use ParagonIE\Paseto\Builder; |
|
8
|
|
|
use ParagonIE\Paseto\JsonToken; |
|
9
|
|
|
use ParagonIE\Paseto\Keys\SymmetricKey; |
|
10
|
|
|
use ParagonIE\Paseto\Parser; |
|
11
|
|
|
use ParagonIE\Paseto\Protocol\Version2; |
|
12
|
|
|
use ParagonIE\Paseto\ProtocolCollection; |
|
13
|
|
|
use ParagonIE\Paseto\Purpose; |
|
14
|
|
|
use ParagonIE\Paseto\Rules\IssuedBy; |
|
15
|
|
|
use ParagonIE\Paseto\Rules\NotExpired; |
|
16
|
|
|
|
|
17
|
|
|
class PasetoService |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string $sharedKey */ |
|
20
|
|
|
private $sharedKey; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* PasetoService constructor. |
|
24
|
|
|
* @param SymmetricKey $sharedKey |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct(SymmetricKey $sharedKey) |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->sharedKey = $sharedKey; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* @param string $duration |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws \ParagonIE\Paseto\Exception\InvalidKeyException |
|
36
|
|
|
* @throws \ParagonIE\Paseto\Exception\InvalidPurposeException |
|
37
|
|
|
* @throws \ParagonIE\Paseto\Exception\PasetoException |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function encryptToken(array $data, string $duration = 'P01D') |
|
40
|
|
|
{ |
|
41
|
1 |
|
$token = (new Builder()) |
|
42
|
1 |
|
->setKey($this->sharedKey) |
|
|
|
|
|
|
43
|
1 |
|
->setVersion(new Version2()) |
|
44
|
1 |
|
->setPurpose(Purpose::local()) |
|
45
|
1 |
|
->setExpiration((new DateTime())->add(new DateInterval($duration))) |
|
46
|
1 |
|
->setClaims($data); |
|
47
|
|
|
|
|
48
|
1 |
|
return $token->toString(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $providedToken |
|
53
|
|
|
* @return JsonToken |
|
54
|
|
|
* @throws \ParagonIE\Paseto\Exception\InvalidPurposeException |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function decryptToken($providedToken): JsonToken |
|
57
|
|
|
{ |
|
58
|
1 |
|
$parser = (new Parser()) |
|
59
|
1 |
|
->setKey($this->sharedKey) |
|
|
|
|
|
|
60
|
1 |
|
->addRule(new NotExpired()) |
|
61
|
1 |
|
->setPurpose(Purpose::local()) |
|
62
|
1 |
|
->setAllowedVersions(ProtocolCollection::v2()); |
|
63
|
1 |
|
$token = $parser->parse($providedToken); |
|
64
|
|
|
|
|
65
|
1 |
|
return $token; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..