Skip to content

Commit 9194706

Browse files
committed
:octocat: added setRawSecret() and getRawSecret() methods
1 parent 4fda0bf commit 9194706

5 files changed

Lines changed: 98 additions & 8 deletions

File tree

src/Authenticator.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function setOptions(SettingsContainerInterface|AuthenticatorOptions|itera
7474
}
7575

7676
/**
77-
* Sets a secret phrase from a Base32 representation
77+
* Sets a secret phrase from an encoded representation
7878
*
7979
* @codeCoverageIgnore
8080
*/
@@ -85,14 +85,34 @@ public function setSecret(#[SensitiveParameter] string $encodedSecret):static{
8585
}
8686

8787
/**
88-
* Returns a Base32 representation of the current secret phrase
88+
* Sets a secret phrase from a a raw binary representation
89+
*
90+
* @codeCoverageIgnore
91+
*/
92+
public function setRawSecret(#[SensitiveParameter] string $rawSecret):static{
93+
$this->authenticator->setRawSecret($rawSecret);
94+
95+
return $this;
96+
}
97+
98+
/**
99+
* Returns an encoded representation of the current secret phrase
89100
*
90101
* @codeCoverageIgnore
91102
*/
92103
public function getSecret():string{
93104
return $this->authenticator->getSecret();
94105
}
95106

107+
/**
108+
* Returns the raw representation of the current secret phrase
109+
*
110+
* @codeCoverageIgnore
111+
*/
112+
public function getRawSecret():string{
113+
return $this->authenticator->getRawSecret();
114+
}
115+
96116
/**
97117
* Generates a new (secure random) secret phrase
98118
*

src/Authenticators/AuthenticatorAbstract.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public function setSecret(#[SensitiveParameter] string $encodedSecret):static{
5959
return $this;
6060
}
6161

62+
public function setRawSecret(#[SensitiveParameter] string $rawSecret):static{
63+
64+
if($rawSecret === ''){
65+
throw new InvalidArgumentException('The given secret string is empty');
66+
}
67+
68+
$this->secret = $rawSecret;
69+
70+
return $this;
71+
}
72+
6273
public function getSecret():string{
6374

6475
if($this->secret === null){
@@ -68,6 +79,16 @@ public function getSecret():string{
6879
return Base32::encode($this->secret);
6980
}
7081

82+
public function getRawSecret():string{
83+
84+
if($this->secret === null){
85+
throw new RuntimeException('No secret set');
86+
}
87+
88+
return $this->secret;
89+
}
90+
91+
7192
public function createSecret(int|null $length = null):string{
7293
$length ??= $this->options->secret_length;
7394

src/Authenticators/AuthenticatorInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,27 @@ public function setOptions(SettingsContainerInterface $options):static;
5555
*/
5656
public function setSecret(#[SensitiveParameter] string $encodedSecret):static;
5757

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

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

tests/Authenticators/AuthenticatorInterfaceTestAbstract.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function setGetSecret():void{
4747
$this::assertSame($this::rawsecret, Base32::decode($secret));
4848
}
4949

50+
#[Test]
51+
public function setGetRawSecret():void{
52+
$this->authenticatorInterface->setRawSecret($this::rawsecret);
53+
54+
$secret = $this->authenticatorInterface->getRawSecret();
55+
56+
$this::assertSame($this::secret, Base32::encode($secret));
57+
$this::assertSame($this::rawsecret, $secret);
58+
}
59+
5060
#[Test]
5161
public function setEmptySecretException():void{
5262
$this->expectException(InvalidArgumentException::class);
@@ -55,6 +65,14 @@ public function setEmptySecretException():void{
5565
$this->authenticatorInterface->setSecret('');
5666
}
5767

68+
#[Test]
69+
public function setEmptyRawSecretException():void{
70+
$this->expectException(InvalidArgumentException::class);
71+
$this->expectExceptionMessage('The given secret string is empty');
72+
73+
$this->authenticatorInterface->setRawSecret('');
74+
}
75+
5876
#[Test]
5977
public function setInvalidSecretException():void{
6078
$this->expectException(InvalidArgumentException::class);
@@ -70,6 +88,14 @@ public function getSecretException():void{
7088
$this->authenticatorInterface->getSecret();
7189
}
7290

91+
#[Test]
92+
public function getRawSecretException():void{
93+
$this->expectException(RuntimeException::class);
94+
$this->expectExceptionMessage('No secret set');
95+
96+
$this->authenticatorInterface->getRawSecret();
97+
}
98+
7399
#[Test]
74100
public function createSecretDefaultLength():void{
75101
$this::assertSame(

tests/Authenticators/SteamGuardTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ class SteamGuardTest extends AuthenticatorInterfaceTestAbstract{
2626

2727
protected const secret = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=';
2828

29+
/**
30+
* Timestamps and -slices from the RFC6238 page, codes from a verified implementation
31+
*
32+
* @see https://tools.ietf.org/html/rfc6238#page-14
33+
*/
2934
protected const SteamGuardVectors = [
30-
// timestamps and time slices from RFC 6238, see https://tools.ietf.org/html/rfc6238#page-14
3135
[ 59, '1', 'PV9M4'],
3236
[ 1111111109, '23523ec', 'PY4YB'],
3337
[ 1111111111, '23523ed', '5PP3V'],
@@ -51,6 +55,16 @@ public function setGetSecret():void{
5155
$this::assertSame($this::rawsecret, Base64::decode($secret));
5256
}
5357

58+
#[Test]
59+
public function setGetRawSecret():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+
5468
#[Test]
5569
public function createSecretDefaultLength():void{
5670
$this::assertSame(
@@ -77,11 +91,6 @@ public function createSecretCheckCharacterSet():void{
7791
$this::assertMatchesRegularExpression('#^['.Base64::CHARSET.']+$#', $secret);
7892
}
7993

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

0 commit comments

Comments
 (0)