Skip to content

Commit

Permalink
Merge pull request #25 from infocyph/feature/enhancement
Browse files Browse the repository at this point in the history
Additional params
  • Loading branch information
abmmhasan authored Aug 3, 2024
2 parents fb82b81 + 9517192 commit bb001aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ $secret = \Infocyph\OTP\HOTP::generateSecret();
> The `getProvisioningUriQR` & `getProvisioningUri` accepts 3rd parameter, where it takes array of parameters
`['algorithm', 'digits', 'period', 'counter']`. Problem you might encounter, with the URI/Image is that most of the
OTP generator might not support all of those options. In that case, passing in a blank array will remove all the optional
keys, or you can pass in selective parameters as you need.
keys, or you can pass in selective parameters as you need. Additionally, you can also pass in additional parameter to reflect
in URI string or QR image in 4th parameter. But be cautious that, it might not be supported by the Client Apps.

- Get current OTP for a given counter
```php
Expand Down Expand Up @@ -115,8 +116,9 @@ $secret = \Infocyph\OTP\TOTP::generateSecret();
```
> The `getProvisioningUriQR` & `getProvisioningUri` accepts 3rd parameter, where it takes array of parameters
`['algorithm', 'digits', 'period', 'counter']`. Problem you might encounter, with the URI/Image is that most of the
OTP generators might not support all of those options. In that case, passing in a blank array will remove all the optional
keys, or you can pass in selective parameters as you need.
OTP generator might not support all of those options. In that case, passing in a blank array will remove all the optional
keys, or you can pass in selective parameters as you need. Additionally, you can also pass in additional parameter to reflect
in URI string or QR image in 4th parameter. But be cautious that, it might not be supported by the Client Apps.

- Get current OTP for a given counter
```php
Expand Down
40 changes: 21 additions & 19 deletions src/Traits/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,32 @@ private function getDigitsFromSuite(): int
* @param string $label The label for the provisioning URI.
* @param string $issuer The issuer for the provisioning URI.
* @param array $include An array of optional parameters to include in the provisioning URI. Default is ['algorithm', 'digits', 'period', 'counter'].
* @param array $additionalParameters An array of additional parameters to include in the provisioning URI.
* @return string The provisioning URI as a string.
*/
public function getProvisioningUri(
string $label,
string $issuer,
array $include = ['algorithm', 'digits', 'period', 'counter']
array $include = ['algorithm', 'digits', 'period', 'counter'],
array $additionalParameters = []
): string {
$include = array_flip($include);
$query = [
'secret' => $this->secret,
'issuer' => $issuer,
];

$query += match ($this->type) {
'ocra' => [
'ocraSuite' => $this->ocraSuiteString,
'algorithm' => isset($include['algorithm']) ? strtoupper($this->getAlgorithmFromSuite()) : null,
'digits' => isset($include['digits']) ? $this->getDigitsFromSuite() : null
],
default => [
'algorithm' => isset($include['algorithm']) ? $this->algorithm : null,
'digits' => isset($include['digits']) ? $this->digitCount : null,
'period' => $this->type === 'totp' && isset($include['period']) ? $this->period : null,
'counter' => isset($include['counter']) ? $this->counter : null
]
};
'secret' => $this->secret,
'issuer' => $issuer,
] + match ($this->type) {
'ocra' => [
'ocraSuite' => $this->ocraSuiteString,
'algorithm' => isset($include['algorithm']) ? strtoupper($this->getAlgorithmFromSuite()) : null,
'digits' => isset($include['digits']) ? $this->getDigitsFromSuite() : null
],
default => [
'algorithm' => isset($include['algorithm']) ? $this->algorithm : null,
'digits' => isset($include['digits']) ? $this->digitCount : null,
'period' => $this->type === 'totp' && isset($include['period']) ? $this->period : null,
'counter' => isset($include['counter']) ? $this->counter : null
]
} + $additionalParameters;

$queryString = http_build_query(
array_filter($query),
Expand All @@ -129,13 +129,15 @@ public function getProvisioningUri(
* @param string $label The label for the provisioning QR code.
* @param string $issuer The issuer for the provisioning QR code.
* @param array $include An array of optional parameters to include in the provisioning QR code. Default is ['algorithm', 'digits', 'period', 'counter'].
* @param array $additionalParameters An array of additional parameters to include in the provisioning URI.
* @param int $imageSize The size of the QR code image.
* @return string The provisioning QR code as SVG string.
*/
public function getProvisioningUriQR(
string $label,
string $issuer,
array $include = ['algorithm', 'digits', 'period', 'counter'],
array $additionalParameters = [],
int $imageSize = 200
): string {
$writer = new Writer(
Expand All @@ -144,7 +146,7 @@ public function getProvisioningUriQR(
new SvgImageBackEnd()
)
);
return $writer->writeString($this->getProvisioningUri($label, $issuer, $include));
return $writer->writeString($this->getProvisioningUri($label, $issuer, $include, $additionalParameters));
}

/**
Expand Down

0 comments on commit bb001aa

Please sign in to comment.