|
|
|
@ -9,77 +9,42 @@ use BradyMcD\TAATP\URI\Otpauth;
|
|
|
|
/** @SuppressWarnings(PHPMD.StaticAccess)*/
|
|
|
|
/** @SuppressWarnings(PHPMD.StaticAccess)*/
|
|
|
|
final class URITest extends TestCase
|
|
|
|
final class URITest extends TestCase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public function testRejectsInvalidUri(): void
|
|
|
|
private static $defaults;
|
|
|
|
{
|
|
|
|
private static $exampleUri;
|
|
|
|
$string = 'totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
|
|
|
|
private static $exampleComponents;
|
|
|
|
|
|
|
|
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Otpauth::fromString($string);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function setUpBeforeClass(): void
|
|
|
|
public function testRejectsMissingSecretInformation(): void
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$string = 'otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
|
|
|
|
self::$defaults = Otpauth::DEFAULTS;
|
|
|
|
|
|
|
|
self::$exampleUri = 'otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
\parse_str(\parse_url(self::$exampleUri)['query'], self::$exampleComponents);
|
|
|
|
|
|
|
|
|
|
|
|
Otpauth::fromString($string);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testPreservesAllUriFields(): void
|
|
|
|
/** @SuppressWarnings(PHPMD.EmptyCatchBlock)*/
|
|
|
|
|
|
|
|
public function testRejectsInvalidUris(): void
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$string = 'otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
|
|
|
|
$invalids = [
|
|
|
|
|
|
|
|
"https://www.example.org",
|
|
|
|
$queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ",
|
|
|
|
"Just a normal string",
|
|
|
|
"issuer" => "ACME Co",
|
|
|
|
"otpauth://totp/NOSECRETS:john.doe@email.com?issuer=ACME%20Co&algorithm=MD5&digits=8&period=60",
|
|
|
|
"algorithm" => "MD5",
|
|
|
|
"otpauth://totp/MISS:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=MATCH&algorithm=MD5&digits=8&period=60",
|
|
|
|
"digits" => 8,
|
|
|
|
"otpauth://totp/vendor:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=vendohr&algorithm=NotAnAlgo&digits=8&period=60",
|
|
|
|
"period" => 60,];
|
|
|
|
"",
|
|
|
|
|
|
|
|
|
|
|
|
$provisioningUri = Otpauth::fromString($string);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->issuer, $queryComponents['issuer']);
|
|
|
|
];
|
|
|
|
$this->assertSame($provisioningUri->digits, $queryComponents['digits']);
|
|
|
|
$pUri;
|
|
|
|
$this->assertSame($provisioningUri->period, $queryComponents['period']);
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->algorithm, $queryComponents['algorithm']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$calculatedUrl = $provisioningUri->emitStr();
|
|
|
|
foreach ($invalids as $invalid)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
|
|
|
|
|
|
|
$parsedOtp = \parse_url($calculatedUrl);
|
|
|
|
$pUri = Otpauth::fromString($invalid);
|
|
|
|
$parsedTest = \parse_url($string);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$parsedOtpQuery = [];
|
|
|
|
$this->assertNull($pUri);
|
|
|
|
$parsedTestQuery = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
\parse_str($parsedOtp['query'], $parsedOtpQuery);
|
|
|
|
|
|
|
|
unset($parsedOtp['query']);
|
|
|
|
|
|
|
|
\parse_str($parsedTest['query'], $parsedTestQuery);
|
|
|
|
|
|
|
|
unset($parsedTest['query']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEqualsCanonicalizing($parsedOtp, $parsedTest);
|
|
|
|
|
|
|
|
$this->assertEqualsCanonicalizing($parsedOtpQuery, $parsedTestQuery);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCanFallbackToDefaults(): void
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$string = 'otpauth://totp/Example:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ';
|
|
|
|
|
|
|
|
$queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ",
|
|
|
|
|
|
|
|
"issuer" => "Example",
|
|
|
|
|
|
|
|
"algorithm" => "SHA1",
|
|
|
|
|
|
|
|
"digits" => 6,
|
|
|
|
|
|
|
|
"period" => 30,];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$provisioningUri = Otpauth::fromString($string);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->algorithm, $queryComponents['algorithm']);
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->digits, $queryComponents['digits']);
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->period, $queryComponents['period']);
|
|
|
|
|
|
|
|
$this->assertSame($provisioningUri->issuer, $queryComponents['issuer']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|
|
|
|
?>
|
|
|
|
|