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