use of org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish in project LanternServer by LanternPowered.
the class HandlerEncryptionResponse method performAuth.
private void performAuth(NetworkSession session, String username, String hash, @Nullable String preventProxiesIp) {
final String postUrl = AUTH_BASE_URL + "?username=" + username + "&serverId=" + hash + (preventProxiesIp == null ? "" : "?ip=" + preventProxiesIp);
try {
// Authenticate
URLConnection connection = new URL(postUrl).openConnection();
JsonObject json;
try (InputStream is = connection.getInputStream()) {
if (is.available() == 0) {
session.disconnect(t("Invalid username or session id!"));
return;
}
try {
json = GSON.fromJson(new InputStreamReader(is), JsonObject.class);
} catch (Exception e) {
Lantern.getLogger().warn("Username \"{}\" failed to authenticate!", username);
session.disconnect(t("multiplayer.disconnect.unverified_username"));
return;
}
}
final String name = json.get("name").getAsString();
final String id = json.get("id").getAsString();
// Parse UUID
final UUID uuid;
try {
uuid = UUIDHelper.fromFlatString(id);
} catch (IllegalArgumentException e) {
Lantern.getLogger().error("Returned authentication UUID invalid: {}", id, e);
session.disconnect(t("Invalid UUID."));
return;
}
final Multimap<String, ProfileProperty> properties = LanternProfileProperty.createPropertiesMapFromJson(json.getAsJsonArray("properties"));
final LanternGameProfile gameProfile = new LanternGameProfile(uuid, name, properties);
Lantern.getLogger().info("Finished authenticating.");
final Cause cause = Cause.of(EventContext.empty(), session, gameProfile);
final ClientConnectionEvent.Auth event = SpongeEventFactory.createClientConnectionEventAuth(cause, session, new MessageEvent.MessageFormatter(t("multiplayer.disconnect.not_allowed_to_join")), gameProfile, false);
Sponge.getEventManager().post(event);
if (event.isCancelled()) {
session.disconnect(event.isMessageCancelled() ? t("multiplayer.disconnect.generic") : event.getMessage());
} else {
session.messageReceived(new MessageLoginInFinish(gameProfile));
}
} catch (Exception e) {
Lantern.getLogger().error("Error in authentication thread", e);
session.disconnect(t("Internal error during authentication."));
}
}
use of org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish in project LanternServer by LanternPowered.
the class HandlerLoginStart method handle.
@Override
public void handle(NetworkContext context, MessageLoginInStart message) {
final NetworkSession session = context.getSession();
final String username = message.getUsername();
if (session.getServer().getOnlineMode()) {
// Convert to X509 format
final byte[] publicKey = SecurityHelper.generateX509Key(session.getServer().getKeyPair().getPublic()).getEncoded();
final byte[] verifyToken = SecurityHelper.generateVerifyToken();
final String sessionId = Long.toString(RANDOM.nextLong(), 16).trim();
// Store the auth data
context.getChannel().attr(AUTH_DATA).set(new LoginAuthData(username, sessionId, verifyToken));
// Send created request message and wait for the response
session.send(new MessageLoginOutEncryptionRequest(sessionId, publicKey, verifyToken));
} else {
// Remove the encryption handler placeholder
context.getChannel().pipeline().remove(NetworkSession.ENCRYPTION);
LanternGameProfile profile = context.getChannel().attr(SPOOFED_GAME_PROFILE).getAndSet(null);
if (profile != null) {
profile = new LanternGameProfile(profile.getUniqueId(), username, profile.getPropertyMap());
} else {
// Try the online id first
try {
profile = (LanternGameProfile) Lantern.getGame().getGameProfileManager().get(username).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
// Generate a offline id
final UUID uniqueId = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
profile = new LanternGameProfile(uniqueId, username);
}
}
session.messageReceived(new MessageLoginInFinish(profile));
}
}
Aggregations