| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace OTPHP; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use OTPHP\Exception\InvalidProvisioningUriException; |
| 9 |
use Psr\Clock\ClockInterface; |
| 10 |
use function sprintf; |
| 11 |
use Throwable; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class is used to load OTP object from a provisioning Uri. |
| 15 |
* |
| 16 |
* @readonly |
| 17 |
* |
| 18 |
* @see \OTPHP\Test\FactoryTest |
| 19 |
*/ |
| 20 |
final class Factory implements FactoryInterface |
| 21 |
{ |
| 22 |
public static function loadFromProvisioningUri(string $uri, ?ClockInterface $clock = null): OTPInterface |
| 23 |
{ |
| 24 |
try { |
| 25 |
$parsed_url = Url::fromString($uri); |
| 26 |
$parsed_url->getScheme() === 'otpauth' || throw new InvalidProvisioningUriException('Invalid scheme.'); |
| 27 |
} catch (Throwable $throwable) { |
| 28 |
throw new InvalidProvisioningUriException( |
| 29 |
'Not a valid OTP provisioning URI', |
| 30 |
$throwable->getCode(), |
| 31 |
$throwable |
| 32 |
); |
| 33 |
} |
| 34 |
if ($clock === null) { |
| 35 |
trigger_deprecation( |
| 36 |
'spomky-labs/otphp', |
| 37 |
'11.3.0', |
| 38 |
'The parameter "$clock" will become mandatory in 12.0.0. Please set a valid PSR Clock implementation instead of "null".' |
| 39 |
); |
| 40 |
$clock = new InternalClock(); |
| 41 |
} |
| 42 |
|
| 43 |
try { |
| 44 |
$otp = self::createOTP($parsed_url, $clock); |
| 45 |
|
| 46 |
self::populateOTP($otp, $parsed_url); |
| 47 |
} catch (InvalidProvisioningUriException $exception) { |
| 48 |
throw $exception; |
| 49 |
} catch (Throwable $throwable) { |
| 50 |
throw new InvalidProvisioningUriException( |
| 51 |
'Not a valid OTP provisioning URI', |
| 52 |
$throwable->getCode(), |
| 53 |
$throwable |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
return $otp; |
| 58 |
} |
| 59 |
|
| 60 |
private static function populateParameters(OTPInterface $otp, Url $data): void |
| 61 |
{ |
| 62 |
foreach ($data->getQuery() as $key => $value) { |
| 63 |
$otp->setParameter($key, $value); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private static function populateOTP(OTPInterface $otp, Url $data): void |
| 68 |
{ |
| 69 |
self::populateParameters($otp, $data); |
| 70 |
$result = explode(':', rawurldecode(substr($data->getPath(), 1))); |
| 71 |
|
| 72 |
if (count($result) < 2) { |
| 73 |
$otp->setIssuerIncludedAsParameter(false); |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$issuerFromLabel = $result[0]; |
| 79 |
$issuerFromParameter = $otp->getIssuer(); |
| 80 |
|
| 81 |
if ($issuerFromParameter !== null) { |
| 82 |
// Issuer parameter takes precedence over issuer in label |
| 83 |
// According to Google Authenticator spec: "they should be equal" but not required to be |
| 84 |
$otp->setIssuerIncludedAsParameter(true); |
| 85 |
} else { |
| 86 |
// No issuer parameter, use the issuer from label |
| 87 |
$issuerFromLabel !== '' || throw new InvalidProvisioningUriException( |
| 88 |
'Issuer from label must not be empty.' |
| 89 |
); |
| 90 |
$otp->setIssuer($issuerFromLabel); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private static function createOTP(Url $parsed_url, ClockInterface $clock): OTPInterface |
| 95 |
{ |
| 96 |
switch ($parsed_url->getHost()) { |
| 97 |
case 'totp': |
| 98 |
$totp = TOTP::createFromSecret($parsed_url->getSecret(), $clock); |
| 99 |
$totp->setLabel(self::getLabel($parsed_url->getPath())); |
| 100 |
|
| 101 |
return $totp; |
| 102 |
case 'hotp': |
| 103 |
$hotp = HOTP::createFromSecret($parsed_url->getSecret()); |
| 104 |
$hotp->setLabel(self::getLabel($parsed_url->getPath())); |
| 105 |
|
| 106 |
return $hotp; |
| 107 |
default: |
| 108 |
throw new InvalidProvisioningUriException(sprintf('Unsupported "%s" OTP type', $parsed_url->getHost())); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param non-empty-string $data |
| 114 |
* @return non-empty-string |
| 115 |
*/ |
| 116 |
private static function getLabel(string $data): string |
| 117 |
{ |
| 118 |
$result = explode(':', rawurldecode(substr($data, 1))); |
| 119 |
$label = count($result) === 2 ? $result[1] : $result[0]; |
| 120 |
$label !== '' || throw new InvalidProvisioningUriException('Label must not be empty.'); |
| 121 |
|
| 122 |
return $label; |
| 123 |
} |
| 124 |
} |