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 login(StatTracker $StatTracker) { $response = new StdClass(); $response->error = false; // Kick off the OAuth process if (empty($StatTracker['session']->get("token"))) { return AuthResponse::authenticationRequired($this); } $this->client->setAccessToken($StatTracker['session']->get("token")); if ($this->client->isAccessTokenExpired()) { return AuthResponse::authenticationRequired($this); } if ($StatTracker['session']->get("agent") === null) { try { $me = $this->plus->people->get('me'); $email_address = ""; foreach ($me->getEmails() as $email) { if ($email->type == "account") { $email_address = $email->value; } } if (empty($email_address)) { return AuthResponse::error("Google did not provide an email address."); } $agent = Agent::lookupAgentName($email_address); if (!$agent->isValid()) { // Could be no token, or new user. // If a name is present, they have been approved, so generate a token and proceed if (!empty($agent->name)) { $this->generateAPIToken($agent); $agent = Agent::lookupAgentName($email_address); if (!$agent->isValid()) { $response = AuthResponse::error("Not a valid agent"); } else { $StatTracker['session']->set("agent", $agent); $response = AuthResponse::okay($agent); } } else { // They need to register, this code is a challenge $this->generateAuthCode($email_address); $response = AuthResponse::registrationRequired(sprintf("An email has been sent to<br/><strong>%s</strong><br/>with steps to complete registration", $email_address), $email_address); $this->logger->info(sprintf("Registration required for %s", $email_address)); } } else { $StatTracker['session']->set("agent", $agent); $response = AuthResponse::okay($agent); $this->logger->info(sprintf("%s authenticated successfully", $agent->name)); } } catch (Exception $e) { $response::error($e->getMessage()); $this->logger->error(sprintf("EXCEPTION: %s\n%s:%s", $e->getMessage(), $e->getFile(), $e->getLine())); return $response; } } else { $agent = $StatTracker['session']->get("agent"); // Ensure token is valid if (Agent::lookupAgentByToken($agent->getToken())->isValid()) { $response = AuthResponse::okay($agent); } else { $this->logger->info(sprintf("Expired token for %s. Logging out", $agent->name)); return $this->logout($StatTracker); } } return $response; }