Skip to content

Commit

Permalink
feat: removed own UUID v4 implementation and add Symfony UID Component
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 15, 2024
1 parent 377d250 commit 3e3c7af
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 36 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"symfony/http-kernel": "^7.0",
"symfony/mailer": "^7.0",
"symfony/routing": "^7.0",
"symfony/uid": "^7.1",
"tecnickcom/tcpdf": "~6.0",
"tivie/htaccess-parser": "*",
"twig/intl-extra": "^3.10",
Expand Down
157 changes: 155 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 17 additions & 25 deletions phpmyfaq/src/phpMyFAQ/Auth/EntraId/Session.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
<?php

/**
* Session class for Entra ID.
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-11-02
*/

namespace phpMyFAQ\Auth\EntraId;

use Exception;
use phpMyFAQ\Configuration;
use phpMyFAQ\Session\AbstractSession;
use Random\RandomException;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
use Symfony\Component\Uid\Uuid;

class Session extends AbstractSession
{
Expand All @@ -35,7 +50,7 @@ public function __construct(private readonly Configuration $configuration, priva
*/
public function createCurrentSessionKey(): void
{
$this->currentSessionKey = $this->uuid();
$this->currentSessionKey = Uuid::v4();
}

/**
Expand Down Expand Up @@ -94,27 +109,4 @@ public function getCookie(string $name): string
$request = Request::createFromGlobals();
return $request->cookies->get($name, '');
}

/**
* Returns a UUID Version 4 compatible universally unique identifier.
*/
public function uuid(): string
{
try {
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
random_int(0, 0xffff),
random_int(0, 0xffff),
random_int(0, 0xffff),
random_int(0, 0x0fff) | 0x4000,
random_int(0, 0x3fff) | 0x8000,
random_int(0, 0xffff),
random_int(0, 0xffff),
random_int(0, 0xffff)
);
} catch (RandomException $e) {
$this->configuration->getLogger()->error('Cannot generate UUID: ' . $e->getMessage());
return '';
}
}
}
9 changes: 0 additions & 9 deletions tests/phpMyFAQ/Auth/EntraId/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,4 @@ public function testSetCurrentSessionKey(): void
$this->session->setCurrentSessionKey();
$this->assertNotNull($this->session->getCurrentSessionKey());
}

public function testUuid(): void
{
$uuid = $this->session->uuid();
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/',
$uuid
);
}
}

0 comments on commit 3e3c7af

Please sign in to comment.