function before($funcion) { if ($funcion != "authenticate") { try { $secretKey = base64_decode(Config::Reader("SALT")); $header = apache_request_headers(); $header["Authorization"] = trim(str_replace("Bearer", "", $header["Authorization"])); $jwt = JWT::decode($header["Authorization"], $secretKey, array('HS512')); $this->UserToken = $jwt; } catch (ExpiredException $e) { $this->Response["error"] = $e->getMessage(); return false; } } return true; }
function authenticate() { $usuario = $this->Usuarios->findByUsuario($this->Post["usuario"]); if (!empty($usuario)) { if (password_verify($this->Post["password"], $usuario["pass"])) { unset($usuario["pass"]); $secretKey = base64_decode(Config::Reader("SALT")); $data = ['iss' => SITE, 'aud' => SITE, 'iat' => time(), 'exp' => time() + 60 * 60 * 24 * 7, 'sub' => 'usuario', 'admin' => true, 'data' => $usuario]; $jwt = JWT::encode($data, $secretKey, 'HS512'); $this->Response = ['token' => $jwt]; } else { $this->Response = ["error" => "La contraseña o usuario incorrecto"]; } } else { $this->Response = ["error" => "El usuario no existe"]; } }
public static function jsonEncode($input) { $json = json_encode($input); if (function_exists('json_last_error') && ($errno = json_last_error())) { JWT::handleJsonError($errno); } elseif ($json === 'null' && $input !== null) { throw new DomainException('Null result with non-null input'); } return $json; }