Skip to content

Commit 87314ec

Browse files
committed
:octocat: introduce AuthenticatorInterface::MODE identifier and remove unnecessary property in Authenticator
(cherry picked from commit 6528215)
1 parent 3e958c3 commit 87314ec

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

src/Authenticator.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class Authenticator{
3434
/** @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\Authenticator\AuthenticatorOptions */
3535
protected SettingsContainerInterface $options;
3636
protected AuthenticatorInterface $authenticator;
37-
protected string $mode = AuthenticatorInterface::TOTP;
3837

3938
/**
4039
* Authenticator constructor
@@ -59,14 +58,11 @@ public function setOptions(SettingsContainerInterface $options):self{
5958
$this->options = $options;
6059

6160
// invoke a new authenticator interface if necessary
62-
if(!isset($this->authenticator) || $this->options->mode !== $this->mode){
61+
if(!isset($this->authenticator) || $this->authenticator::MODE !== $this->options->mode){
6362
$class = AuthenticatorInterface::MODES[$this->options->mode];
64-
$this->mode = $this->options->mode;
65-
$this->authenticator = new $class;
63+
$this->authenticator = new $class($this->options);
6664
}
6765

68-
$this->authenticator->setOptions($this->options);
69-
7066
return $this;
7167
}
7268

@@ -148,28 +144,31 @@ public function getUri(string $label, string $issuer, ?int $hotpCounter = null,
148144
'issuer' => $issuer,
149145
];
150146

151-
if(($omitSettings ?? $this->options->omitUriSettings) !== true){
152-
$values['digits'] = $this->options->digits;
153-
$values['algorithm'] = $this->options->algorithm;
147+
if($this->authenticator::MODE === AuthenticatorInterface::HOTP){
154148

155-
if($this->mode === AuthenticatorInterface::TOTP){
156-
$values['period'] = $this->options->period;
149+
if($hotpCounter === null || $hotpCounter < 0){
150+
throw new InvalidArgumentException('initial counter value must be set and greater or equal to 0');
157151
}
158152

153+
$values['counter'] = $hotpCounter;
159154
}
160155

161-
if($this->mode === AuthenticatorInterface::HOTP){
156+
if(($omitSettings ?? $this->options->omitUriSettings) !== true){
157+
$values['digits'] = $this->options->digits;
158+
$values['algorithm'] = $this->options->algorithm;
162159

163-
if($hotpCounter === null || $hotpCounter < 0){
164-
throw new InvalidArgumentException('initial counter value must be set and greater or equal to 0');
160+
if($this->authenticator::MODE === AuthenticatorInterface::TOTP){
161+
$values['period'] = $this->options->period;
165162
}
166163

167-
$values['counter'] = $hotpCounter;
168164
}
169165

170-
$values = http_build_query($values, '', '&', PHP_QUERY_RFC3986);
171-
172-
return sprintf('otpauth://%s/%s?%s', $this->mode, rawurlencode($label), $values);
166+
return sprintf(
167+
'otpauth://%s/%s?%s',
168+
$this->authenticator::MODE,
169+
rawurlencode($label),
170+
http_build_query($values, '', '&', PHP_QUERY_RFC3986),
171+
);
173172
}
174173

175174
}

src/Authenticators/AuthenticatorInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ interface AuthenticatorInterface{
3838
self::ALGO_SHA512,
3939
];
4040

41+
/**
42+
* Mode identifier. Do not call this constant from the interface. but rather from an authenticator instance.
43+
*
44+
* @var string
45+
*/
46+
public const MODE = '';
47+
4148
/**
4249
* Sets the options
4350
*/

src/Authenticators/HOTP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
class HOTP extends AuthenticatorAbstract{
2828

29+
public const MODE = self::HOTP;
30+
2931
/**
3032
* @inheritDoc
3133
*/

src/Authenticators/SteamGuard.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
final class SteamGuard extends TOTP{
4141

42+
public const MODE = self::STEAM_GUARD;
43+
4244
private const steamCodeChars = '23456789BCDFGHJKMNPQRTVWXY';
4345
private const steamTimeURL = 'https://api.steampowered.com/ITwoFactorService/QueryTime/v0001';
4446

src/Authenticators/TOTP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class TOTP extends HOTP{
2222

23+
public const MODE = self::TOTP;
24+
2325
/**
2426
* @inheritDoc
2527
*/

tests/AuthenticatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public function testGetUri():void{
6767
->setSecret(self::secret);
6868

6969
$this::assertSame(
70-
sprintf('otpauth://hotp/%s?secret=%s&issuer=%s&digits=8&algorithm=SHA1&counter=42', $label, self::secret, $issuer),
70+
sprintf('otpauth://hotp/%s?secret=%s&issuer=%s&counter=42&digits=8&algorithm=SHA1', $label, self::secret, $issuer),
7171
$this->authenticator->getUri(self::label, self::issuer, 42)
7272
);
7373

7474
$this->options->algorithm = AuthenticatorInterface::ALGO_SHA512;
7575
$this::assertSame(
76-
sprintf('otpauth://hotp/%s?secret=%s&issuer=%s&digits=8&algorithm=SHA512', $label, self::secret, $issuer),
77-
$this->authenticator->getUri(self::label, self::issuer)
76+
sprintf('otpauth://hotp/%s?secret=%s&issuer=%s&counter=0&digits=8&algorithm=SHA512', $label, self::secret, $issuer),
77+
$this->authenticator->getUri(self::label, self::issuer, 0)
7878
);
7979

8080
// test omit settings

0 commit comments

Comments
 (0)