|
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\Signer\Hmac\Sha256; |
|
11
|
|
|
use Lcobucci\JWT\Parser; |
|
12
|
|
|
use Lcobucci\JWT\Token as JwtToken; |
|
13
|
|
|
|
|
14
|
|
|
class Processor |
|
15
|
|
|
extends HelperBase |
|
|
|
|
|
|
16
|
|
|
implements ProcessorContract |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
19 |
|
final public function __invoke($tokenString, $isRefresh = false) |
|
23
|
|
|
{ |
|
24
|
19 |
|
$token = (new Parser())->parse((string) $tokenString); |
|
25
|
19 |
|
$signer = new Sha256(); |
|
26
|
19 |
|
$claims = $token->getClaims(); |
|
27
|
|
|
if ( |
|
28
|
19 |
|
!$token->verify($signer, $this->config['secret']) |
|
29
|
19 |
|
|| !$this->checkRequiredClaims(array_keys($claims)) |
|
30
|
18 |
|
|| !$this->validateToken($token, $isRefresh) |
|
31
|
19 |
|
) { |
|
32
|
6 |
|
return false; |
|
33
|
4 |
|
}; |
|
34
|
13 |
|
foreach ($claims as $key => $value) { |
|
35
|
13 |
|
$claims[$key] = $value->getValue(); |
|
36
|
13 |
|
} |
|
37
|
13 |
|
return new Token((string) $token, $claims); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Validate token with validation data |
|
42
|
|
|
* |
|
43
|
|
|
* @param JwtToken $token |
|
44
|
|
|
* @param boolean $isRefresh Is a token refresh happening |
|
45
|
|
|
* @return boolean |
|
46
|
|
|
*/ |
|
47
|
17 |
|
private function validateToken(JwtToken $token, $isRefresh = false) |
|
48
|
|
|
{ |
|
49
|
17 |
|
$data = new ValidationData(); |
|
50
|
17 |
|
if (isset($this->config['issuer'])) { |
|
51
|
17 |
|
$data->setIssuer($this->config['issuer']); |
|
52
|
17 |
|
} |
|
53
|
17 |
|
if (isset($this->config['audience'])) { |
|
54
|
1 |
|
$data->setAudience($this->config['audience']); |
|
55
|
1 |
|
} |
|
56
|
17 |
|
if ($isRefresh) { |
|
57
|
7 |
|
$data->setExpiration(time() - $this->config['refreshOffsetAllowance']); |
|
58
|
7 |
|
} |
|
59
|
17 |
|
if (!$token->validate($data)) { |
|
60
|
4 |
|
return false; |
|
61
|
|
|
} |
|
62
|
13 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|