Skip to content

Commit 3b6e7cf

Browse files
committed
Make getGoogleAuthenticatorUsername and getTotpAuthenticationUsername nullable, #293
No need to return an empty string, can return null to have a record without label in the TOTP app
1 parent 4e6debf commit 3b6e7cf

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

doc/providers/google.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Authenticator for a user, generate a secret code and persist it with the user en
7676
return null !== $this->googleAuthenticatorSecret;
7777
}
7878
79-
public function getGoogleAuthenticatorUsername(): string
79+
public function getGoogleAuthenticatorUsername(): ?string
8080
{
8181
return $this->username;
8282
}
@@ -114,7 +114,7 @@ Authenticator for a user, generate a secret code and persist it with the user en
114114
return null !== $this->googleAuthenticatorSecret;
115115
}
116116
117-
public function getGoogleAuthenticatorUsername(): string
117+
public function getGoogleAuthenticatorUsername(): ?string
118118
{
119119
return $this->username;
120120
}

src/google-authenticator/Model/Google/TwoFactorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function isGoogleAuthenticatorEnabled(): bool;
1414
/**
1515
* Return the user name. This is used in QR code generation to display the username in the TOTP app.
1616
*/
17-
public function getGoogleAuthenticatorUsername(): string;
17+
public function getGoogleAuthenticatorUsername(): string|null;
1818

1919
/**
2020
* Return the Google Authenticator secret

src/google-authenticator/Security/TwoFactor/Provider/Google/GoogleTotpFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ public function createTotpForUser(TwoFactorInterface $user): TOTPInterface
3434
/** @psalm-suppress ArgumentTypeCoercion */
3535
$totp = TOTP::create($secret, 30, 'sha1', $this->digits, clock: $this->clock);
3636

37-
$userAndHost = $user->getGoogleAuthenticatorUsername().(null !== $this->server && $this->server ? '@'.$this->server : '');
37+
$usernameLabel = $user->getGoogleAuthenticatorUsername() ?? '';
38+
$serverLabel = $this->server ?? '';
39+
$userAndHost = $usernameLabel.(strlen($serverLabel) > 0 && strlen($usernameLabel) > 0 ? '@' : '').$serverLabel;
3840
if ('' !== $userAndHost) {
3941
$totp->setLabel($userAndHost);
4042
}
4143

42-
if (null !== $this->issuer && $this->issuer) {
44+
if (null !== $this->issuer && '' !== $this->issuer) {
4345
$totp->setIssuer($this->issuer);
4446
}
4547

src/totp/Model/Totp/TwoFactorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function isTotpAuthenticationEnabled(): bool;
1515
* Return the user name. This is used in QR code generation to display the username in the TOTP app. Return an
1616
* empty string, if you don't want it to show up in the app.
1717
*/
18-
public function getTotpAuthenticationUsername(): string;
18+
public function getTotpAuthenticationUsername(): string|null;
1919

2020
/**
2121
* Return the configuration for TOTP authentication.

src/totp/Security/TwoFactor/Provider/Totp/TotpFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ public function createTotpForUser(TwoFactorInterface $user): TOTPInterface
4848
clock: $this->clock,
4949
);
5050

51-
$userAndHost = $user->getTotpAuthenticationUsername().(null !== $this->server && $this->server ? '@'.$this->server : '');
51+
$usernameLabel = $user->getTotpAuthenticationUsername() ?? '';
52+
$serverLabel = $this->server ?? '';
53+
$userAndHost = $usernameLabel.(strlen($serverLabel) > 0 && strlen($usernameLabel) > 0 ? '@' : '').$serverLabel;
5254
if ('' !== $userAndHost) {
5355
$totp->setLabel($userAndHost);
5456
}
5557

56-
if (null !== $this->issuer && $this->issuer) {
58+
if (null !== $this->issuer && '' !== $this->issuer) {
5759
$totp->setIssuer($this->issuer);
5860
}
5961

0 commit comments

Comments
 (0)