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; }
if (!$agent->hasSubmitted()) { $response->message .= " Since this was your first submission, predictions are not available. Submit again tomorrow to see your predictions."; } $StatTracker['session']->set("agent", Agent::lookupAgentByToken($token)); } else { $response->error = true; $response->message = $result; } } catch (Exception $e) { $response->error = true; $response->message = $e->getMessage(); } return $StatTracker->json($response); })->before($validateRequest); $StatTracker->post("/api/{token}/ocr", function (Request $request, $token) use($StatTracker) { $agent = Agent::lookupAgentByToken($token); if (!$agent->isValid()) { return $StatTracker->abort(403); } $content_type = explode(";", $request->headers->get("content_type"))[0]; $file = UPLOAD_DIR . uniqid("ocr_") . ".png"; switch ($content_type) { case "application/x-www-form-urlencoded": // Not a file upload, but a POST of bytes $hndl = fopen($file, "w+"); fwrite($hndl, file_get_contents("php://input")); fclose($hndl); break; case "multipart/form-data": // Typically an HTTP file upload if ($_FILES['screenshot']['error'] !== 0) {
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; }
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); } // Slack tokens do not expire $this->client->setToken($StatTracker['session']->get("token")); if ($StatTracker['session']->get("agent") === null) { try { $resp = $this->client->execute("users.identity", [])->getBody(); if (!$resp['ok']) { $this->logger->error(sprintf("users.identity response: %s", print_r($resp, true))); throw new Exception(sprintf("Slack identification failed users.identity: %s", $resp['error'])); } $email_address = $resp['user']['email']; if (empty($email_address)) { $this->logger->error("Slack did not provide an email address. Redirect to Login"); return AuthResponse::authenticationRequired($this); } $agent = Agent::lookupAgentName($email_address); if (!$agent->isValid()) { // Could be no token, or new user. if (!empty($agent->name) && $agent->name === "Agent") { // We only need this if the user's email address is not in the ST database $resp = $this->client->execute("auth.test", [])->getBody(); if (!$resp['ok']) { if ($resp['error'] == "missing_scope") { $this->second_auth_pass = true; return AuthResponse::authenticationRequired($this); } else { $this->logger->error(sprintf("auth.test response: %s", print_r($resp, true))); throw new Exception(sprintf("Slack identification failed auth.test: %s", $resp['error'])); } } $agent->name = $resp['user']; $this->createNewAgent($email_address, $agent->name); $this->logger->info(sprintf("Created new agent %s for %s", $agent->name, $email_address)); $this->generateAPIToken($agent); $agent = Agent::lookupAgentName($email_address); if (!$agent->isValid()) { $response = AuthResponse::error(sprintf("No agent associated with %s", $email_address)); } else { $StatTracker['session']->set("agent", $agent); $response = AuthResponse::okay($agent); } } else { error_log(print_r($agent, true)); $response = AuthResponse::error("Not a valid or new agent"); } } else { $StatTracker['session']->set("agent", $agent); $response = AuthResponse::okay($agent); $this->logger->info(sprintf("%s authenticated successfully", $agent->name)); } // Now, this part is utterly stupid...cannot request identify scope (for auth.test) and identity.* // scopes (for users.identity) at the same time. If we don't have the identity.* scopes, redirect and // request them. } catch (Exception $e) { $response = AuthResponse::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; }