Drop deprecated v2 API

This commit is contained in:
Gabriele C 2018-09-17 20:21:09 +02:00
parent f7024d5382
commit 09c3e7bf22
3 changed files with 2 additions and 639 deletions

View File

@ -3,7 +3,7 @@ package fr.xephi.authme;
import ch.jalu.injector.Injector;
import ch.jalu.injector.InjectorBuilder;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.api.v3.AuthMeApi;
import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
@ -112,17 +112,6 @@ public class AuthMe extends JavaPlugin {
return pluginBuildNumber;
}
/**
* Method used to obtain the v2 plugin's api instance
* @deprecated Will be removed in 5.5, use {@link fr.xephi.authme.api.v3.AuthMeApi} instead
*
* @return The plugin's api instance
*/
@Deprecated
public static NewAPI getApi() {
return NewAPI.getInstance();
}
/**
* Method called when the server enables the plugin.
*/
@ -257,8 +246,7 @@ public class AuthMe extends JavaPlugin {
injector.getSingleton(BungeeReceiver.class);
// Trigger construction of API classes; they will keep track of the singleton
injector.getSingleton(fr.xephi.authme.api.v3.AuthMeApi.class);
injector.getSingleton(NewAPI.class);
injector.getSingleton(AuthMeApi.class);
}
/**

View File

@ -1,258 +0,0 @@
package fr.xephi.authme.api;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
/**
* The v2 API of AuthMe.
*
* Recommended method of retrieving the API object:
* <code>
* NewAPI authmeApi = NewAPI.getInstance();
* </code>
*
* @deprecated Will be removed in 5.5! Use {@link fr.xephi.authme.api.v3.AuthMeApi} instead.
*/
@SuppressWarnings({"checkstyle:AbbreviationAsWordInName"}) // Justification: Class name cannot be changed anymore
@Deprecated
public class NewAPI {
private static NewAPI singleton;
private final AuthMe plugin;
private final DataSource dataSource;
private final PasswordSecurity passwordSecurity;
private final Management management;
private final ValidationService validationService;
private final PlayerCache playerCache;
/*
* Constructor for NewAPI.
*/
@Inject
NewAPI(AuthMe plugin, DataSource dataSource, PasswordSecurity passwordSecurity,
Management management, ValidationService validationService, PlayerCache playerCache) {
this.plugin = plugin;
this.dataSource = dataSource;
this.passwordSecurity = passwordSecurity;
this.management = management;
this.validationService = validationService;
this.playerCache = playerCache;
NewAPI.singleton = this;
}
/**
* Get the API object for AuthMe.
*
* @return The API object, or null if the AuthMe plugin is not enabled or not fully initialized yet
*/
public static NewAPI getInstance() {
return singleton;
}
/**
* Return the plugin instance.
*
* @return The AuthMe instance
*/
public AuthMe getPlugin() {
return plugin;
}
/**
* Gather the version number of the plugin.
* This can be used to determine whether certain API features are available or not.
*
* @return Plugin version identifier as a string.
*/
public String getPluginVersion() {
return AuthMe.getPluginVersion();
}
/**
* Return whether the given player is authenticated.
*
* @param player The player to verify
* @return true if the player is authenticated
*/
public boolean isAuthenticated(Player player) {
return playerCache.isAuthenticated(player.getName());
}
/**
* Check whether the given player is an NPC.
*
* @param player The player to verify
* @return true if the player is an npc
*/
public boolean isNPC(Player player) {
return PlayerUtils.isNpc(player);
}
/**
* Check whether the given player is unrestricted. For such players, AuthMe will not require
* them to authenticate.
*
* @param player The player to verify
* @return true if the player is unrestricted
* @see fr.xephi.authme.settings.properties.RestrictionSettings#UNRESTRICTED_NAMES
*/
public boolean isUnrestricted(Player player) {
return validationService.isUnrestricted(player.getName());
}
/**
* Get the last location of an online player.
*
* @param player The player to process
* @return Location The location of the player
*/
public Location getLastLocation(Player player) {
PlayerAuth auth = playerCache.getAuth(player.getName());
if (auth != null) {
return new Location(Bukkit.getWorld(auth.getWorld()),
auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ());
}
return null;
}
/**
* Return whether the player is registered.
*
* @param playerName The player name to check
* @return true if player is registered, false otherwise
*/
public boolean isRegistered(String playerName) {
String player = playerName.toLowerCase();
return dataSource.isAuthAvailable(player);
}
/**
* Check the password for the given player.
*
* @param playerName The player to check the password for
* @param passwordToCheck The password to check
* @return true if the password is correct, false otherwise
*/
public boolean checkPassword(String playerName, String passwordToCheck) {
return passwordSecurity.comparePassword(passwordToCheck, playerName);
}
/**
* Register an OFFLINE/ONLINE player with the given password.
*
* @param playerName The player to register
* @param password The password to register the player with
*
* @return true if the player was registered successfully
*/
public boolean registerPlayer(String playerName, String password) {
String name = playerName.toLowerCase();
if (isRegistered(name)) {
return false;
}
HashedPassword result = passwordSecurity.computeHash(password, name);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.password(result)
.realName(playerName)
.registrationDate(System.currentTimeMillis())
.build();
return dataSource.saveAuth(auth);
}
/**
* Force a player to login, i.e. the player is logged in without needing his password.
*
* @param player The player to log in
*/
public void forceLogin(Player player) {
management.forceLogin(player);
}
/**
* Force a player to logout.
*
* @param player The player to log out
*/
public void forceLogout(Player player) {
management.performLogout(player);
}
/**
* Force an ONLINE player to register.
*
* @param player The player to register
* @param password The password to use
* @param autoLogin Should the player be authenticated automatically after the registration?
*/
public void forceRegister(Player player, String password, boolean autoLogin) {
management.performRegister(RegistrationMethod.API_REGISTRATION,
ApiPasswordRegisterParams.of(player, password, autoLogin));
}
/**
* Register an ONLINE player with the given password.
*
* @param player The player to register
* @param password The password to use
*/
public void forceRegister(Player player, String password) {
forceRegister(player, password, true);
}
/**
* Unregister a player from AuthMe.
*
* @param player The player to unregister
*/
public void forceUnregister(Player player) {
management.performUnregisterByAdmin(null, player.getName(), player);
}
/**
* Unregister a player from AuthMe by name.
*
* @param name the name of the player (case-insensitive)
*/
public void forceUnregister(String name) {
management.performUnregisterByAdmin(null, name, Bukkit.getPlayer(name));
}
/**
* Get all the registered names (lowercase)
*
* @return registered names
*/
public List<String> getRegisteredNames() {
List<String> registeredNames = new ArrayList<>();
dataSource.getAllAuths().forEach(auth -> registeredNames.add(auth.getNickname()));
return registeredNames;
}
/**
* Get all the registered real-names (original case)
*
* @return registered real-names
*/
public List<String> getRegisteredRealNames() {
List<String> registeredNames = new ArrayList<>();
dataSource.getAllAuths().forEach(auth -> registeredNames.add(auth.getRealName()));
return registeredNames;
}
}

View File

@ -1,367 +0,0 @@
package fr.xephi.authme.api;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static fr.xephi.authme.IsEqualByReflectionMatcher.hasEqualValuesOnAllFields;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
/**
* Test for {@link fr.xephi.authme.api.NewAPI}.
*/
@RunWith(MockitoJUnitRunner.class)
public class NewAPITest {
@InjectMocks
private NewAPI api;
@Mock
private ValidationService validationService;
@Mock
private DataSource dataSource;
@Mock
private Management management;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private PlayerCache playerCache;
@Mock
private AuthMe authMe;
@Test
public void shouldReturnInstanceOrNull() {
NewAPI result = NewAPI.getInstance();
assertThat(result, sameInstance(api));
ReflectionTestUtils.setField(NewAPI.class, null, "singleton", null);
assertThat(NewAPI.getInstance(), nullValue());
}
@Test
public void shouldReturnIfPlayerIsAuthenticated() {
// given
String name = "Bobby";
Player player = mockPlayerWithName(name);
given(playerCache.isAuthenticated(name)).willReturn(true);
// when
boolean result = api.isAuthenticated(player);
// then
verify(playerCache).isAuthenticated(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnIfPlayerIsNpc() {
// given
Player player = mock(Player.class);
given(player.hasMetadata("NPC")).willReturn(true);
// when
boolean result = api.isNPC(player);
// then
assertThat(result, equalTo(true));
verify(player).hasMetadata("NPC");
}
@Test
public void shouldReturnIfPlayerIsUnrestricted() {
// given
String name = "Tester";
Player player = mockPlayerWithName(name);
given(validationService.isUnrestricted(name)).willReturn(true);
// when
boolean result = api.isUnrestricted(player);
// then
verify(validationService).isUnrestricted(name);
assertThat(result, equalTo(true));
}
@Test
public void shouldGetLastLocation() {
// given
String name = "Gary";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.locWorld("world")
.locX(12.4)
.locY(24.6)
.locZ(-438.2)
.locYaw(3.41f)
.locPitch(0.29f)
.build();
given(playerCache.getAuth(name)).willReturn(auth);
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
World world = mock(World.class);
given(server.getWorld(auth.getWorld())).willReturn(world);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, not(nullValue()));
assertThat(result.getX(), equalTo(auth.getQuitLocX()));
assertThat(result.getY(), equalTo(auth.getQuitLocY()));
assertThat(result.getZ(), equalTo(auth.getQuitLocZ()));
assertThat(result.getWorld(), equalTo(world));
}
@Test
public void shouldReturnNullForUnavailablePlayer() {
// given
String name = "Numan";
Player player = mockPlayerWithName(name);
given(playerCache.getAuth(name)).willReturn(null);
// when
Location result = api.getLastLocation(player);
// then
assertThat(result, nullValue());
}
@Test
public void shouldCheckForRegisteredName() {
// given
String name = "toaster";
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
boolean result = api.isRegistered(name);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldCheckPassword() {
// given
String playerName = "Robert";
String password = "someSecretPhrase2983";
given(passwordSecurity.comparePassword(password, playerName)).willReturn(true);
// when
boolean result = api.checkPassword(playerName, password);
// then
verify(passwordSecurity).comparePassword(password, playerName);
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnAuthNames() {
// given
String[] names = {"bobby", "peter", "elisabeth", "craig"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldReturnAuthRealNames() {
// given
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
List<PlayerAuth> auths = Arrays.stream(names)
.map(name -> PlayerAuth.builder().name(name).realName(name).build())
.collect(Collectors.toList());
given(dataSource.getAllAuths()).willReturn(auths);
// when
List<String> result = api.getRegisteredRealNames();
// then
assertThat(result, contains(names));
}
@Test
public void shouldUnregisterPlayer() {
// given
Player player = mock(Player.class);
String name = "Donald";
given(player.getName()).willReturn(name);
// when
api.forceUnregister(player);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
@Test
public void shouldUnregisterPlayerByName() {
// given
Server server = mock(Server.class);
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
String name = "tristan";
Player player = mock(Player.class);
given(server.getPlayer(name)).willReturn(player);
// when
api.forceUnregister(name);
// then
verify(management).performUnregisterByAdmin(null, name, player);
}
@Test
public void shouldReturnAuthMeInstance() {
// given / when
AuthMe result = api.getPlugin();
// then
assertThat(result, equalTo(authMe));
}
@Test
public void shouldReturnVersion() {
// given / when
String result = api.getPluginVersion();
// then
assertThat(result, equalTo(AuthMe.getPluginVersion()));
}
@Test
public void shouldForceLogin() {
// given
Player player = mock(Player.class);
// when
api.forceLogin(player);
// then
verify(management).forceLogin(player);
}
@Test
public void shouldForceLogout() {
// given
Player player = mock(Player.class);
// when
api.forceLogout(player);
// then
verify(management).performLogout(player);
}
@Test
public void shouldForceRegister() {
// given
Player player = mock(Player.class);
String pass = "test235";
// when
api.forceRegister(player, pass);
// then
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
argThat(hasEqualValuesOnAllFields(ApiPasswordRegisterParams.of(player, pass, true))));
}
@Test
public void shouldForceRegisterAndNotAutoLogin() {
// given
Player player = mock(Player.class);
String pass = "test235";
// when
api.forceRegister(player, pass, false);
// then
verify(management).performRegister(eq(RegistrationMethod.API_REGISTRATION),
argThat(hasEqualValuesOnAllFields(ApiPasswordRegisterParams.of(player, pass, false))));
}
@Test
public void shouldRegisterPlayer() {
// given
String name = "Marco";
String password = "myP4ss";
HashedPassword hashedPassword = new HashedPassword("0395872SLKDFJOWEIUTEJSD");
given(passwordSecurity.computeHash(password, name.toLowerCase())).willReturn(hashedPassword);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
// when
boolean result = api.registerPlayer(name, password);
// then
assertThat(result, equalTo(true));
verify(passwordSecurity).computeHash(password, name.toLowerCase());
ArgumentCaptor<PlayerAuth> authCaptor = ArgumentCaptor.forClass(PlayerAuth.class);
verify(dataSource).saveAuth(authCaptor.capture());
assertThat(authCaptor.getValue().getNickname(), equalTo(name.toLowerCase()));
assertThat(authCaptor.getValue().getRealName(), equalTo(name));
assertThat(authCaptor.getValue().getPassword(), equalTo(hashedPassword));
}
@Test
public void shouldNotRegisterAlreadyRegisteredPlayer() {
// given
String name = "jonah";
given(dataSource.isAuthAvailable(name)).willReturn(true);
// when
boolean result = api.registerPlayer(name, "pass");
// then
assertThat(result, equalTo(false));
verify(dataSource, only()).isAuthAvailable(name);
verifyZeroInteractions(management, passwordSecurity);
}
private static Player mockPlayerWithName(String name) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
return player;
}
}