Ensure auto-generated name API keys do not throw duplicated name

This commit is contained in:
Alejandro Celaya
2024-11-09 12:07:07 +01:00
parent d228b88e51
commit 3c6f12aec6
5 changed files with 53 additions and 12 deletions

View File

@@ -18,12 +18,13 @@ final readonly class ApiKeyMeta
private function __construct(
public string $key,
public string $name,
public bool $isNameAutoGenerated,
public Chronos|null $expirationDate,
public iterable $roleDefinitions,
) {
}
public static function empty(): self
public static function create(): self
{
return self::fromParams();
}
@@ -38,9 +39,10 @@ final readonly class ApiKeyMeta
iterable $roleDefinitions = [],
): self {
$resolvedKey = $key ?? Uuid::uuid4()->toString();
$isNameAutoGenerated = empty($name);
// If a name was not provided, fall back to the key
if (empty($name)) {
if ($isNameAutoGenerated) {
// If the key was auto-generated, fall back to a redacted version of the UUID, otherwise simply use the
// plain key as fallback name
$name = $key === null
@@ -51,6 +53,7 @@ final readonly class ApiKeyMeta
return new self(
key: $resolvedKey,
name: $name,
isNameAutoGenerated: $isNameAutoGenerated,
expirationDate: $expirationDate,
roleDefinitions: $roleDefinitions,
);