public function login(StatTracker $app) { $response = null; if (wp_validate_auth_cookie('', 'logged_in')) { if ($app['session']->get("agent") === null) { $user = wp_get_current_user(); // Allow a plugin to grant/deny this user. See wiki for details $user = apply_filters(ST_USER_AUTH_FILTER, $user); if (!$user instanceof \WP_User) { if (is_string($user)) { $response = AuthResponse::registrationRequired($user); } else { $response = AuthResponse::registrationRequired("Access was denied. Please contact @" . ADMIN_AGENT); } $this->logger->info(sprintf("Registration required for %s", $email_address)); } else { $agent = Agent::lookupAgentName($user->user_email); if (!$agent->isValid()) { $name = apply_filters(ST_AGENT_NAME_FILTER, $user->user_login); $this->logger->info(sprintf("Adding new agent %s", $name)); $agent->name = $name; // Insert them into the DB $stmt = $app->db()->prepare("INSERT INTO Agent (email, agent) VALUES (?, ?) ON DUPLICATE KEY UPDATE agent = ?;"); $stmt->execute(array($user->user_email, $name, $name)); $stmt->closeCursor(); // Generate an API token $this->generateAPIToken($agent); $agent = Agent::lookupAgentName($user->user_email); if (!$agent->isValid()) { $this->logger->error(sprintf("%s still not a valid agent", $agent->name)); return AuthResponse::error("An unrecoverable error has occured"); } } $app['session']->set("agent", $agent); $response = AuthResponse::okay($agent); $this->logger->info(sprintf("%s authenticated successfully", $agent->name)); } } else { $agent = $app['session']->get("agent"); if (Agent::lookupAgentByToken($agent->getToken())->isValid()) { $response = AuthResponse::okay($agent); } else { $this->logger->info(sprintf("Invalid token for %s. Logging out", $agent->name)); return $this->logout($app); } } return $response; } else { $app['session']->set("agent", null); $response = AuthResponse::authenticationRequired($this); } return $response; }
public function logout(StatTracker $StatTracker) { $agent = $StatTracker['session']->get("agent"); $cookies = explode(';', $_SERVER['HTTP_COOKIE']); foreach ($cookies as $cookie) { $parts = explode('=', $cookie); $name = trim($parts[0]); setcookie($name, '', time() - 1000); setcookie($name, '', time() - 1000, '/'); } $this->client->revokeToken($StatTracker['session']->get("token")); session_destroy(); $response = AuthResponse::loggedOut(); $this->logger->info(sprintf("%s logged out", $agent->name)); return $response; }