[PLUGIN][OAuth2] Fix some static issues

This commit is contained in:
2022-02-04 18:34:08 +00:00
parent 4dd976eb22
commit 81f6d496c6
15 changed files with 206 additions and 202 deletions

View File

@@ -34,6 +34,7 @@ namespace Plugin\OAuth2\Entity;
use DateTimeInterface;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use Plugin\OAuth2\Util\Token;
class AccessToken extends Token implements AccessTokenEntityInterface
{
@@ -47,20 +48,14 @@ class AccessToken extends Token implements AccessTokenEntityInterface
private bool $revoked;
private DateTimeInterface $created;
public function setId(string $id): self
{
$this->id = mb_substr($id, 0, 64);
return $this;
}
public function getId(): string
{
return $this->id;
}
public function setExpiry(DateTimeInterface $expiry): self
public function setId(string $id): self
{
$this->expiry = $expiry;
$this->id = mb_substr($id, 0, 64);
return $this;
}
@@ -69,9 +64,9 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->expiry;
}
public function setUserId(?int $user_id): self
public function setExpiry(DateTimeInterface $expiry): self
{
$this->user_id = $user_id;
$this->expiry = $expiry;
return $this;
}
@@ -80,9 +75,9 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->user_id;
}
public function setClientId(string $client_id): self
public function setUserId(?int $user_id): self
{
$this->client_id = mb_substr($client_id, 0, 64);
$this->user_id = $user_id;
return $this;
}
@@ -91,9 +86,9 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->client_id;
}
public function setTokenScopes(string $token_scopes): self
public function setClientId(string $client_id): self
{
$this->token_scopes = $token_scopes;
$this->client_id = mb_substr($client_id, 0, 64);
return $this;
}
@@ -102,9 +97,9 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->token_scopes;
}
public function setRevoked(bool $revoked): self
public function setTokenScopes(string $token_scopes): self
{
$this->revoked = $revoked;
$this->token_scopes = $token_scopes;
return $this;
}
@@ -113,9 +108,9 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->revoked;
}
public function setCreated(DateTimeInterface $created): self
public function setRevoked(bool $revoked): self
{
$this->created = $created;
$this->revoked = $revoked;
return $this;
}
@@ -124,13 +119,19 @@ class AccessToken extends Token implements AccessTokenEntityInterface
return $this->created;
}
public function setCreated(DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
public CryptKey $private_key;
public function setPrivateKey(CryptKey $private_key)
public function setPrivateKey(CryptKey $privateKey)
{
$this->private_key = $private_key;
$this->private_key = $privateKey;
}
public function __toString()

View File

@@ -34,6 +34,7 @@ namespace Plugin\OAuth2\Entity;
use DateTimeInterface;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use Plugin\OAuth2\Repository;
use Plugin\OAuth2\Util\Token;
class AuthCode extends Token implements AuthCodeEntityInterface
{
@@ -47,20 +48,14 @@ class AuthCode extends Token implements AuthCodeEntityInterface
private bool $revoked;
private DateTimeInterface $created;
public function setId(string $id): self
{
$this->id = mb_substr($id, 0, 64);
return $this;
}
public function getId(): string
{
return $this->id;
}
public function setExpiry(DateTimeInterface $expiry): self
public function setId(string $id): self
{
$this->expiry = $expiry;
$this->id = mb_substr($id, 0, 64);
return $this;
}
@@ -69,9 +64,9 @@ class AuthCode extends Token implements AuthCodeEntityInterface
return $this->expiry;
}
public function setUserId(?int $user_id): self
public function setExpiry(DateTimeInterface $expiry): self
{
$this->user_id = $user_id;
$this->expiry = $expiry;
return $this;
}
@@ -80,20 +75,9 @@ class AuthCode extends Token implements AuthCodeEntityInterface
return $this->user_id;
}
public function setClientId(string $client_id): self
public function setUserId(?int $user_id): self
{
$this->client_id = mb_substr($client_id, 0, 64);
return $this;
}
public function getClientId(): string
{
return $this->client_id;
}
public function setTokenScopes(string $token_scopes): self
{
$this->token_scopes = $token_scopes;
$this->user_id = $user_id;
return $this;
}
@@ -102,9 +86,9 @@ class AuthCode extends Token implements AuthCodeEntityInterface
return $this->token_scopes;
}
public function setRevoked(bool $revoked): self
public function setTokenScopes(string $token_scopes): self
{
$this->revoked = $revoked;
$this->token_scopes = $token_scopes;
return $this;
}
@@ -113,9 +97,9 @@ class AuthCode extends Token implements AuthCodeEntityInterface
return $this->revoked;
}
public function setCreated(DateTimeInterface $created): self
public function setRevoked(bool $revoked): self
{
$this->created = $created;
$this->revoked = $revoked;
return $this;
}
@@ -124,25 +108,38 @@ class AuthCode extends Token implements AuthCodeEntityInterface
return $this->created;
}
public function setCreated(DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
/**
* @return null|string
*/
public function getRedirectUri()
public function getClientId(): string
{
return (new Repository\Client)->getClientEntity($this->getClientId())->getRedirectUri();
return $this->client_id;
}
/**
* @param string $uri
*/
public function setRedirectUri($uri)
public function setClientId(string $client_id): self
{
$this->client_id = mb_substr($client_id, 0, 64);
return $this;
}
public function getRedirectUri(): ?string
{
/** @var Client $client */
$client = (new Repository\Client)->getClientEntity($this->getClientId());
$client->setRedirectUris($uri);
return $client->getRedirectUris();
}
public function setRedirectUri($uri): Client
{
/** @var Client $client */
$client = (new Repository\Client)->getClientEntity($this->getClientId());
return $client->setRedirectUris($uri);
}
public static function schemaDef(): array

View File

@@ -52,20 +52,14 @@ class Client extends Entity implements ClientEntityInterface
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setId(string $id): self
{
$this->id = mb_substr($id, 0, 64);
return $this;
}
public function getId(): string
{
return $this->id;
}
public function setSecret(string $secret): self
public function setId(string $id): self
{
$this->secret = mb_substr($secret, 0, 64);
$this->id = mb_substr($id, 0, 64);
return $this;
}
@@ -74,9 +68,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->secret;
}
public function setActive(bool $active): self
public function setSecret(string $secret): self
{
$this->active = $active;
$this->secret = mb_substr($secret, 0, 64);
return $this;
}
@@ -85,9 +79,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->active;
}
public function setPlainPcke(bool $plain_pcke): self
public function setActive(bool $active): self
{
$this->plain_pcke = $plain_pcke;
$this->active = $active;
return $this;
}
@@ -96,9 +90,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->plain_pcke;
}
public function setIsConfidential(bool $is_confidential): self
public function setPlainPcke(bool $plain_pcke): self
{
$this->is_confidential = $is_confidential;
$this->plain_pcke = $plain_pcke;
return $this;
}
@@ -107,9 +101,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->is_confidential;
}
public function setRedirectUris(string $redirect_uris): self
public function setIsConfidential(bool $is_confidential): self
{
$this->redirect_uris = $redirect_uris;
$this->is_confidential = $is_confidential;
return $this;
}
@@ -118,9 +112,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->redirect_uris;
}
public function setGrants(string $grants): self
public function setRedirectUris(string $redirect_uris): self
{
$this->grants = $grants;
$this->redirect_uris = $redirect_uris;
return $this;
}
@@ -129,9 +123,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->grants;
}
public function setScopes(string $scopes): self
public function setGrants(string $grants): self
{
$this->scopes = $scopes;
$this->grants = $grants;
return $this;
}
@@ -140,6 +134,12 @@ class Client extends Entity implements ClientEntityInterface
return $this->scopes;
}
public function setScopes(string $scopes): self
{
$this->scopes = $scopes;
return $this;
}
public function setClientName(string $client_name): self
{
$this->client_name = mb_substr($client_name, 0, 191);
@@ -151,20 +151,14 @@ class Client extends Entity implements ClientEntityInterface
return $this->client_name;
}
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
public function getWebsite(): ?string
{
return $this->website;
}
public function setCreated(DateTimeInterface $created): self
public function setWebsite(?string $website): self
{
$this->created = $created;
$this->website = $website;
return $this;
}
@@ -173,9 +167,9 @@ class Client extends Entity implements ClientEntityInterface
return $this->created;
}
public function setModified(DateTimeInterface $modified): self
public function setCreated(DateTimeInterface $created): self
{
$this->modified = $modified;
$this->created = $created;
return $this;
}
@@ -184,6 +178,11 @@ class Client extends Entity implements ClientEntityInterface
return $this->modified;
}
public function setModified(DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode

View File

@@ -32,9 +32,11 @@ declare(strict_types = 1);
namespace Plugin\OAuth2\Entity;
use App\Core\Entity;
use DateTimeImmutable;
use DateTimeInterface;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Plugin\OAuth2\Repository;
class RefreshToken extends Entity implements RefreshTokenEntityInterface
{
@@ -46,20 +48,14 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
private bool $revoked;
private DateTimeInterface $created;
public function setId(string $id): self
{
$this->id = mb_substr($id, 0, 64);
return $this;
}
public function getId(): string
{
return $this->id;
}
public function setExpiry(DateTimeInterface $expiry): self
public function setId(string $id): self
{
$this->expiry = $expiry;
$this->id = mb_substr($id, 0, 64);
return $this;
}
@@ -68,9 +64,9 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
return $this->expiry;
}
public function setAccessTokenId(?string $access_token_id): self
public function setExpiry(DateTimeInterface $expiry): self
{
$this->access_token_id = \is_null($access_token_id) ? null : mb_substr($access_token_id, 0, 64);
$this->expiry = $expiry;
return $this;
}
@@ -79,9 +75,9 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
return $this->access_token_id;
}
public function setRevoked(bool $revoked): self
public function setAccessTokenId(?string $access_token_id): self
{
$this->revoked = $revoked;
$this->access_token_id = \is_null($access_token_id) ? null : mb_substr($access_token_id, 0, 64);
return $this;
}
@@ -90,9 +86,9 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
return $this->revoked;
}
public function setCreated(DateTimeInterface $created): self
public function setRevoked(bool $revoked): self
{
$this->created = $created;
$this->revoked = $revoked;
return $this;
}
@@ -101,45 +97,21 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
return $this->created;
}
public function setCreated(DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
/**
* Get the token's identifier.
*
* @return string
* Get the access token that the refresh token was originally associated with.
*/
public function getIdentifier()
public function getAccessToken(): AccessTokenEntityInterface
{
return $this->getId();
}
/**
* Set the token's identifier.
*/
public function setIdentifier($identifier)
{
$this->setId($identifier);
}
/**
* Get the token's expiry date time.
*
* @return DateTimeImmutable
*/
public function getExpiryDateTime()
{
return $this->getExpiry();
}
/**
* Set the date time when the token expires.
*
* @param DateTimeImmutable $dateTime
*/
public function setExpiryDateTime(\DateTimeImmutable $dateTime)
{
$this->setExpiry($dateTime);
return (new Repository\AccessToken())->getAccessTokenEntity($this->getAccessTokenId());
}
/**
@@ -150,14 +122,27 @@ class RefreshToken extends Entity implements RefreshTokenEntityInterface
$this->setAccessTokenId($accessToken->getIdentifier());
}
/**
* Get the access token that the refresh token was originally associated with.
*
* @return AccessTokenEntityInterface
*/
public function getAccessToken()
public function setIdentifier($identifier): self
{
return (new Repository\AccessToken)->getAccessTokenEntity($this->getAccessTokenId());
return $this->setId($identifier);
}
public function getIdentifier(): string
{
return $this->getId();
}
/**
* Set the date time when the token expires.
*/
public function setExpiryDateTime(DateTimeImmutable $dateTime)
{
$this->setExpiry($dateTime);
}
public function getExpiryDateTime(): DateTimeImmutable
{
return DateTimeImmutable::createFromInterface($this->getExpiry());
}
public static function schemaDef(): array

View File

@@ -1,149 +0,0 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* OAuth2 Client
*
* @package GNUsocial
*
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Plugin\OAuth2\Entity;
use App\Core\Entity;
use Functional as F;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\TokenInterface;
use Plugin\OAuth2\Repository;
abstract class Token extends Entity implements TokenInterface
{
public function getIdentifier()
{
return $this->getId();
}
public function setIdentifier($identifier)
{
$this->setId($identifier);
}
/**
* Get the token's expiry date time.
*
* @return DateTimeImmutable
*/
public function getExpiryDateTime()
{
return $this->getExpiry();
}
/**
* Set the date time when the token expires.
*
* @param DateTimeImmutable $dateTime
*/
public function setExpiryDateTime(\DateTimeImmutable $dateTime)
{
$this->setExpiry($dateTime);
}
/**
* Set the identifier of the user associated with the token.
*
* @param null|int|string $identifier The identifier of the user
*/
public function setUserIdentifier($identifier)
{
$this->setUserId($identifier);
}
/**
* Get the token user's identifier.
*
* @return null|int|string
*/
public function getUserIdentifier()
{
return $this->getUserId();
}
/**
* Get the client that the token was issued to.
*
* @return ClientEntityInterface
*/
public function getClient()
{
return (new Repository\Client)->getClientEntity($this->getClientId());
}
/**
* Set the client that the token was issued to.
*/
public function setClient(ClientEntityInterface $client)
{
$this->setClientId($client->getIdentifier());
}
/**
* Associate a scope with the token.
*/
public function addScope(ScopeEntityInterface $scope)
{
$scope = $this->hasTokenScopes() ? $this->getTokenScopes() . ' ' . $scope->getIdentifier() : $scope->getIdentifier();
$this->setTokenScopes($scope);
}
/**
* Return an array of scopes associated with the token.
*
* @return ScopeEntityInterface[]
*/
public function getScopes()
{
return F\map(
explode(' ', $this->getTokenScopes()),
fn (string $scope) => (new Repository\Scope)->getScopeEntityByIdentifier($scope),
);
}
public static function tokenSchema(string $table_name): array
{
return [
'name' => $table_name,
'fields' => [
'id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'description' => 'identifier for this token'],
'expiry' => ['type' => 'datetime', 'not null' => true, 'description' => 'when this token expires'],
'user_id' => ['type' => 'int', 'foreign key' => true, 'description' => 'Actor foreign key'],
'client_id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'foreign key' => true, 'description' => 'OAuth client foreign key'],
'token_scopes' => ['type' => 'text', 'not null' => true, 'description' => 'Space separated scopes'],
'revoked' => ['type' => 'bool', 'not null' => true, 'foreign key' => true, 'description' => 'Whether this token is revoked'],
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
],
'primary key' => ['id'],
];
}
}