Commit | Line | Data |
---|---|---|
e9631169 S |
1 | <?php |
2 | /** | |
3 | * @author Jan Altensen (Stricted) | |
4 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
dd76f288 | 5 | * @copyright 2015-2016 Jan Altensen (Stricted) |
e9631169 S |
6 | */ |
7 | trait Login { | |
8 | /** | |
9 | * password-challenge | |
10 | * @var string | |
11 | */ | |
12 | private $challenge = ''; | |
13 | ||
14 | /** | |
15 | * csrf_token | |
16 | * @var string | |
17 | */ | |
18 | private $token = ''; | |
19 | ||
20 | /** | |
21 | * hashed password | |
22 | * @var string | |
23 | */ | |
24 | private $hash = ''; | |
25 | ||
26 | /** | |
27 | * session cookie | |
28 | * @var string | |
29 | */ | |
30 | private $cookie = ''; | |
31 | ||
32 | /** | |
33 | * derivedk cookie | |
34 | * @var string | |
35 | */ | |
36 | private $derivedk = ''; | |
37 | ||
38 | /** | |
39 | * login into the router with the given password | |
40 | * | |
41 | * @param string $password | |
42 | * @return boolean | |
43 | */ | |
44 | public function login ($password) { | |
45 | $this->challenge = $this->getChallenge(); | |
46 | ||
47 | $path = 'data/Login.json'; | |
48 | $this->hash = hash('sha256', $this->challenge.':'.$password); | |
5575d017 | 49 | $fields = array('csrf_token' => 'nulltoken', "challengev" => $this->challenge, 'showpw' => 0, 'password' => $this->hash); |
e94573dd | 50 | $data = $this->sendRequest($path, $fields); |
e9631169 S |
51 | $json = $this->getValues($data['body']); |
52 | ||
5575d017 S |
53 | if (in_array("challenge", $json)) { |
54 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash); | |
55 | $data = $this->sendRequest($path, $fields); | |
56 | $json = $this->getValues($data['body']); | |
57 | } | |
58 | ||
e9631169 S |
59 | if (isset($json['login']) && $json['login'] == 'success') { |
60 | $this->cookie = $this->getCookie($data); | |
61 | ||
62 | $this->derivedk = $this->getDerviedk($password); | |
63 | ||
64 | // get the csrf_token | |
65 | $this->token = $this->getToken(); | |
66 | ||
67 | if ($this->checkLogin(false) === true) { | |
68 | return true; | |
69 | } | |
70 | } | |
71 | ||
72 | return false; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Requests the password-challenge from the router. | |
77 | */ | |
5575d017 | 78 | private function getChallenge_old () { |
e9631169 S |
79 | $path = 'data/Login.json'; |
80 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null'); | |
e94573dd | 81 | $data = $this->sendRequest($path, $fields); |
e9631169 S |
82 | $data = $this->getValues($data['body']); |
83 | ||
84 | if (isset($data['challengev']) && !empty($data['challengev'])) { | |
85 | return $data['challengev']; | |
86 | } | |
87 | else { | |
88 | throw new RouterException('unable to get the challenge from the router'); | |
89 | } | |
90 | } | |
91 | ||
5575d017 S |
92 | /** |
93 | * Requests the password-challenge from the router. | |
94 | */ | |
95 | private function getChallenge () { | |
96 | $path = 'html/content/overview/index.html'; | |
97 | $fields = array(); | |
98 | $data = $this->sendRequest($path, $fields, true); | |
99 | ||
100 | $a = explode('challenge = "', $data['body']); | |
101 | ||
102 | if (!isset($a[1])) { | |
103 | return getChallenge_old(); | |
104 | } | |
105 | ||
106 | $a = explode('";', $a[1]); | |
107 | ||
108 | if (isset($a[0]) && !empty($a[0])) { | |
109 | return $a[0]; | |
110 | } | |
111 | else { | |
112 | throw new RouterException('unable to get the challenge from the router'); | |
113 | } | |
114 | } | |
115 | ||
e9631169 S |
116 | /** |
117 | * check if we are logged in | |
118 | * | |
119 | * @param boolean $exception | |
120 | * @return boolean | |
121 | */ | |
122 | public function checkLogin ($exception = true) { | |
123 | // check if challenge or session is empty | |
124 | if (empty($this->challenge) || empty($this->cookie)) { | |
125 | if ($exception === true) { | |
126 | throw new RouterException('you musst be logged in to use this method'); | |
127 | } | |
128 | ||
129 | return false; | |
130 | } | |
131 | ||
132 | $path = 'data/SecureStatus.json'; | |
133 | $fields = array(); | |
e94573dd | 134 | $data = $this->sendRequest($path, $fields, true); |
e9631169 S |
135 | $data = $this->getValues($data['body']); |
136 | ||
137 | if ($data['loginstate'] != 1) { | |
138 | if ($exception === true) { | |
139 | throw new RouterException('you musst be logged in to use this method'); | |
140 | } | |
141 | ||
142 | return false; | |
143 | } | |
144 | ||
145 | return true; | |
146 | } | |
147 | ||
148 | /** | |
149 | * logout | |
150 | * | |
151 | * @return boolean | |
152 | */ | |
153 | public function logout () { | |
154 | $this->checkLogin(); | |
155 | ||
156 | $path = 'data/Login.json'; | |
157 | $fields = array('csrf_token' => $this->token, 'logout' => 'byby'); | |
e94573dd | 158 | $data = $this->sendRequest($path, $fields, true); |
e9631169 S |
159 | $data = $this->getValues($data['body']); |
160 | if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) { | |
161 | // reset challenge and session | |
162 | $this->challenge = ''; | |
163 | $this->cookie = ''; | |
164 | $this->token = ''; | |
165 | $this->derivedk = ''; | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | return false; | |
171 | } | |
172 | ||
173 | /** | |
174 | * get the csrf_token | |
175 | * | |
176 | * @return string | |
177 | */ | |
178 | private function getToken () { | |
179 | $this->checkLogin(); | |
180 | ||
181 | $path = 'html/content/overview/index.html'; | |
182 | $fields = array(); | |
e94573dd | 183 | $data = $this->sendRequest($path, $fields, true); |
e9631169 S |
184 | |
185 | $a = explode('csrf_token = "', $data['body']); | |
186 | $a = explode('";', $a[1]); | |
187 | ||
188 | if (isset($a[0]) && !empty($a[0])) { | |
189 | return $a[0]; | |
190 | } | |
191 | else { | |
192 | throw new RouterException('unable to get csrf_token'); | |
193 | } | |
194 | } | |
195 | ||
196 | /** | |
197 | * calculate the derivedk | |
198 | * | |
199 | * @param string $password | |
200 | * @return string | |
201 | */ | |
202 | private function getDerviedk ($password) { | |
203 | $derivedk = ''; | |
204 | ||
205 | // calculate derivedk | |
206 | if (!function_exists('hash_pbkdf2')) { | |
207 | $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1')); | |
c1827596 | 208 | $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 16)); |
e9631169 S |
209 | } |
210 | else { | |
211 | $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32); | |
212 | } | |
213 | ||
214 | if (empty($derivedk)) { | |
215 | throw new RouterException('unable to calculate derivedk'); | |
216 | } | |
217 | ||
218 | return $derivedk; | |
219 | } | |
220 | ||
221 | /** | |
222 | * get cookie from header data | |
223 | * | |
224 | * @param array $data | |
225 | * @return string | |
226 | */ | |
227 | private function getCookie ($data) { | |
228 | $cookie = ''; | |
229 | if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) { | |
230 | preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match); | |
231 | if (isset($match[1]) && !empty($match[1])) { | |
232 | $cookie = $match[1]; | |
233 | } | |
234 | } | |
235 | ||
236 | if (empty($cookie)) { | |
237 | throw new RouterException('unable to get the session cookie from the router'); | |
238 | } | |
239 | ||
240 | return $cookie; | |
241 | } | |
242 | } |