From f0d3d085c6ebd565456b66399ae8239af6b39922 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 2 Aug 2019 15:38:13 +0200 Subject: [PATCH] #1574 Use server name in 2FA QR code link instead of IP --- .../authme/security/totp/TotpAuthenticator.java | 11 ++++++----- .../security/totp/TotpAuthenticatorTest.java | 17 ++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/fr/xephi/authme/security/totp/TotpAuthenticator.java b/src/main/java/fr/xephi/authme/security/totp/TotpAuthenticator.java index ad74ffc6..e26b136f 100644 --- a/src/main/java/fr/xephi/authme/security/totp/TotpAuthenticator.java +++ b/src/main/java/fr/xephi/authme/security/totp/TotpAuthenticator.java @@ -9,7 +9,8 @@ import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator; import com.warrenstrange.googleauth.IGoogleAuthenticator; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.initialization.HasCleanup; -import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.PluginSettings; import org.bukkit.entity.Player; import javax.inject.Inject; @@ -24,13 +25,13 @@ public class TotpAuthenticator implements HasCleanup { private static final int CODE_RETENTION_MINUTES = 5; private final IGoogleAuthenticator authenticator; - private final BukkitService bukkitService; + private final Settings settings; private final Table usedCodes = HashBasedTable.create(); @Inject - TotpAuthenticator(BukkitService bukkitService) { + TotpAuthenticator(Settings settings) { this.authenticator = createGoogleAuthenticator(); - this.bukkitService = bukkitService; + this.settings = settings; } /** @@ -66,7 +67,7 @@ public class TotpAuthenticator implements HasCleanup { public TotpGenerationResult generateTotpKey(Player player) { GoogleAuthenticatorKey credentials = authenticator.createCredentials(); String qrCodeUrl = GoogleAuthenticatorQRGenerator.getOtpAuthURL( - bukkitService.getIp(), player.getName(), credentials); + settings.getProperty(PluginSettings.SERVER_NAME), player.getName(), credentials); return new TotpGenerationResult(credentials.getKey(), qrCodeUrl); } diff --git a/src/test/java/fr/xephi/authme/security/totp/TotpAuthenticatorTest.java b/src/test/java/fr/xephi/authme/security/totp/TotpAuthenticatorTest.java index ed3bf2c7..52f8b990 100644 --- a/src/test/java/fr/xephi/authme/security/totp/TotpAuthenticatorTest.java +++ b/src/test/java/fr/xephi/authme/security/totp/TotpAuthenticatorTest.java @@ -5,7 +5,8 @@ import com.warrenstrange.googleauth.IGoogleAuthenticator; import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.security.totp.TotpAuthenticator.TotpGenerationResult; -import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; import org.junit.Before; @@ -15,6 +16,7 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import static fr.xephi.authme.AuthMeMatchers.stringWithLength; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; @@ -33,25 +35,25 @@ public class TotpAuthenticatorTest { private TotpAuthenticator totpAuthenticator; @Mock - private BukkitService bukkitService; + private Settings settings; @Mock private IGoogleAuthenticator googleAuthenticator; @Before public void initializeTotpAuthenticator() { - totpAuthenticator = new TotpAuthenticatorTestImpl(bukkitService); + totpAuthenticator = new TotpAuthenticatorTestImpl(settings); } @Test public void shouldGenerateTotpKey() { // given // Use the GoogleAuthenticator instance the TotpAuthenticator normally creates to test its parameters - totpAuthenticator = new TotpAuthenticator(bukkitService); + totpAuthenticator = new TotpAuthenticator(settings); Player player = mock(Player.class); given(player.getName()).willReturn("Bobby"); - given(bukkitService.getIp()).willReturn("127.48.44.4"); + given(settings.getProperty(PluginSettings.SERVER_NAME)).willReturn("MCtopia"); // when TotpGenerationResult key1 = totpAuthenticator.generateTotpKey(player); @@ -61,6 +63,7 @@ public class TotpAuthenticatorTest { assertThat(key1.getTotpKey(), stringWithLength(16)); assertThat(key2.getTotpKey(), stringWithLength(16)); assertThat(key1.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200")); + assertThat(key1.getAuthenticatorQrCodeUrl(), containsString("MCtopia")); assertThat(key2.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200")); assertThat(key1.getTotpKey(), not(equalTo(key2.getTotpKey()))); } @@ -130,8 +133,8 @@ public class TotpAuthenticatorTest { private final class TotpAuthenticatorTestImpl extends TotpAuthenticator { - TotpAuthenticatorTestImpl(BukkitService bukkitService) { - super(bukkitService); + TotpAuthenticatorTestImpl(Settings settings) { + super(settings); } @Override