Skip to content
Open
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
26 changes: 23 additions & 3 deletions src/OTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,37 @@ protected function filterOptions(array &$options): void
*/
protected function generateURI(string $type, array $options): string
{
$issuer = $this->getIssuer();
$label = $this->getLabel();
is_string($label) || throw new InvalidArgumentException('The label is not set.');
$this->hasColon($label) === false || throw new InvalidArgumentException('Label must not contain a colon.');

if (null !== $issuer && !$issuer) {
throw new InvalidArgumentException('Issuer must not be an empty string');
}

if (null !== $label && !$label) {
throw new InvalidArgumentException('Label must not be an empty string');
}

if (!$issuer && !$label) {
throw new InvalidArgumentException('The label is not set. Either label or issuer must be set.');
}

if ($label && $this->hasColon($label)) {
throw new InvalidArgumentException('Label must not contain a colon.');
}

$options = [...$options, ...$this->getParameters()];
$this->filterOptions($options);
$params = str_replace(['+', '%7E'], ['%20', '~'], http_build_query($options, '', '&'));

return sprintf(
'otpauth://%s/%s?%s',
$type,
rawurlencode(($this->getIssuer() !== null ? $this->getIssuer() . ':' : '') . $label),
rawurlencode(match (true) {
null !== $issuer && null !== $label => $issuer.':'.$label,
null !== $issuer => $issuer,
default => $label,
}),
$params
);
}
Expand Down