Total Complexity | 7 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
14 | final class Parser implements TokenParser |
||
15 | { |
||
16 | /** |
||
17 | * @var JwtConfiguration |
||
18 | */ |
||
19 | private $config; |
||
20 | |||
21 | /** |
||
22 | * @var Constraint[] |
||
23 | */ |
||
24 | private $constraints; |
||
25 | |||
26 | public function __construct(JwtConfiguration $config, Clock $clock, array $issuers = null, string $audience = null) |
||
27 | { |
||
28 | $this->config = $config; |
||
29 | |||
30 | $this |
||
31 | ->addConstraint(new Constraint\ValidAt($clock)) |
||
32 | ->addConstraint(new Constraint\SignedWith($this->config->getSigner(), $this->config->getVerificationKey())) |
||
33 | ; |
||
34 | |||
35 | if ($issuers) { |
||
36 | $this->addConstraint(new Constraint\IssuedBy(...$issuers)); |
||
37 | } |
||
38 | |||
39 | if ($audience) { |
||
40 | $this->addConstraint(new Constraint\PermittedFor($audience)); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public function isValid(string $jwt): bool |
||
47 | } |
||
48 | |||
49 | public function parse(string $jwt): Token |
||
50 | { |
||
51 | return Token::fromClaims($this->parseJwt($jwt)->claims()->all()); |
||
52 | } |
||
53 | |||
54 | private function addConstraint(Constraint $constraint): self |
||
55 | { |
||
56 | $this->constraints[] = $constraint; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | private function parseJwt(string $jwt): JwtToken |
||
64 | } |
||
65 | } |
||
66 |