[PLUGIN][Pinboard] Change token to user user ID rather than nickname, to avoid complications with it possibly changing

This commit is contained in:
2022-03-31 22:06:37 +01:00
parent 94ab4ce8c4
commit 1664293cf7
4 changed files with 15 additions and 32 deletions

View File

@@ -67,10 +67,10 @@ class Token extends Entity
// @codeCoverageIgnoreEnd
// }}} Autocode
public static function cacheKeys(string $nickname): array
public static function cacheKeys(int $id): array
{
return [
'user-token' => "pinboard-token-{$nickname}",
'user-token' => "pinboard-token-{$id}",
];
}
@@ -80,31 +80,30 @@ class Token extends Entity
}
/**
* Get a token for a $nickname and $token pair, unless given a $user, in which case the token field is not validated
* Get a token for a $id and $token pair, unless given a $user, in which case the token field is not validated
*
* XXX: may need to verify it's timing safe
*/
public static function get(?string $nickname, ?string $token, ?LocalUser $user = null): ?self
public static function get(?int $id, ?string $token, ?LocalUser $user = null): ?self
{
if (!\is_null($user)) {
return Cache::get(
self::cacheKeys($user->getNickname())['user-token'],
self::cacheKeys($user->getId())['user-token'],
fn () => DB::dql(
'select t from \Plugin\Pinboard\Entity\Token t where t.actor_id = :id',
['id' => $user->getId()],
options: ['limit' => 1],
),
);
} elseif (!\is_null($nickname) && !\is_null($token)) {
} elseif (!is_id($id) && !\is_null($token)) {
return Cache::get(
self::cacheKeys($nickname)['user-token'],
self::cacheKeys($id)['user-token'],
fn () => DB::dql(
<<<'EOF'
select lu from \App\Entity\LocalUser lu
join \Plugin\Pinboard\Entity\Token t on t.actor_id = lu.actor_id
where lu.nickname = :nickname and t.token = :token and t.enabled = true
select t from \Plugin\Pinboard\Entity\Token t
where t.actor_id = :id and t.token = :token and t.enabled = true
EOF,
['nickname' => $nickame, 'token' => $token],
['id' => $id, 'token' => $token],
options: ['limit' => 1],
),
);
@@ -113,7 +112,7 @@ class Token extends Entity
public function getUserTokenString()
{
return LocalUser::getById($this->getActorId())->getNickname() . ':' . $this->getToken();
return $this->getActorId() . ':' . $this->getToken();
}
public static function generateTokenString(): string