fix login logic
[GitHub/Stricted/speedport-hybrid-php-api.git] / lib / trait / Login.class.php
CommitLineData
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 */
7trait 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 51 $json = $this->getValues($data['body']);
a2bacc49 52 if (!in_array("challenge", $json)) {
5575d017
S
53 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash);
54 $data = $this->sendRequest($path, $fields);
55 $json = $this->getValues($data['body']);
56 }
57
e9631169
S
58 if (isset($json['login']) && $json['login'] == 'success') {
59 $this->cookie = $this->getCookie($data);
60
61 $this->derivedk = $this->getDerviedk($password);
62
63 // get the csrf_token
64 $this->token = $this->getToken();
65
66 if ($this->checkLogin(false) === true) {
67 return true;
68 }
69 }
70
71 return false;
72 }
73
74 /**
75 * Requests the password-challenge from the router.
76 */
5575d017 77 private function getChallenge_old () {
e9631169
S
78 $path = 'data/Login.json';
79 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
e94573dd 80 $data = $this->sendRequest($path, $fields);
e9631169
S
81 $data = $this->getValues($data['body']);
82
83 if (isset($data['challengev']) && !empty($data['challengev'])) {
84 return $data['challengev'];
85 }
86 else {
87 throw new RouterException('unable to get the challenge from the router');
88 }
89 }
90
5575d017
S
91 /**
92 * Requests the password-challenge from the router.
93 */
94 private function getChallenge () {
a2bacc49 95 $path = 'html/login/index.html';
5575d017 96 $fields = array();
a2bacc49 97 $data = $this->sendRequest($path, $fields);
5575d017
S
98 $a = explode('challenge = "', $data['body']);
99
100 if (!isset($a[1])) {
fad29c09 101 return $this->getChallenge_old();
5575d017
S
102 }
103
104 $a = explode('";', $a[1]);
105
106 if (isset($a[0]) && !empty($a[0])) {
107 return $a[0];
108 }
109 else {
110 throw new RouterException('unable to get the challenge from the router');
111 }
112 }
113
e9631169
S
114 /**
115 * check if we are logged in
116 *
117 * @param boolean $exception
118 * @return boolean
119 */
120 public function checkLogin ($exception = true) {
121 // check if challenge or session is empty
122 if (empty($this->challenge) || empty($this->cookie)) {
123 if ($exception === true) {
124 throw new RouterException('you musst be logged in to use this method');
125 }
126
127 return false;
128 }
129
130 $path = 'data/SecureStatus.json';
131 $fields = array();
e94573dd 132 $data = $this->sendRequest($path, $fields, true);
e9631169
S
133 $data = $this->getValues($data['body']);
134
135 if ($data['loginstate'] != 1) {
136 if ($exception === true) {
137 throw new RouterException('you musst be logged in to use this method');
138 }
139
140 return false;
141 }
142
143 return true;
144 }
145
146 /**
147 * logout
148 *
149 * @return boolean
150 */
151 public function logout () {
152 $this->checkLogin();
153
154 $path = 'data/Login.json';
155 $fields = array('csrf_token' => $this->token, 'logout' => 'byby');
e94573dd 156 $data = $this->sendRequest($path, $fields, true);
e9631169
S
157 $data = $this->getValues($data['body']);
158 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
159 // reset challenge and session
160 $this->challenge = '';
161 $this->cookie = '';
162 $this->token = '';
163 $this->derivedk = '';
164
165 return true;
166 }
167
168 return false;
169 }
170
171 /**
172 * get the csrf_token
173 *
174 * @return string
175 */
176 private function getToken () {
177 $this->checkLogin();
178
179 $path = 'html/content/overview/index.html';
180 $fields = array();
e94573dd 181 $data = $this->sendRequest($path, $fields, true);
e9631169
S
182
183 $a = explode('csrf_token = "', $data['body']);
184 $a = explode('";', $a[1]);
185
186 if (isset($a[0]) && !empty($a[0])) {
187 return $a[0];
188 }
189 else {
190 throw new RouterException('unable to get csrf_token');
191 }
192 }
193
194 /**
195 * calculate the derivedk
196 *
197 * @param string $password
198 * @return string
199 */
200 private function getDerviedk ($password) {
201 $derivedk = '';
202
203 // calculate derivedk
204 if (!function_exists('hash_pbkdf2')) {
205 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
c1827596 206 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 16));
e9631169
S
207 }
208 else {
209 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
210 }
211
212 if (empty($derivedk)) {
213 throw new RouterException('unable to calculate derivedk');
214 }
215
216 return $derivedk;
217 }
218
219 /**
220 * get cookie from header data
221 *
222 * @param array $data
223 * @return string
224 */
225 private function getCookie ($data) {
226 $cookie = '';
227 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
228 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
229 if (isset($match[1]) && !empty($match[1])) {
230 $cookie = $match[1];
231 }
232 }
233
234 if (empty($cookie)) {
235 throw new RouterException('unable to get the session cookie from the router');
236 }
237
238 return $cookie;
239 }
240}