Skip to content

Commit

Permalink
Switch to ramsey/uuid for token generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 16, 2023
1 parent 561b11e commit 4c5138b
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 17 deletions.
1 change: 1 addition & 0 deletions application/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^2.9",
"firebase/php-jwt": "^5.4",
"ramsey/uuid": "^4.7",
"symfony/console": "5.3.*",
"symfony/dotenv": "5.3.*",
"symfony/flex": "^1.3.1",
Expand Down
239 changes: 238 additions & 1 deletion application/composer.lock

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

2 changes: 1 addition & 1 deletion application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function login(string $serverID, Request $request) : JsonResponse {
$response['refresh_token'] = $token->getRefreshToken();
}

$token->setAccessToken($this->generateToken('access-token'));
$token->setAccessToken($this->generateToken());
$entityManager->persist($token);
$entityManager->flush();

Expand Down
20 changes: 5 additions & 15 deletions application/src/Traits/GeneralTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Traits;

use Symfony\Component\HttpFoundation\Request;
use App\Service\ApiCheck;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

trait GeneralTrait {
Expand Down Expand Up @@ -43,21 +44,10 @@ public function authHttpCheck(array $requestMethod, Request $request, bool $doAu
/**
* Generates a unique token.
*
* @param string $extra
* @return string
*/
private function generateToken(string $extra = null): string {
$string = hash('sha256', $extra.date("Ymdhms"));
$token = null;
$previousPosition = 0;
for ($i = 0; $i < strlen($string); $i++) {
$randomDashedPosition = (int)rand(1, 10);
if (($randomDashedPosition > 3) && (($i % $randomDashedPosition) === 0)) {
$previousPosition = (int)($previousPosition + $randomDashedPosition);
$token = substr_replace($token ?? $string, '-', $previousPosition, 1);
}
}
return $token;
private function generateToken(): string {
return Uuid::uuid4()->toString();
}

/**
Expand Down Expand Up @@ -178,4 +168,4 @@ private function loginIdentifierType(object $identifier = null): array {
}
return $response;
}
}
}

0 comments on commit 4c5138b

Please sign in to comment.