|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Ratchet. |
|
5
|
|
|
* |
|
6
|
|
|
** (c) 2016 Cees-Jan Kiewiet |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WyriHaximus\Ratchet\Security; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Core\Configure; |
|
15
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
|
16
|
|
|
use React\EventLoop\LoopInterface; |
|
17
|
|
|
use React\Promise\PromiseInterface; |
|
18
|
|
|
use Thruway\Authentication\AbstractAuthProviderClient; |
|
19
|
|
|
use function React\Promise\resolve; |
|
20
|
|
|
use Lcobucci\JWT\Parser; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class WampCraAuthProvider |
|
24
|
|
|
* |
|
25
|
|
|
* @package Thruway\Authentication |
|
26
|
|
|
*/ |
|
27
|
|
|
final class JWTAuthProvider extends AbstractAuthProviderClient |
|
28
|
|
|
{ |
|
29
|
|
|
private $activeRealms = []; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(array $authRealms, LoopInterface $loop = null) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($authRealms, $loop); |
|
34
|
|
|
|
|
35
|
|
|
$realmSalt = Configure::read('WyriHaximus.Ratchet.realm_salt'); |
|
36
|
|
|
$authKeySalt = Configure::read('WyriHaximus.Ratchet.realm_auth_key_salt'); |
|
37
|
|
|
foreach (Configure::read('WyriHaximus.Ratchet.realms') as $realm => $options) { |
|
38
|
|
|
if (!isset($options['auth'])) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!$options['auth']) { |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (!isset($options['auth_key'])) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (empty($options['auth_key'])) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$hash = hash('sha512', $realmSalt . $realm . $realmSalt); |
|
55
|
|
|
$this->activeRealms[$hash] = $authKeySalt . $options['auth_key'] . $authKeySalt; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getMethodName() |
|
63
|
|
|
{ |
|
64
|
|
|
return 'jwt'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Process authenticate |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $signature |
|
71
|
|
|
* @param mixed $extra |
|
72
|
|
|
* @return PromiseInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function processAuthenticate($signature, $extra = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->getRealm(); |
|
|
|
|
|
|
77
|
|
|
$token = (new Parser())->parse($signature); |
|
78
|
|
|
|
|
79
|
|
|
$iss = $token->getClaim('iss'); |
|
80
|
|
|
$iss = base64_decode($iss); |
|
81
|
|
|
if (!isset($this->activeRealms[$iss])) { |
|
82
|
|
|
return resolve(["FAILURE"]); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($token->verify(new Sha256(), $this->activeRealms[$iss])) { |
|
86
|
|
|
return resolve(["SUCCESS", ['authid' => $token->getClaim('authid')]]); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return resolve(["FAILURE"]); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: