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 Thruway\Authentication\AbstractAuthProviderClient; |
15
|
|
|
use Thruway\Message\HelloMessage; |
16
|
|
|
use Thruway\Message\Message; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class WampCraAuthProvider |
20
|
|
|
* |
21
|
|
|
* @package Thruway\Authentication |
22
|
|
|
*/ |
23
|
|
|
class WampCraAuthProvider extends AbstractAuthProviderClient |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function getMethodName() |
29
|
|
|
{ |
30
|
|
|
return 'wampcra'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The arguments given by the server are the actual hello message ($args[0]) |
35
|
|
|
* and some session information ($args[1]) |
36
|
|
|
* |
37
|
|
|
* The session information is an associative array that contains the sessionId and realm |
38
|
|
|
* |
39
|
|
|
* @param array $args |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function processHello(array $args) |
43
|
|
|
{ |
44
|
|
|
debug($args); |
45
|
|
|
$helloMsg = array_shift($args); |
46
|
|
|
$sessionInfo = array_shift($args); |
47
|
|
|
|
48
|
|
|
if (!is_array($helloMsg)) { |
49
|
|
|
return ["ERROR"]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!is_object($sessionInfo)) { |
53
|
|
|
return ["ERROR"]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$helloMsg = Message::createMessageFromArray($helloMsg); |
57
|
|
|
|
58
|
|
|
if (!$helloMsg instanceof HelloMessage |
59
|
|
|
|| !$sessionInfo |
60
|
|
|
|| !isset($helloMsg->getDetails()->authid) |
61
|
|
|
|| !$this->getUserDb() instanceof WampCraUserDbInterface |
|
|
|
|
62
|
|
|
) { |
63
|
|
|
return ["ERROR"]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$authid = $helloMsg->getDetails()->authid; |
67
|
|
|
$user = $this->getUserDb()->get($authid); |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (!$user) { |
70
|
|
|
return ["FAILURE"]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// create a challenge |
74
|
|
|
$nonce = bin2hex(mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)); |
75
|
|
|
$authRole = "user"; |
76
|
|
|
$authMethod = "wampcra"; |
77
|
|
|
$authProvider = "userdb"; |
78
|
|
|
$now = new \DateTime(); |
79
|
|
|
$timeStamp = $now->format($now::ISO8601); |
80
|
|
|
if (!isset($sessionInfo->sessionId)) { |
81
|
|
|
return ["ERROR"]; |
82
|
|
|
} |
83
|
|
|
$sessionId = $sessionInfo->sessionId; |
84
|
|
|
|
85
|
|
|
$challenge = [ |
86
|
|
|
"authid" => $authid, |
87
|
|
|
"authrole" => $authRole, |
88
|
|
|
"authprovider" => $authProvider, |
89
|
|
|
"authmethod" => $authMethod, |
90
|
|
|
"nonce" => $nonce, |
91
|
|
|
"timestamp" => $timeStamp, |
92
|
|
|
"session" => $sessionId |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$serializedChallenge = json_encode($challenge); |
96
|
|
|
|
97
|
|
|
$challengeDetails = [ |
98
|
|
|
"challenge" => $serializedChallenge, |
99
|
|
|
"challenge_method" => $this->getMethodName() |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if ($user['salt'] !== null) { |
103
|
|
|
// we are using salty password |
104
|
|
|
$saltInfo = [ |
105
|
|
|
"salt" => $user['salt'], |
106
|
|
|
"keylen" => 32, |
107
|
|
|
"iterations" => 1000 |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$challengeDetails = array_merge($challengeDetails, $saltInfo); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return ["CHALLENGE", (object)$challengeDetails]; |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Process authenticate |
119
|
|
|
* |
120
|
|
|
* @param mixed $signature |
121
|
|
|
* @param mixed $extra |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function processAuthenticate($signature, $extra = null) |
125
|
|
|
{ |
126
|
|
|
debug([$signature, $extra]); |
127
|
|
|
$challenge = $this->getChallengeFromExtra($extra); |
|
|
|
|
128
|
|
|
|
129
|
|
|
if (!$challenge |
130
|
|
|
|| !isset($challenge->authid) |
131
|
|
|
|| !$this->getUserDb() instanceof WampCraUserDbInterface |
|
|
|
|
132
|
|
|
) { |
133
|
|
|
return ["FAILURE"]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$authid = $challenge->authid; |
137
|
|
|
$user = $this->getUserDb()->get($authid); |
|
|
|
|
138
|
|
|
|
139
|
|
|
if (!$user) { |
140
|
|
|
return ["FAILURE"]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$keyToUse = $user['key']; |
144
|
|
|
$token = base64_encode(hash_hmac('sha256', json_encode($challenge), $keyToUse, true)); |
145
|
|
|
|
146
|
|
|
if ($token != $signature) { |
147
|
|
|
return ["FAILURE"]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$authDetails = [ |
151
|
|
|
"authmethod" => "wampcra", |
152
|
|
|
"authrole" => "user", |
153
|
|
|
"authid" => $challenge->authid, |
154
|
|
|
"authprovider" => $challenge->authprovider |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
return ["SUCCESS", $authDetails]; |
|
|
|
|
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.