Skip to content

Commit 05df474

Browse files
committed
:octocat: added setRawSecret() and getRawSecret() methods
(cherry picked from commit 9194706) # Conflicts: # src/Authenticators/AuthenticatorAbstract.php # tests/Authenticators/AuthenticatorInterfaceTestAbstract.php # tests/Authenticators/SteamGuardTest.php
1 parent c43bcf9 commit 05df474

5 files changed

Lines changed: 93 additions & 8 deletions

File tree

src/Authenticator.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function setOptions(SettingsContainerInterface $options):self{
6666
}
6767

6868
/**
69-
* Sets a secret phrase from a Base32 representation
69+
* Sets a secret phrase from an encoded representation
7070
*
7171
* @codeCoverageIgnore
7272
*/
@@ -77,14 +77,34 @@ public function setSecret(string $encodedSecret):self{
7777
}
7878

7979
/**
80-
* Returns a Base32 representation of the current secret phrase
80+
* Sets a secret phrase from a a raw binary representation
81+
*
82+
* @codeCoverageIgnore
83+
*/
84+
public function setRawSecret(string $rawSecret):self{
85+
$this->authenticator->setRawSecret($rawSecret);
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* Returns an encoded representation of the current secret phrase
8192
*
8293
* @codeCoverageIgnore
8394
*/
8495
public function getSecret():string{
8596
return $this->authenticator->getSecret();
8697
}
8798

99+
/**
100+
* Returns the raw representation of the current secret phrase
101+
*
102+
* @codeCoverageIgnore
103+
*/
104+
public function getRawSecret():string{
105+
return $this->authenticator->getRawSecret();
106+
}
107+
88108
/**
89109
* Generates a new (secure random) secret phrase
90110
*

src/Authenticators/AuthenticatorAbstract.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public function setSecret(string $encodedSecret):AuthenticatorInterface{
5454
return $this;
5555
}
5656

57+
public function setRawSecret(string $rawSecret):AuthenticatorInterface{
58+
59+
if($rawSecret === ''){
60+
throw new InvalidArgumentException('The given secret string is empty');
61+
}
62+
63+
$this->secret = $rawSecret;
64+
65+
return $this;
66+
}
67+
5768
public function getSecret():string{
5869

5970
if($this->secret === null){
@@ -63,6 +74,15 @@ public function getSecret():string{
6374
return Base32::encode($this->secret);
6475
}
6576

77+
public function getRawSecret():string{
78+
79+
if($this->secret === null){
80+
throw new RuntimeException('No secret set');
81+
}
82+
83+
return $this->secret;
84+
}
85+
6686
public function createSecret(?int $length = null):string{
6787
$length ??= $this->options->secret_length;
6888

src/Authenticators/AuthenticatorInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,27 @@ public function setOptions(SettingsContainerInterface $options):AuthenticatorInt
5454
*/
5555
public function setSecret(string $encodedSecret):AuthenticatorInterface;
5656

57+
/**
58+
* Sets a secret phrase from a a raw binary representation
59+
*
60+
* @throws \RuntimeException
61+
*/
62+
public function setRawSecret(string $rawSecret):AuthenticatorInterface;
63+
5764
/**
5865
* Returns an encoded representation of the current secret phrase
5966
*
6067
* @throws \RuntimeException
6168
*/
6269
public function getSecret():string;
6370

71+
/**
72+
* Returns the raw representation of the current secret phrase
73+
*
74+
* @throws \RuntimeException
75+
*/
76+
public function getRawSecret():string;
77+
6478
/**
6579
* Generates a new (secure random) secret phrase
6680
*

tests/Authenticators/AuthenticatorInterfaceTestAbstract.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,29 @@ public function testSetGetSecret():void{
4343
$this::assertSame($this::rawsecret, Base32::decode($secret));
4444
}
4545

46+
public function testSetGetRawSecret():void{
47+
$this->authenticatorInterface->setRawSecret($this::rawsecret);
48+
49+
$secret = $this->authenticatorInterface->getRawSecret();
50+
51+
$this::assertSame($this::secret, Base32::encode($secret));
52+
$this::assertSame($this::rawsecret, $secret);
53+
}
54+
4655
public function testSetEmptySecretException():void{
4756
$this->expectException(InvalidArgumentException::class);
4857
$this->expectExceptionMessage('The given secret string is empty');
4958

5059
$this->authenticatorInterface->setSecret('');
5160
}
5261

62+
public function testSetEmptyRawSecretException():void{
63+
$this->expectException(InvalidArgumentException::class);
64+
$this->expectExceptionMessage('The given secret string is empty');
65+
66+
$this->authenticatorInterface->setRawSecret('');
67+
}
68+
5369
public function testSetInvalidSecretException():void{
5470
$this->expectException(InvalidArgumentException::class);
5571

@@ -63,6 +79,13 @@ public function testGetSecretException():void{
6379
$this->authenticatorInterface->getSecret();
6480
}
6581

82+
public function testGetRawSecretException():void{
83+
$this->expectException(RuntimeException::class);
84+
$this->expectExceptionMessage('No secret set');
85+
86+
$this->authenticatorInterface->getRawSecret();
87+
}
88+
6689
public function testCreateSecretDefaultLength():void{
6790
$this::assertSame(
6891
$this->options->secret_length,

tests/Authenticators/SteamGuardTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ class SteamGuardTest extends AuthenticatorInterfaceTestAbstract{
2828

2929
protected const secret = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=';
3030

31+
/**
32+
* Timestamps and -slices from the RFC6238 page, codes from a verified implementation
33+
*
34+
* @see https://tools.ietf.org/html/rfc6238#page-14
35+
*/
3136
protected const SteamGuardVectors = [
32-
// timestamps and time slices from RFC 6238, see https://tools.ietf.org/html/rfc6238#page-14
3337
[ 59, '1', 'PV9M4'],
3438
[ 1111111109, '23523ec', 'PY4YB'],
3539
[ 1111111111, '23523ed', '5PP3V'],
@@ -52,6 +56,15 @@ public function testSetGetSecret():void{
5256
$this::assertSame($this::rawsecret, Base64::decode($secret));
5357
}
5458

59+
public function testSetGetRawSecret():void{
60+
$this->authenticatorInterface->setRawSecret($this::rawsecret);
61+
62+
$secret = $this->authenticatorInterface->getRawSecret();
63+
64+
$this::assertSame($this::secret, Base64::encode($secret));
65+
$this::assertSame($this::rawsecret, $secret);
66+
}
67+
5568
public function testCreateSecretDefaultLength():void{
5669
$this::assertSame(
5770
$this->options->secret_length,
@@ -75,11 +88,6 @@ public function testCreateSecretCheckCharacterSet():void{
7588
$this::assertMatchesRegularExpression('#^['.Base64::CHARSET.']+$#', $secret);
7689
}
7790

78-
/**
79-
* Timestamps and -slices from the RFC6238 page, codes from a verified implementation
80-
*
81-
* @see https://tools.ietf.org/html/rfc6238#page-14
82-
*/
8391
public static function steamGuardVectors():Generator{
8492
foreach(self::SteamGuardVectors as [$timestamp, $timeslice, $totp]){
8593
// skip 64bit numbers on 32bit PHP

0 commit comments

Comments
 (0)