Search in sources :

Example 1 with MessageLoginInFinish

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."));
    }
}
Also used : ProfileProperty(org.spongepowered.api.profile.property.ProfileProperty) LanternProfileProperty(org.lanternpowered.server.profile.LanternProfileProperty) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MessageEvent(org.spongepowered.api.event.message.MessageEvent) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) JsonObject(com.google.gson.JsonObject) URLConnection(java.net.URLConnection) URL(java.net.URL) SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageLoginInFinish(org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish) Cause(org.spongepowered.api.event.cause.Cause) UUID(java.util.UUID)

Example 2 with MessageLoginInFinish

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));
    }
}
Also used : NetworkSession(org.lanternpowered.server.network.NetworkSession) MessageLoginOutEncryptionRequest(org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutEncryptionRequest) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) ExecutionException(java.util.concurrent.ExecutionException) UUID(java.util.UUID) MessageLoginInFinish(org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish)

Aggregations

UUID (java.util.UUID)2 MessageLoginInFinish (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish)2 LanternGameProfile (org.lanternpowered.server.profile.LanternGameProfile)2 JsonObject (com.google.gson.JsonObject)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketException (java.net.SocketException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ExecutionException (java.util.concurrent.ExecutionException)1 NetworkSession (org.lanternpowered.server.network.NetworkSession)1 MessageLoginOutEncryptionRequest (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutEncryptionRequest)1 LanternProfileProperty (org.lanternpowered.server.profile.LanternProfileProperty)1 Cause (org.spongepowered.api.event.cause.Cause)1 MessageEvent (org.spongepowered.api.event.message.MessageEvent)1 ClientConnectionEvent (org.spongepowered.api.event.network.ClientConnectionEvent)1 ProfileProperty (org.spongepowered.api.profile.property.ProfileProperty)1