Search in sources :

Example 1 with AuthPacket

use of io.mycat.beans.mysql.packet.AuthPacket in project Mycat2 by MyCATApache.

the class ConnectHandler method sendHandshakeResponseMessage.

private void sendHandshakeResponseMessage(String username, String password, String database, byte[] nonce, String clientPluginName, int clientCapabilitiesFlags, int collationId, Map<String, String> clientConnectionAttributes) {
    AuthPacket authPacket = new AuthPacket();
    authPacket.setCapabilities(clientCapabilitiesFlags);
    authPacket.setMaxPacketSize(PACKET_PAYLOAD_LENGTH_LIMIT);
    authPacket.setCharacterSet((byte) collationId);
    authPacket.setUsername(username);
    authPacket.setDatabase(database);
    authPacket.setClientConnectAttrs(clientConnectionAttributes);
    String authMethod = clientPluginName;
    if (password.isEmpty()) {
        authPacket.setPassword(new byte[] {});
    } else {
        byte[] authResponse;
        switch(authMethod) {
            case "mysql_native_password":
                authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
            case "caching_sha2_password":
                authResponse = CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
            case "mysql_clear_password":
                authResponse = password.getBytes(StandardCharsets.UTF_8);
                break;
            default:
                authMethod = "mysql_native_password";
                authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
        }
        authPacket.setPassword(authResponse);
    }
    authPacket.setAuthPluginName(authMethod);
    MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter();
    authPacket.writePayload(mySQLPayloadWriter);
    super.socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(++packetId, mySQLPayloadWriter.toByteArray())));
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) AuthPacket(io.mycat.beans.mysql.packet.AuthPacket)

Example 2 with AuthPacket

use of io.mycat.beans.mysql.packet.AuthPacket in project Mycat2 by MyCATApache.

the class VertxMySQLAuthHandler method handle.

@Override
public void handle(Buffer event) {
    buffer.appendBuffer(event);
    if (buffer.length() > 3) {
        int length = readInt(buffer, 0, 3);
        if (length == buffer.length() - 4) {
            int packetId = buffer.getUnsignedByte(3);
            Buffer payload = buffer.slice(4, buffer.length());
            ReadView readView = new ReadView(payload);
            if (!authSwitchResponse) {
                this.authPacket = new AuthPacket();
                authPacket.readPayload(readView);
                if ("mysql_native_password".equalsIgnoreCase(authPacket.getAuthPluginName()) || authPacket.getAuthPluginName() == null) {
                    auth(packetId);
                } else {
                    authSwitchResponse = true;
                    buffer = Buffer.buffer();
                    AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
                    authSwitchRequestPacket.setStatus((byte) 0xfe);
                    authSwitchRequestPacket.setAuthPluginName("mysql_native_password");
                    authSwitchRequestPacket.setAuthPluginData(new String(seedParts[2]));
                    MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
                    authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
                    socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(packetId + 1, mySQLPayloadWriter)));
                    return;
                }
            } else {
                byte[] bytes = readView.readEOFStringBytes();
                authPacket.setPassword(bytes);
                auth(packetId);
            }
        }
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AuthSwitchRequestPacket(io.mycat.beans.mysql.packet.AuthSwitchRequestPacket) MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) AuthPacket(io.mycat.beans.mysql.packet.AuthPacket)

Example 3 with AuthPacket

use of io.mycat.beans.mysql.packet.AuthPacket in project Mycat2 by MyCATApache.

the class MySQLClientAuthHandler method readResponseAuthPacket.

private AuthPacket readResponseAuthPacket(MycatSession mycat) {
    MySQLPacket mySQLPacket = mycat.currentProxyPayload();
    AuthPacket auth = new AuthPacket();
    auth.readPayload(mySQLPacket);
    mycat.resetCurrentProxyPayload();
    return auth;
}
Also used : AuthPacket(io.mycat.beans.mysql.packet.AuthPacket) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Aggregations

AuthPacket (io.mycat.beans.mysql.packet.AuthPacket)3 MySQLPayloadWriter (io.mycat.beans.mysql.MySQLPayloadWriter)2 AuthSwitchRequestPacket (io.mycat.beans.mysql.packet.AuthSwitchRequestPacket)1 MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)1 Buffer (io.vertx.core.buffer.Buffer)1