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

HttpUtil: Move creating RefreshTokenExistenceCookie to storeRefreshTokenOnResponse #10

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion src/HttpClient/OAuth2HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function __construct(
private readonly HttpClientInterface $client,
private readonly OAuth2Configuration $configuration,
private readonly Serializer $serializer,

) {
}

Expand Down
15 changes: 8 additions & 7 deletions src/Util/HttpUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,9 @@ public function storeJwtOnResponse(Response $response, Token $token, DateTimeImm
$signature,
$lifetime
);
$refreshTokenExistenceCookie = $this->createCookie(
$this->cookieConfiguration->getRefreshTokenExistenceCookieName(),
'1',
$this->cookieConfiguration->getRefreshTokenLifetime(),
false
);

$response->headers->setCookie($payloadCookie);
$response->headers->setCookie($signatureCookie);
$response->headers->setCookie($refreshTokenExistenceCookie);
}

public function storeRefreshTokenOnResponse(Response $response, RefreshTokenDto $refreshTokenDto): void
Expand All @@ -130,7 +123,15 @@ public function storeRefreshTokenOnResponse(Response $response, RefreshTokenDto
$this->cookieConfiguration->getRefreshTokenLifetime(),
);

$refreshTokenExistenceCookie = $this->createCookie(
$this->cookieConfiguration->getRefreshTokenExistenceCookieName(),
'1',
$this->cookieConfiguration->getRefreshTokenLifetime(),
false
);

$response->headers->setCookie($refreshTokenCookie);
$response->headers->setCookie($refreshTokenExistenceCookie);
}

public function storeDeviceIdOnResponse(Response $response, string $deviceId): void
Expand Down
85 changes: 85 additions & 0 deletions tests/Util/HttpUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace AnzuSystems\AuthBundle\Tests\Util;

use AnzuSystems\AuthBundle\Configuration\CookieConfiguration;
use AnzuSystems\AuthBundle\Configuration\JwtConfiguration;
use AnzuSystems\AuthBundle\Util\HttpUtil;
use AnzuSystems\AuthBundle\Util\JwtUtil;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;

final class HttpUtilTest extends TestCase
{
private HttpUtil $httpUtil;
private JwtConfiguration $jwtConfiguration;
private JwtUtil $jwtUtil;

protected function setUp(): void
{
$this->jwtConfiguration = new JwtConfiguration(
audience: 'anz',
algorithm: 'ES256',
publicCert: base64_decode('LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZrd0V3WUhLb1pJemowQ0FRWUlLb1pJemowREFRY0RRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTQp5eFNiQm1DK0hSYWF2Z1dLM25aNG1HNFlmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==', true),
privateCert: base64_decode('LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSU9OdDBIdEdzUGdRRytKY2VGUk5GdlRZMVVVeDVITTdqQzNVS1ZHRHBlS0tvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTXl4U2JCbUMrSFJhYXZnV0szblo0bUc0WQpmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=', true),
lifetime: 3_600
);
$this->jwtUtil = new JwtUtil($this->jwtConfiguration);

$cookieConfiguration = new CookieConfiguration(
domain: '.example.com',
secure: true,
jwtPayloadCookieName: 'qux',
jwtSignatureCookieName: 'quux',
deviceIdCookieName: 'corge',
refreshTokenCookieName: 'garply',
refreshTokenExistenceCookieName: 'grault',
refreshTokenLifetime: 3600,
);

$this->httpUtil = new HttpUtil(
cookieConfiguration: $cookieConfiguration,
jwtConfiguration: $this->jwtConfiguration,
authRedirectDefaultUrl: 'https://example.com',
authRedirectQueryUrlAllowedPattern: '^https?://(.*)\.example\.com$'
);
}

public function testStoreJwtOnResponse(): void
{
$response = new Response();
$token = $this->jwtUtil->create('123');
$expireAt = new DateTimeImmutable(sprintf('+%d seconds', $this->jwtConfiguration->getLifetime()));
$this->httpUtil->storeJwtOnResponse($response, $token, $expireAt);

$cookies = $response->headers->getCookies();
$this->assertCount(2, $cookies);

/** @var Cookie $payloadCookie */
$payloadCookie = current(
array_filter($cookies, static function (Cookie $cookie) {
return $cookie->getName() === 'qux';
})
);

/** @var Cookie $signCookie */
$signCookie = current(
array_filter($cookies, static function (Cookie $cookie) {
return $cookie->getName() === 'quux';
})
);

$this->assertSame($token->toString(), $payloadCookie->getValue() . '.' . $signCookie->getValue());
$this->assertTrue($payloadCookie->isSecure());
$this->assertFalse($payloadCookie->isHttpOnly());
$this->assertSame('.example.com', $payloadCookie->getDomain());

$this->assertTrue($signCookie->isSecure());
$this->assertTrue($signCookie->isHttpOnly());
$this->assertSame('.example.com', $signCookie->getDomain());
}
}
4 changes: 2 additions & 2 deletions tests/Util/JwtUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protected function setUp(): void
$this->jwtConfiguration = new JwtConfiguration(
audience: 'anz',
algorithm: 'ES256',
publicCert: base64_decode('LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZrd0V3WUhLb1pJemowQ0FRWUlLb1pJemowREFRY0RRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTQp5eFNiQm1DK0hSYWF2Z1dLM25aNG1HNFlmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg=='),
privateCert: base64_decode('LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSU9OdDBIdEdzUGdRRytKY2VGUk5GdlRZMVVVeDVITTdqQzNVS1ZHRHBlS0tvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTXl4U2JCbUMrSFJhYXZnV0szblo0bUc0WQpmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo='),
publicCert: base64_decode('LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZrd0V3WUhLb1pJemowQ0FRWUlLb1pJemowREFRY0RRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTQp5eFNiQm1DK0hSYWF2Z1dLM25aNG1HNFlmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==', true),
privateCert: base64_decode('LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSU9OdDBIdEdzUGdRRytKY2VGUk5GdlRZMVVVeDVITTdqQzNVS1ZHRHBlS0tvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFT0hIQzMvVDZ3cnVNTk40OTBqVE1maXNFa1BoTXl4U2JCbUMrSFJhYXZnV0szblo0bUc0WQpmVDRxMmF1L3V4TktBTjJvODJOdW84VTQ0ZXZkcExkQUhBPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=', true),
lifetime: 3_600
);
$this->jwtUtil = new JwtUtil($this->jwtConfiguration);
Expand Down
3 changes: 1 addition & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

declare(strict_types=1);

require dirname(__DIR__).'/vendor/autoload.php';

require dirname(__DIR__) . '/vendor/autoload.php';