Skip to content

Commit

Permalink
Trailing chars (Spomky-Labs#116)
Browse files Browse the repository at this point in the history
* Auto remove trailing "=" + test
* Doc
  • Loading branch information
Spomky authored Oct 26, 2018
1 parent 1928325 commit 4974ef2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/Customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $mySecret = trim(Base32::encodeUpper(random_bytes(128)), '='); // We generate ou
$otp = TOTP::create($mySecret);
```

*Please note that the trailing `=` are automatically removed by the library.*

## Period and Counter

By default, the period for a TOTP is 30 seconds and the counter for a HOTP is 0.
Expand Down
4 changes: 2 additions & 2 deletions src/ParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ protected function getParameterMap(): array
},
'secret' => function ($value) {
if (null === $value) {
$value = trim(Base32::encodeUpper(random_bytes(64)), '=');
$value = Base32::encodeUpper(random_bytes(64));
}
$value = strtoupper($value);
$value = trim(strtoupper($value), '=');

return $value;
},
Expand Down
10 changes: 10 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,14 @@ public function testTOTPLoadWithoutIssuer()
$this->assertFalse($result->isIssuerIncludedAsParameter());
$this->assertEquals($otp, $result->getProvisioningUri());
}

public function testTOTPLoadAndRemoveSecretTrailingCharacters()
{
$uri = 'otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ%3D%3D';
$totp = Factory::loadFromProvisioningUri($uri);

$this->assertInstanceOf('\OTPHP\TOTP', $totp);
$this->assertEquals('JDDK4U6G3BJLEQ', $totp->getSecret());
$this->assertEquals('otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ', $totp->getProvisioningUri());
}
}

0 comments on commit 4974ef2

Please sign in to comment.