Skip to content

Commit 6528215

Browse files
committed
:octocat: introduce AuthenticatorInterface::MODE identifier and remove unnecessary property in Authenticator
1 parent bf675cc commit 6528215

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

src/Authenticator.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class Authenticator{
3434

3535
protected SettingsContainerInterface|AuthenticatorOptions $options;
3636
protected AuthenticatorInterface $authenticator;
37-
protected string $mode = AuthenticatorInterface::TOTP;
3837

3938
/**
4039
* Authenticator constructor
@@ -62,8 +61,7 @@ public function setOptions(SettingsContainerInterface|AuthenticatorOptions $opti
6261
$this->options = $options;
6362

6463
// invoke a new authenticator interface if necessary
65-
if(!isset($this->authenticator) || $this->options->mode !== $this->mode){
66-
$this->mode = $this->options->mode;
64+
if(!isset($this->authenticator) || $this->authenticator::MODE !== $this->options->mode){
6765
$this->authenticator = new (AuthenticatorInterface::MODES[$this->options->mode])($this->options);
6866
}
6967

@@ -150,28 +148,31 @@ public function getUri(string $label, string $issuer, int|null $hotpCounter = nu
150148
'issuer' => $issuer,
151149
];
152150

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

157-
if($this->mode === AuthenticatorInterface::TOTP){
158-
$values['period'] = $this->options->period;
153+
if($hotpCounter === null || $hotpCounter < 0){
154+
throw new InvalidArgumentException('initial counter value must be set and greater or equal to 0');
159155
}
160156

157+
$values['counter'] = $hotpCounter;
161158
}
162159

163-
if($this->mode === AuthenticatorInterface::HOTP){
160+
if(($omitSettings ?? $this->options->omitUriSettings) !== true){
161+
$values['digits'] = $this->options->digits;
162+
$values['algorithm'] = $this->options->algorithm;
164163

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

169-
$values['counter'] = $hotpCounter;
170168
}
171169

172-
$values = http_build_query($values, '', '&', PHP_QUERY_RFC3986);
173-
174-
return sprintf('otpauth://%s/%s?%s', $this->mode, rawurlencode($label), $values);
170+
return sprintf(
171+
'otpauth://%s/%s?%s',
172+
$this->authenticator::MODE,
173+
rawurlencode($label),
174+
http_build_query($values, '', '&', PHP_QUERY_RFC3986),
175+
);
175176
}
176177

177178
}

src/Authenticators/AuthenticatorInterface.php

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

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

src/Authenticators/HOTP.php

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

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

src/Authenticators/SteamGuard.php

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

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

src/Authenticators/TOTP.php

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

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

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)