Skip to content

Commit 4974ef2

Browse files
authored
Trailing chars (#116)
* Auto remove trailing "=" + test * Doc
1 parent 1928325 commit 4974ef2

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/Customize.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $mySecret = trim(Base32::encodeUpper(random_bytes(128)), '='); // We generate ou
2424
$otp = TOTP::create($mySecret);
2525
```
2626

27+
*Please note that the trailing `=` are automatically removed by the library.*
28+
2729
## Period and Counter
2830

2931
By default, the period for a TOTP is 30 seconds and the counter for a HOTP is 0.

src/ParameterTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ protected function getParameterMap(): array
205205
},
206206
'secret' => function ($value) {
207207
if (null === $value) {
208-
$value = trim(Base32::encodeUpper(random_bytes(64)), '=');
208+
$value = Base32::encodeUpper(random_bytes(64));
209209
}
210-
$value = strtoupper($value);
210+
$value = trim(strtoupper($value), '=');
211211

212212
return $value;
213213
},

tests/FactoryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,14 @@ public function testTOTPLoadWithoutIssuer()
140140
$this->assertFalse($result->isIssuerIncludedAsParameter());
141141
$this->assertEquals($otp, $result->getProvisioningUri());
142142
}
143+
144+
public function testTOTPLoadAndRemoveSecretTrailingCharacters()
145+
{
146+
$uri = 'otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ%3D%3D';
147+
$totp = Factory::loadFromProvisioningUri($uri);
148+
149+
$this->assertInstanceOf('\OTPHP\TOTP', $totp);
150+
$this->assertEquals('JDDK4U6G3BJLEQ', $totp->getSecret());
151+
$this->assertEquals('otpauth://totp/My%20Test%20-%20Auth?secret=JDDK4U6G3BJLEQ', $totp->getProvisioningUri());
152+
}
143153
}

0 commit comments

Comments
 (0)