#1574 Use server name in 2FA QR code link instead of IP

This commit is contained in:
ljacqu 2019-08-02 15:38:13 +02:00
parent c00109074a
commit f0d3d085c6
2 changed files with 16 additions and 12 deletions

View File

@ -9,7 +9,8 @@ import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator;
import com.warrenstrange.googleauth.IGoogleAuthenticator; import com.warrenstrange.googleauth.IGoogleAuthenticator;
import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.initialization.HasCleanup; 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 org.bukkit.entity.Player;
import javax.inject.Inject; import javax.inject.Inject;
@ -24,13 +25,13 @@ public class TotpAuthenticator implements HasCleanup {
private static final int CODE_RETENTION_MINUTES = 5; private static final int CODE_RETENTION_MINUTES = 5;
private final IGoogleAuthenticator authenticator; private final IGoogleAuthenticator authenticator;
private final BukkitService bukkitService; private final Settings settings;
private final Table<String, Integer, Long> usedCodes = HashBasedTable.create(); private final Table<String, Integer, Long> usedCodes = HashBasedTable.create();
@Inject @Inject
TotpAuthenticator(BukkitService bukkitService) { TotpAuthenticator(Settings settings) {
this.authenticator = createGoogleAuthenticator(); this.authenticator = createGoogleAuthenticator();
this.bukkitService = bukkitService; this.settings = settings;
} }
/** /**
@ -66,7 +67,7 @@ public class TotpAuthenticator implements HasCleanup {
public TotpGenerationResult generateTotpKey(Player player) { public TotpGenerationResult generateTotpKey(Player player) {
GoogleAuthenticatorKey credentials = authenticator.createCredentials(); GoogleAuthenticatorKey credentials = authenticator.createCredentials();
String qrCodeUrl = GoogleAuthenticatorQRGenerator.getOtpAuthURL( String qrCodeUrl = GoogleAuthenticatorQRGenerator.getOtpAuthURL(
bukkitService.getIp(), player.getName(), credentials); settings.getProperty(PluginSettings.SERVER_NAME), player.getName(), credentials);
return new TotpGenerationResult(credentials.getKey(), qrCodeUrl); return new TotpGenerationResult(credentials.getKey(), qrCodeUrl);
} }

View File

@ -5,7 +5,8 @@ import com.warrenstrange.googleauth.IGoogleAuthenticator;
import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.totp.TotpAuthenticator.TotpGenerationResult; 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 fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
@ -15,6 +16,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength; import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -33,25 +35,25 @@ public class TotpAuthenticatorTest {
private TotpAuthenticator totpAuthenticator; private TotpAuthenticator totpAuthenticator;
@Mock @Mock
private BukkitService bukkitService; private Settings settings;
@Mock @Mock
private IGoogleAuthenticator googleAuthenticator; private IGoogleAuthenticator googleAuthenticator;
@Before @Before
public void initializeTotpAuthenticator() { public void initializeTotpAuthenticator() {
totpAuthenticator = new TotpAuthenticatorTestImpl(bukkitService); totpAuthenticator = new TotpAuthenticatorTestImpl(settings);
} }
@Test @Test
public void shouldGenerateTotpKey() { public void shouldGenerateTotpKey() {
// given // given
// Use the GoogleAuthenticator instance the TotpAuthenticator normally creates to test its parameters // 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); Player player = mock(Player.class);
given(player.getName()).willReturn("Bobby"); given(player.getName()).willReturn("Bobby");
given(bukkitService.getIp()).willReturn("127.48.44.4"); given(settings.getProperty(PluginSettings.SERVER_NAME)).willReturn("MCtopia");
// when // when
TotpGenerationResult key1 = totpAuthenticator.generateTotpKey(player); TotpGenerationResult key1 = totpAuthenticator.generateTotpKey(player);
@ -61,6 +63,7 @@ public class TotpAuthenticatorTest {
assertThat(key1.getTotpKey(), stringWithLength(16)); assertThat(key1.getTotpKey(), stringWithLength(16));
assertThat(key2.getTotpKey(), stringWithLength(16)); assertThat(key2.getTotpKey(), stringWithLength(16));
assertThat(key1.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200")); 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(key2.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200"));
assertThat(key1.getTotpKey(), not(equalTo(key2.getTotpKey()))); assertThat(key1.getTotpKey(), not(equalTo(key2.getTotpKey())));
} }
@ -130,8 +133,8 @@ public class TotpAuthenticatorTest {
private final class TotpAuthenticatorTestImpl extends TotpAuthenticator { private final class TotpAuthenticatorTestImpl extends TotpAuthenticator {
TotpAuthenticatorTestImpl(BukkitService bukkitService) { TotpAuthenticatorTestImpl(Settings settings) {
super(bukkitService); super(settings);
} }
@Override @Override