|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Canis.io |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Canis\Lumen\Jwt\Adapters\Lcobucci; |
|
7
|
|
|
|
|
8
|
|
|
use Canis\Lumen\Jwt\Token; |
|
9
|
|
|
use Canis\Lumen\Jwt\Contracts\Processor as ProcessorContract; |
|
10
|
|
|
use Lcobucci\JWT\ValidationData; |
|
11
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
|
12
|
|
|
use Lcobucci\JWT\Parser; |
|
13
|
|
|
use Lcobucci\JWT\Token as JwtToken; |
|
14
|
|
|
|
|
15
|
|
|
class Processor |
|
16
|
|
|
extends HelperBase |
|
|
|
|
|
|
17
|
|
|
implements ProcessorContract |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
16 |
|
final public function __invoke($tokenString, $validateClaims = []) |
|
23
|
|
|
{ |
|
24
|
16 |
|
$token = (new Parser())->parse((string) $tokenString); |
|
25
|
16 |
|
$signer = new Sha256(); |
|
26
|
16 |
|
$claims = $token->getClaims(); |
|
27
|
|
|
if ( |
|
28
|
16 |
|
!$token->verify($signer, $this->config['secret']) |
|
29
|
16 |
|
|| !$this->checkRequiredClaims(array_keys($claims)) |
|
30
|
15 |
|
|| !$this->validateToken($token) |
|
31
|
16 |
|
) { |
|
32
|
4 |
|
return false; |
|
33
|
4 |
|
}; |
|
34
|
12 |
|
foreach ($claims as $key => $value) { |
|
35
|
12 |
|
$claims[$key] = $value->getValue(); |
|
36
|
12 |
|
} |
|
37
|
12 |
|
if (!$this->validateClaims($claims, $validateClaims)) { |
|
38
|
3 |
|
return false; |
|
39
|
|
|
} |
|
40
|
9 |
|
return new Token((string) $token, $claims); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Validate token with validation data |
|
45
|
|
|
* |
|
46
|
|
|
* @param JwtToken $token |
|
47
|
|
|
* @return boolean |
|
48
|
|
|
*/ |
|
49
|
14 |
|
private function validateToken(JwtToken $token) |
|
50
|
|
|
{ |
|
51
|
14 |
|
$data = new ValidationData(); |
|
52
|
14 |
|
if (isset($this->config['issuer'])) { |
|
53
|
14 |
|
$data->setIssuer($this->config['issuer']); |
|
54
|
14 |
|
} |
|
55
|
14 |
|
if (isset($this->config['audience'])) { |
|
56
|
1 |
|
$data->setAudience($this->config['audience']); |
|
57
|
1 |
|
} |
|
58
|
14 |
|
if (!$token->validate($data)) { |
|
59
|
2 |
|
return false; |
|
60
|
|
|
} |
|
61
|
12 |
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Validate the claims of the token |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $claims |
|
68
|
|
|
* @param array $validateClaims |
|
69
|
|
|
* @return boolean |
|
70
|
|
|
*/ |
|
71
|
12 |
|
private function validateClaims($claims, $validateClaims) |
|
72
|
|
|
{ |
|
73
|
12 |
|
foreach ($validateClaims as $claim => $value) { |
|
74
|
9 |
|
if (!isset($claims[$claim]) || $claims[$claim] !== $value) { |
|
75
|
3 |
|
return false; |
|
76
|
|
|
} |
|
77
|
9 |
|
} |
|
78
|
9 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|