Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a283299
[#.x] - removed helloService
niden Oct 26, 2025
0b5c2e3
[#.x] - removed processIsolation
niden Oct 26, 2025
1c997cb
[#.x] - added new Http Codes
niden Oct 26, 2025
a8b2196
[#.x] - refactored EnvManager
niden Oct 26, 2025
35421cf
[#.x] - added Cache class
niden Oct 26, 2025
04619f3
[#.x] - added exception trait and new exception
niden Oct 26, 2025
8a8b41f
[#.x] - added auth post login service
niden Oct 26, 2025
5ed4129
[#.x] - added new route and adjusted tests
niden Oct 26, 2025
1f3b256
[#.x] - added JTW class, tests and enum
niden Oct 26, 2025
0dbc02d
[#.x] - corrected env references
niden Oct 26, 2025
1568160
[#.x] - new types and added POST to input
niden Oct 26, 2025
1455fe9
[#.x] - added new middleware for tokens
niden Oct 26, 2025
4f94a64
[#.x] - corrected test
niden Oct 26, 2025
cd3556b
[#.x] - new types for user
niden Oct 26, 2025
81ebcd2
[#.x] - minor adjustments
niden Oct 26, 2025
87ca7d9
[#.x] - added new services (cache, jwt etc.)
niden Oct 26, 2025
de73eab
[#.x] - added token related methods
niden Oct 26, 2025
10fa5bf
[#.x] - added getToken and strengthened password generation
niden Oct 26, 2025
578a23a
[#.x] - added imports
niden Oct 26, 2025
dc5a132
[#.x] - new session transport properties
niden Oct 26, 2025
1dadc3e
[#.x] - phpstan
niden Oct 26, 2025
b8b37ff
Update src/Domain/Components/DataSource/TransportRepository.php
niden Oct 26, 2025
2dd49da
Update src/Domain/Components/DataSource/User/UserRepository.php
niden Oct 26, 2025
a53936d
[#.x] - fixed getOptions call
niden Oct 26, 2025
5088121
[#.x] - phpcs
niden Oct 26, 2025
e91e734
[#.x] - correcting .env
niden Oct 26, 2025
b35db43
[#.x] - added redis service
niden Oct 26, 2025
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
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ jobs:
MYSQL_DATABASE: phalcon
MYSQL_PASSWORD: secret

redis:
image: redis:8-alpine
ports:
- "6379:6379"

steps:
- uses: actions/checkout@v4

Expand Down
25 changes: 21 additions & 4 deletions config/.env.ci
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Docker prefix
PROJECT_NAME="rest"

# Environment
APP_ENV_ADAPTER="dotenv"
APP_ENV_FILE_PATH="./"

# Mariadb
DB_HOST="127.0.0.1"
DB_PORT=3306
Expand All @@ -8,9 +13,21 @@ DB_PASS="secret"
DB_NAME="phalcon"
DB_CHARSET="utf8"

# Redis
DATA_REDIS_HOST="app-cache"
DATA_REDIS_PORT=6379
DATA_REDIS_NAME="0"
# Cache
CACHE_ADAPTER="redis"
CACHE_HOST="127.0.0.1"
CACHE_PORT=6379
CACHE_INDEX="0"
CACHE_LIFETIME=86400
CACHE_PREFIX="-rest-"

# Logger
LOG_FILENAME="rest-api"
LOG_PATH="storage/logs"

# Code Coverage
XDEBUG_MODE=coverage

# JWT
TOKEN_EXPIRATION=14400
TOKEN_AUDIENCE="https://rest-api.phalcon.io"
29 changes: 23 additions & 6 deletions config/.env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
# Docker prefix
PROJECT_NAME="rest"

# Environment
APP_ENV_ADAPTER="dotenv"
APP_ENV_FILE_PATH="./"

# Mariadb
DB_HOST="app-db"
DB_HOST="rest-db"
DB_PORT=3306
DB_USER="root"
DB_PASS="secret"
DB_NAME="phalcon"
DB_CHARSET= "utf8"
DB_CHARSET="utf8"

# Cache
CACHE_ADAPTER="redis"
CACHE_HOST="rest-cache"
CACHE_PORT=6379
CACHE_INDEX="0"
CACHE_LIFETIME=86400
CACHE_PREFIX="-rest-"

# Redis
DATA_REDIS_HOST="app-cache"
DATA_REDIS_PORT=6379
DATA_REDIS_NAME="0"
# Logger
LOG_FILENAME="rest-api"
LOG_PATH="storage/logs"

# Code Coverage
XDEBUG_MODE=coverage

# JWT
TOKEN_EXPIRATION=14400
TOKEN_AUDIENCE="https://rest-api.phalcon.io"
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
bootstrap="./phpunit.php"
cacheDirectory="tests/_output/.phpunit.result.cache"
colors="true"
processIsolation="true"
>
<testsuites>
<testsuite name="unit tests">
Expand Down
6 changes: 6 additions & 0 deletions src/Action/ActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@

use Phalcon\Api\Domain\ADR\DomainInterface;
use Phalcon\Api\Domain\ADR\Input;
use Phalcon\Api\Domain\ADR\InputTypes;
use Phalcon\Api\Responder\ResponderInterface;
use Phalcon\Http\RequestInterface;
use Phalcon\Http\ResponseInterface;

/**
* @phpstan-import-type TLoginInput from InputTypes
* @phpstan-import-type TUserInput from InputTypes
*/
final readonly class ActionHandler implements ActionInterface
{
public function __construct(
Expand All @@ -32,6 +37,7 @@ public function __construct(
public function __invoke(): void
{
$input = new Input();
/** @var TLoginInput|TUserInput $data */
$data = $input->__invoke($this->request);

$this->responder->__invoke(
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/ADR/DomainInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use Phalcon\Domain\Payload;

/**
* @phpstan-import-type THelloInput from InputTypes
* @phpstan-import-type TLoginInput from InputTypes
* @phpstan-import-type TUserInput from InputTypes
*/
interface DomainInterface
{
/**
* @param THelloInput|TUserInput $input
* @param TLoginInput|TUserInput $input
*
* @return Payload
*/
Expand Down
6 changes: 5 additions & 1 deletion src/Domain/ADR/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Phalcon\Http\RequestInterface;

use function array_merge;

/**
* @phpstan-import-type TRequestQuery from InputTypes
*/
Expand All @@ -29,7 +31,9 @@ public function __invoke(RequestInterface $request): array
{
/** @var TRequestQuery $query */
$query = $request->getQuery();
/** @var TRequestQuery $post */
$post = $request->getPost();

return $query;
return array_merge($query, $post);
}
}
5 changes: 4 additions & 1 deletion src/Domain/ADR/InputTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
namespace Phalcon\Api\Domain\ADR;

/**
* @phpstan-type THelloInput array{}
* @phpstan-type TLoginInput array{
* email?: string,
* password?: string
* }
* @phpstan-type TUserInput array{
* userId?: int
* }
Expand Down
92 changes: 92 additions & 0 deletions src/Domain/Components/Cache/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\Components\Cache;

use DateTimeImmutable;
use Phalcon\Api\Domain\Components\Constants\Dates;
use Phalcon\Api\Domain\Components\DataSource\User\UserTransport;
use Phalcon\Api\Domain\Components\Env\EnvManager;
use Phalcon\Cache\Cache as PhalconCache;
use Psr\SimpleCache\InvalidArgumentException;

use function sha1;

class Cache extends PhalconCache
{
/** @var int */
public const CACHE_LIFETIME_DAY = 86400;
/** @var int */
public const CACHE_LIFETIME_HOUR = 3600;
/**
* Cache Timeouts
*/
/** @var int */
public const CACHE_LIFETIME_MINUTE = 60;
/** @var int */
public const CACHE_LIFETIME_MONTH = 2592000;
/**
* Default token expiry - 4 hours
*/
/** @var int */
public const CACHE_TOKEN_EXPIRY = 14400;
/**
* Cache masks
*/
/** @var string */
private const MASK_TOKEN_USER = 'tk-%s-%s';

/**
* @param UserTransport $domainUser
* @param string $token
*
* @return string
*/
public function getCacheTokenKey(UserTransport $domainUser, string $token): string
{
return sprintf(
self::MASK_TOKEN_USER,
$domainUser->getId(),
sha1($token)
);
}

/**
* @param EnvManager $env
* @param UserTransport $domainUser
* @param string $token
*
* @return bool
* @throws InvalidArgumentException
*/
public function storeTokenInCache(
EnvManager $env,
UserTransport $domainUser,
string $token
): bool {
$cacheKey = $this->getCacheTokenKey($domainUser, $token);
/** @var int $expiration */
$expiration = $env->get('TOKEN_EXPIRATION', self::CACHE_TOKEN_EXPIRY, 'int');
$expirationDate = (new DateTimeImmutable())
->modify('+' . $expiration . ' seconds')
->format(Dates::DATE_TIME_FORMAT)
;

$payload = [
'token' => $token,
'expiry' => $expirationDate,
];

return $this->set($cacheKey, $payload, $expiration);
}
}
Loading
Loading