Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5314: Implemented token storage #217

Merged
merged 46 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1e58ce5
wip: TokenService
Nattfarinn Mar 7, 2023
c565714
fix: strict_types
Nattfarinn Mar 7, 2023
404e918
Marked classes as final
ciastektk Mar 15, 2023
5dacbad
Marked TokenServiceDecorator as abstract class
ciastektk Mar 15, 2023
2d34fb9
Fixed constructor argument
ciastektk Mar 15, 2023
4c7c2f3
Added Token/AbstractGateway
ciastektk Mar 15, 2023
4346d24
Fixed Token services config
ciastektk Mar 15, 2023
6487797
Reordered token tables in schema
ciastektk Mar 15, 2023
96be6c6
Deleted token.yaml
ciastektk Mar 16, 2023
0f32b1d
Marked WebSafeGenerator as final
ciastektk Mar 16, 2023
64aa8cb
Fixed schema
ciastektk Mar 22, 2023
50a3a4f
Added missing internal annotation
ciastektk Mar 22, 2023
4a63a6e
Fixed TokenService
ciastektk Mar 22, 2023
357ca05
Renamed token variable in Handler::getToken to avoid override
ciastektk Mar 22, 2023
8540a49
Used \Doctrine\DBAL\ParameterType instead of PDO
ciastektk Mar 22, 2023
f9984fb
Fixed CS - Exception message line too long
ciastektk Mar 22, 2023
69b725c
Changed Repository\Token properties to private
ciastektk Mar 22, 2023
5b60680
[PHPDOC] Fixed throws annotations for Handler
ciastektk Mar 22, 2023
62ad314
Fixed Gateways
ciastektk Mar 22, 2023
0bc89b7
Added new line to exception message to avoid too long single line
ciastektk Mar 23, 2023
8710d85
Added WebSafeGeneratorTest
ciastektk Mar 23, 2023
8363aeb
Added token length validation to avoid generate too long token
ciastektk Mar 23, 2023
e9c6d29
Added sequence name for last inserted ids
ciastektk Mar 28, 2023
a2ea0fa
Added TokenServiceTest
ciastektk Mar 28, 2023
3528b5e
Added test to cover throwing TokenLengthException
ciastektk Mar 28, 2023
b848680
Update src/lib/Persistence/Legacy/Token/Gateway/Token/Doctrine/Doctri…
ciastektk Mar 31, 2023
20eb642
Update src/lib/Persistence/Legacy/Token/Gateway/Token/Doctrine/Doctri…
ciastektk Mar 31, 2023
4e49413
Update src/lib/Persistence/Legacy/Token/Gateway/TokenType/Doctrine/Do…
ciastektk Mar 31, 2023
7e803d0
Update src/lib/Persistence/Legacy/Token/Gateway/TokenType/Doctrine/Do…
ciastektk Mar 31, 2023
334e597
Update src/lib/Persistence/Legacy/Token/Gateway/TokenType/Doctrine/Do…
ciastektk Mar 31, 2023
e87468e
Update src/bundle/Core/Resources/config/storage/legacy/schema.yaml
ciastektk Mar 31, 2023
626936f
[Code style] Fixed CS
ciastektk Mar 31, 2023
5641575
[Tests] Fixed TokenServiceTest
ciastektk Mar 31, 2023
13e6155
Fixed token events exception messages
ciastektk Mar 31, 2023
42c6ea3
Fixed Exception messages
ciastektk Apr 3, 2023
70c631f
Moved token generating to RandomBytesGenerator
ciastektk Apr 3, 2023
052aa05
Fixed WebSafeGeneratorTest
ciastektk Apr 3, 2023
b032ab5
Removed Token::fromArray
ciastektk Apr 3, 2023
61c2df5
Fixed RandomBytesGeneratorTest
ciastektk Apr 3, 2023
0975d24
Added RandomBytesGeneratorTest
ciastektk Apr 3, 2023
37fb306
Update tests/lib/Token/RandomBytesGeneratorTest.php
ciastektk Apr 3, 2023
19d232d
Update tests/lib/Token/WebSafeGeneratorTest.php
ciastektk Apr 3, 2023
61e4f31
Fixed truncating token by RandomBytesGenerator
ciastektk Apr 4, 2023
4e5d793
Fixed TokenServiceTest
ciastektk Apr 4, 2023
47abdc3
Removed token settings from old integration test setup
ciastektk Apr 4, 2023
2982da0
[Tests] Restored loading Tokens DIC for the container to compile
alongosz Apr 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -390,6 +392,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
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
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
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @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
ciastektk marked this conversation as resolved.
Show resolved Hide resolved
{
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