Skip to content

Commit

Permalink
feat(API): implement token re-use mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
celestora committed Mar 30, 2024
1 parent 939ea30 commit 0b80c0a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
24 changes: 24 additions & 0 deletions ServiceAPI/Apps.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;

use openvk\Web\Models\Entities\APIToken;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\APITokens;
use openvk\Web\Models\Repositories\Applications;
use WhichBrowser;

class Apps implements Handler
{
Expand Down Expand Up @@ -89,4 +92,25 @@ function withdrawFunds(int $appId, callable $resolve, callable $reject): void
$app->withdrawCoins();
$resolve($coins);
}

function getRegularToken(string $clientName, bool $acceptsStale, callable $resolve, callable $reject): void
{
$token = NULL;
$stale = true;
if($acceptsStale)
$token = (new APITokens)->getStaleByUser($this->user->getId(), $clientName);

if(is_null($token)) {
$stale = false;
$token = new APIToken;
$token->setUser($this->user);
$token->setPlatform($clientName ?? (new WhichBrowser\Parser(getallheaders()))->toString());
$token->save();
}

$resolve([
'is_stale' => $stale,
'token' => $token->getFormattedToken(),
]);
}
}
9 changes: 9 additions & 0 deletions Web/Models/Repositories/APITokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ function getByCode(string $code, bool $withRevoked = false): ?APIToken

return $token;
}

function getStaleByUser(int $userId, string $platform, bool $withRevoked = false): ?APIToken
{
return $this->toEntity($this->table->where([
'user' => $userId,
'platform' => $platform,
'deleted' => $withRevoked,
])->fetch());
}
}
26 changes: 20 additions & 6 deletions Web/Presenters/VKAPIPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,31 @@ function renderTokenLogin(): void
$this->fail(28, "Invalid 2FA code", "internal", "acquireToken");
}

$platform = $this->requestParam("client_name");

$token = new APIToken;
$token->setUser($user);
$token->setPlatform($platform ?? (new WhichBrowser\Parser(getallheaders()))->toString());
$token->save();
$token = NULL;
$tokenIsStale = true;
$platform = $this->requestParam("client_name");
$acceptsStale = $this->requestParam("accepts_stale");
if($acceptsStale == "1") {
if(is_null($platform))
$this->fail(101, "accepts_stale can only be used with explicitly set client_name", "internal", "acquireToken");

$token = (new APITokens)->getStaleByUser($uId, $platform);
}

if(is_null($token)) {
$tokenIsStale = false;

$token = new APIToken;
$token->setUser($user);
$token->setPlatform($platform ?? (new WhichBrowser\Parser(getallheaders()))->toString());
$token->save();
}

$payload = json_encode([
"access_token" => $token->getFormattedToken(),
"expires_in" => 0,
"user_id" => $uId,
"is_stale" => $tokenIsStale,
]);

$size = strlen($payload);
Expand Down

0 comments on commit 0b80c0a

Please sign in to comment.