Search in sources :

Example 1 with MessageLoginOutEncryptionRequest

use of org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutEncryptionRequest 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)1 ExecutionException (java.util.concurrent.ExecutionException)1 NetworkSession (org.lanternpowered.server.network.NetworkSession)1 MessageLoginInFinish (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginInFinish)1 MessageLoginOutEncryptionRequest (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutEncryptionRequest)1 LanternGameProfile (org.lanternpowered.server.profile.LanternGameProfile)1