Repositories | Polyback | Log in to see private repositories


polyback-website / b03649d777b564d64767a436c04d21dc3fbdcf0a / api/studio/login.php

2,937 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// Polygon Studio sign-in.
//   POST JSON {"username", "password", "code"?}   code: 2FA code or a recovery code
// -> {"success":true, "token", "user":{"id","name"}}
//    {"success":false, "needs2fa":true, "message"} when the account has 2FA and no/invalid code

use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Password;
use pizzaboxer\ProjectPolygon\Studio;
use pizzaboxer\ProjectPolygon\Users;
use Sonata\GoogleAuthenticator\GoogleAuthenticator;

API::initialize(["method" => "POST"]);

$ip = (string) ($_SERVER["REMOTE_ADDR"] ?? "");
$fails = Database::singleton()->run(
	"SELECT COUNT(*) FROM StudioLoginAttempts WHERE IP = :ip AND Time + 600 > UNIX_TIMESTAMP()", [":ip" => $ip]
)->fetchColumn();
if ($fails >= 10) API::respond(429, false, "Too many sign-in attempts. Please wait a few minutes.");

function fail($message, $extra = [])
{
	global $ip;
	Database::singleton()->run("DELETE FROM StudioLoginAttempts WHERE Time + 3600 < UNIX_TIMESTAMP()");
	Database::singleton()->run("INSERT INTO StudioLoginAttempts (IP, Time) VALUES (:ip, UNIX_TIMESTAMP())", [":ip" => $ip]);
	API::respondCustom(array_merge(["status" => 401, "success" => false, "message" => $message], $extra));
}

$body = Studio::Body();
$username = trim((string) ($body["username"] ?? ""));
$password = (string) ($body["password"] ?? "");
$code = trim((string) ($body["code"] ?? ""));
if ($username === "" || $password === "") API::respond(400, false, "Enter your username and password");

$user = Users::GetInfoFromName($username);
$auth = new Password($password);
if (!$user || !$auth->verify($user->password)) fail("Incorrect username or password");
if (!empty($user->Banned)) API::respond(403, false, "This account is banned");

if ($user->twofa)
{
	if ($code === "") API::respondCustom(["status" => 401, "success" => false, "needs2fa" => true, "message" => "Enter the code from your authenticator app (or a recovery code)"]);
	$recovery = json_decode((string) $user->twofaRecoveryCodes, true) ?: [];
	if (ctype_digit($code))
	{
		$gauth = new GoogleAuthenticator();
		if (!$gauth->checkCode($user->twofaSecret, $code, 1)) fail("Incorrect 2FA code", ["needs2fa" => true]);
	}
	else
	{
		if (empty($recovery[$code])) fail("Invalid recovery code", ["needs2fa" => true]);
		$recovery[$code] = false;
		Database::singleton()->run("UPDATE users SET twofaRecoveryCodes = :c WHERE id = :id", [":c" => json_encode($recovery), ":id" => $user->id]);
	}
}

// keep passwords on the current hash, as the site's own sign-in does
if (strpos($user->password, "$2y$10") !== false) $auth->update($user->id);

$token = Studio::CreateToken($user->id, substr((string) ($body["label"] ?? "Polygon Studio"), 0, 100));
API::respondCustom(["status" => 200, "success" => true, "token" => $token, "user" => ["id" => (int) $user->id, "name" => $user->username]]);