-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Validate.php
75 lines (64 loc) · 2.23 KB
/
Validate.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
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Easy;
use function count;
use Exception;
use Jose\Component\Checker;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\Util\JsonConverter;
use Jose\Component\Signature\Algorithm;
use Jose\Component\Signature\JWSTokenSupport;
use Jose\Component\Signature\JWSVerifier;
use Jose\Component\Signature\Serializer\CompactSerializer;
class Validate extends AbstractLoader
{
public static function token(string $token): self
{
return new self($token);
}
public function run(): JWT
{
if (0 !== count($this->allowedAlgorithms)) {
$this->headerCheckers[] = new Checker\AlgorithmChecker($this->allowedAlgorithms, true);
}
$jws = (new CompactSerializer())->unserialize($this->token);
$headerChecker = new Checker\HeaderCheckerManager($this->headerCheckers, [new JWSTokenSupport()]);
$headerChecker->check($jws, 0);
$verifier = new JWSVerifier(new AlgorithmManager($this->algorithms));
if (!$verifier->verifyWithKeySet($jws, $this->jwkset, 0)) {
throw new Exception('Invalid signature');
}
$jwt = new JWT();
$jwt->header->replace($jws->getSignature(0)->getProtectedHeader());
$jwt->claims->replace(JsonConverter::decode($jws->getPayload()));
$claimChecker = new Checker\ClaimCheckerManager($this->claimCheckers);
$claimChecker->check($jwt->claims->all(), $this->mandatoryClaims);
return $jwt;
}
protected function getAlgorithmMap(): array
{
return [
Algorithm\HS256::class,
Algorithm\HS384::class,
Algorithm\HS512::class,
Algorithm\RS256::class,
Algorithm\RS384::class,
Algorithm\RS512::class,
Algorithm\PS256::class,
Algorithm\PS384::class,
Algorithm\PS512::class,
Algorithm\ES256::class,
Algorithm\ES384::class,
Algorithm\ES512::class,
Algorithm\EdDSA::class,
];
}
}