#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 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<String, Integer, Long> 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);
}

View File

@ -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