Skip to content

Commit

Permalink
IBX-5314: [PAPI] Implemented Token Storage Service (#217)
Browse files Browse the repository at this point in the history
For more details see #217

Co-authored-by: Paweł Niedzielski <Steveb-p@users.noreply.github.com>
Co-authored-by: Nattfarinn <Nattfarinn@users.noreply.github.com>
Co-authored-by: Andrew Longosz <alongosz@users.noreply.github.com>
  • Loading branch information
4 people authored Apr 5, 2023
1 parent a14007e commit 8f1a3c3
Show file tree
Hide file tree
Showing 42 changed files with 2,241 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/bundle/Core/DependencyInjection/IbexaCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Core\DependencyInjection;

use Ibexa\Bundle\Core\DependencyInjection\Compiler\QueryTypePass;
Expand Down Expand Up @@ -392,6 +394,7 @@ private function handleApiLoading(ContainerBuilder $container, FileLoader $loade
$coreLoader->load('user_preference.yml');
$coreLoader->load('events.yml');
$coreLoader->load('thumbnails.yml');
$coreLoader->load('tokens.yml');
$coreLoader->load('content_location_mapper.yml');

// Public API services
Expand Down
25 changes: 25 additions & 0 deletions src/bundle/Core/Resources/config/storage/legacy/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,28 @@ tables:
group: { type: string, nullable: false, length: 128 }
identifier: { type: string, nullable: false, length: 128 }
value: { type: json, nullable: false, length: 0 }
ibexa_token_type:
uniqueConstraints:
ibexa_token_type_unique: { fields: [identifier] }
id:
id: { type: integer, nullable: false, options: { autoincrement: true } }
fields:
identifier: { type: string, nullable: false, length: 32 }
ibexa_token:
uniqueConstraints:
ibexa_token_unique: { fields: [token, identifier, type_id] }
foreignKeys:
ibexa_token_type_id_fk:
fields: [type_id]
foreignTable: ibexa_token_type
foreignFields: [id]
options:
onDelete: CASCADE
id:
id: { type: integer, nullable: false, options: { autoincrement: true } }
fields:
type_id: { type: integer, nullable: false }
token: { type: string, nullable: false, length: 255 }
identifier: { type: string, nullable: true, length: 128 }
created: { type: integer, nullable: false, options: { default: '0' } }
expires: { type: integer, nullable: false, options: { default: '0' } }
25 changes: 25 additions & 0 deletions src/contracts/Persistence/Token/CreateStruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Persistence\Token;

use Ibexa\Contracts\Core\Persistence\ValueObject;

/**
* @internal
*/
final class CreateStruct extends ValueObject
{
public string $type;

public string $token;

public int $ttl;

public ?string $identifier = null;
}
37 changes: 37 additions & 0 deletions src/contracts/Persistence/Token/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Persistence\Token;

/**
* @internal
*/
interface Handler
{
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
public function getToken(
string $tokenType,
string $token,
?string $identifier = null
): Token;

public function getTokenType(
string $identifier
): TokenType;

public function createToken(CreateStruct $createStruct): Token;

public function deleteToken(Token $token): void;

public function deleteTokenById(int $tokenId): void;

public function deleteExpiredTokens(?string $tokenType = null): void;
}
29 changes: 29 additions & 0 deletions src/contracts/Persistence/Token/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Persistence\Token;

use Ibexa\Contracts\Core\Persistence\ValueObject;

/**
* @internal
*/
final class Token extends ValueObject
{
public int $id;

public int $typeId;

public string $token;

public ?string $identifier = null;

public int $created;

public int $expires;
}
21 changes: 21 additions & 0 deletions src/contracts/Persistence/Token/TokenType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Persistence\Token;

use Ibexa\Contracts\Core\Persistence\ValueObject;

/**
* @internal
*/
final class TokenType extends ValueObject
{
public int $id;

public string $identifier;
}
69 changes: 69 additions & 0 deletions src/contracts/Repository/Decorator/TokenServiceDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Decorator;

use Ibexa\Contracts\Core\Repository\TokenService;
use Ibexa\Contracts\Core\Repository\Values\Token\Token;
use Ibexa\Contracts\Core\Token\TokenGeneratorInterface;

abstract class TokenServiceDecorator implements TokenService
{
protected TokenService $innerService;

public function __construct(
TokenService $innerService
) {
$this->innerService = $innerService;
}

public function getToken(
string $tokenType,
string $token,
?string $identifier = null
): Token {
return $this->innerService->getToken(
$tokenType,
$token,
$identifier
);
}

public function checkToken(
string $tokenType,
string $token,
?string $identifier = null
): bool {
return $this->innerService->checkToken(
$tokenType,
$token,
$identifier
);
}

public function generateToken(
string $type,
int $ttl,
?string $identifier = null,
int $tokenLength = 64,
?TokenGeneratorInterface $tokenGenerator = null
): Token {
return $this->innerService->generateToken(
$type,
$ttl,
$identifier,
$tokenLength,
$tokenGenerator
);
}

public function deleteToken(Token $token): void
{
$this->innerService->deleteToken($token);
}
}
70 changes: 70 additions & 0 deletions src/contracts/Repository/Events/Token/BeforeCheckTokenEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Events\Token;

use Ibexa\Contracts\Core\Repository\Event\BeforeEvent;
use UnexpectedValueException;

final class BeforeCheckTokenEvent extends BeforeEvent
{
private ?bool $result = null;

private string $tokenType;

private string $token;

private ?string $identifier;

public function __construct(
string $tokenType,
string $token,
?string $identifier = null
) {
$this->tokenType = $tokenType;
$this->token = $token;
$this->identifier = $identifier;
}

public function getResult(): bool
{
if (!$this->hasResult()) {
throw new UnexpectedValueException(
'Return value is not set.' . PHP_EOL
. 'Check hasResult() or set it using setResult() before you call the getter.'
);
}

return $this->result;
}

public function setResult(?bool $result): void
{
$this->result = $result;
}

public function hasResult(): bool
{
return $this->result !== null;
}

public function getTokenType(): string
{
return $this->tokenType;
}

public function getToken(): string
{
return $this->token;
}

public function getIdentifier(): ?string
{
return $this->identifier;
}
}
27 changes: 27 additions & 0 deletions src/contracts/Repository/Events/Token/BeforeDeleteTokenEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Events\Token;

use Ibexa\Contracts\Core\Repository\Event\BeforeEvent;
use Ibexa\Contracts\Core\Repository\Values\Token\Token;

final class BeforeDeleteTokenEvent extends BeforeEvent
{
private Token $token;

public function __construct(Token $token)
{
$this->token = $token;
}

public function getToken(): Token
{
return $this->token;
}
}
Loading

0 comments on commit 8f1a3c3

Please sign in to comment.