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