[FEATURE] UserSupportProperties extension

This commit is contained in:
Gravita 2022-03-17 22:55:30 +07:00
parent d38feed952
commit 2b117f6717
4 changed files with 59 additions and 13 deletions

View file

@ -10,6 +10,7 @@
import pro.gravit.launchserver.HttpRequester;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportProperties;
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportTextures;
import pro.gravit.launchserver.helper.HttpHelper;
import pro.gravit.launchserver.manangers.AuthManager;
@ -18,7 +19,9 @@
import pro.gravit.utils.helper.CommonHelper;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class HttpAuthCoreProvider extends AuthCoreProvider {
@ -243,7 +246,7 @@ public RefreshTokenRequest(String refreshToken, AuthResponse.AuthContext context
}
}
public static class HttpUser implements User, UserSupportTextures {
public static class HttpUser implements User, UserSupportTextures, UserSupportProperties {
private String username;
private UUID uuid;
private String serverId;
@ -251,6 +254,7 @@ public static class HttpUser implements User, UserSupportTextures {
private ClientPermissions permissions;
private Texture skin;
private Texture cloak;
private Map<String, String> properties;
public HttpUser() {
}
@ -273,6 +277,17 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken,
this.cloak = cloak;
}
public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, Map<String, String> properties) {
this.username = username;
this.uuid = uuid;
this.serverId = serverId;
this.accessToken = accessToken;
this.permissions = permissions;
this.skin = skin;
this.cloak = cloak;
this.properties = properties;
}
@Override
public String getUsername() {
return username;
@ -307,6 +322,14 @@ public Texture getSkinTexture() {
public Texture getCloakTexture() {
return cloak;
}
@Override
public Map<String, String> getProperties() {
if(properties == null) {
return new HashMap<>();
}
return properties;
}
}
public static class HttpUserSession implements UserSession {

View file

@ -0,0 +1,7 @@
package pro.gravit.launchserver.auth.core.interfaces.user;
import java.util.Map;
public interface UserSupportProperties {
Map<String, String> getProperties();
}

View file

@ -16,6 +16,7 @@
import pro.gravit.launchserver.auth.core.AuthCoreProvider;
import pro.gravit.launchserver.auth.core.User;
import pro.gravit.launchserver.auth.core.UserSession;
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportProperties;
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportTextures;
import pro.gravit.launchserver.auth.texture.TextureProvider;
import pro.gravit.launchserver.socket.Client;
@ -26,10 +27,7 @@
import javax.crypto.Cipher;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
public class AuthManager {
private transient final LaunchServer server;
@ -206,7 +204,7 @@ public PlayerProfile getPlayerProfile(Client client) {
if (playerProfile != null) return playerProfile;
}
if (client.auth.textureProvider != null) {
return getPlayerProfile(client.uuid, client.username, client.profile == null ? null : client.profile.getTitle(), client.auth.textureProvider);
return getPlayerProfile(client.uuid, client.username, client.profile == null ? null : client.profile.getTitle(), client.auth.textureProvider, new HashMap<>());
}
// Return combined profile
return new PlayerProfile(client.uuid, client.username, null, null);
@ -229,7 +227,7 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, String username, Cl
return null;
}
if (pair.textureProvider != null) {
return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider);
return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider, new HashMap<>());
}
return new PlayerProfile(uuid, username, null, null);
}
@ -251,27 +249,33 @@ public PlayerProfile getPlayerProfile(AuthProviderPair pair, UUID uuid, ClientPr
return null;
}
if (pair.textureProvider != null) {
return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider);
return getPlayerProfile(uuid, username, profile == null ? null : profile.getTitle(), pair.textureProvider, new HashMap<>());
}
return new PlayerProfile(uuid, username, null, null);
return new PlayerProfile(uuid, username, null, null, new HashMap<>());
}
public PlayerProfile getPlayerProfile(AuthProviderPair pair, User user) {
Map<String, String> properties;
if(user instanceof UserSupportProperties userSupportProperties) {
properties = userSupportProperties.getProperties();
} else {
properties = new HashMap<>();
}
if (user instanceof UserSupportTextures) {
return new PlayerProfile(user.getUUID(), user.getUsername(), ((UserSupportTextures) user).getSkinTexture(), ((UserSupportTextures) user).getCloakTexture());
return new PlayerProfile(user.getUUID(), user.getUsername(), ((UserSupportTextures) user).getSkinTexture(), ((UserSupportTextures) user).getCloakTexture(), properties);
}
if (pair.textureProvider == null) {
throw new NullPointerException("TextureProvider not found");
}
return getPlayerProfile(user.getUUID(), user.getUsername(), "", pair.textureProvider);
return getPlayerProfile(user.getUUID(), user.getUsername(), "", pair.textureProvider, properties);
}
private PlayerProfile getPlayerProfile(UUID uuid, String username, String client, TextureProvider textureProvider) {
private PlayerProfile getPlayerProfile(UUID uuid, String username, String client, TextureProvider textureProvider, Map<String, String> properties) {
// Get skin texture
TextureProvider.SkinAndCloakTextures textures = textureProvider.getTextures(uuid, username, client);
// Return combined profile
return new PlayerProfile(uuid, username, textures.skin, textures.cloak);
return new PlayerProfile(uuid, username, textures.skin, textures.cloak, properties);
}
public AuthRequest.AuthPasswordInterface decryptPassword(AuthRequest.AuthPasswordInterface password) throws AuthException {

View file

@ -3,6 +3,8 @@
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.VerifyHelper;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
@ -11,6 +13,7 @@ public final class PlayerProfile {
public final UUID uuid;
public final String username;
public final Texture skin, cloak;
public final Map<String, String> properties;
public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak) {
@ -18,6 +21,15 @@ public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak) {
this.username = username;
this.skin = skin;
this.cloak = cloak;
this.properties = new HashMap<>();
}
public PlayerProfile(UUID uuid, String username, Texture skin, Texture cloak, Map<String, String> properties) {
this.uuid = Objects.requireNonNull(uuid, "uuid");
this.username = username;
this.skin = skin;
this.cloak = cloak;
this.properties = properties;
}
public static PlayerProfile newOfflineProfile(String username) {