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