"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthenticationSuccessMessage = exports.AuthenticationFailureMessage = exports.PasswordRequestMessage = exports.PublicKeyOKMessage = exports.AuthenticationInfoResponseMessage = exports.AuthenticationInfoRequestMessage = exports.PublicKeyRequestMessage = exports.AuthenticationRequestMessage = exports.AuthenticationMessage = void 0;
const sshMessage_1 = require("./sshMessage");
class AuthenticationMessage extends sshMessage_1.SshMessage {
}
exports.AuthenticationMessage = AuthenticationMessage;
class AuthenticationRequestMessage extends AuthenticationMessage {
    get messageType() {
        return 50;
    }
    onRead(reader) {
        this.username = reader.readString('utf8');
        this.serviceName = reader.readString('ascii');
        this.methodName = reader.readString('ascii');
    }
    onWrite(writer) {
        writer.writeString(this.username || '', 'utf8');
        writer.writeString(this.serviceName || '', 'ascii');
        writer.writeString(this.validateField(this.methodName, 'method name'), 'ascii');
    }
    toString() {
        return super.toString() + ` (Method: ${this.methodName}, Username: ${this.username})`;
    }
}
exports.AuthenticationRequestMessage = AuthenticationRequestMessage;
class PublicKeyRequestMessage extends AuthenticationRequestMessage {
    constructor() {
        super();
        this.methodName = "publickey" /* AuthenticationMethod.publicKey */;
    }
    get hasSignature() {
        return this.signature && this.signature.length > 0 ? true : false;
    }
    onRead(reader) {
        super.onRead(reader);
        const hasSignature = reader.readBoolean();
        this.keyAlgorithmName = reader.readString('ascii');
        this.publicKey = reader.readBinary();
        if (hasSignature) {
            this.signature = reader.readBinary();
            this.payloadWithoutSignature = this.rawBytes.slice(0, this.rawBytes.length - this.signature.length - 4);
        }
        else {
            this.signature = undefined;
        }
    }
    onWrite(writer) {
        var _a, _b;
        super.onWrite(writer);
        if (!this.keyAlgorithmName)
            throw new Error('Key algorithm name not set.');
        if (this.methodName === "hostbased" /* AuthenticationMethod.hostBased */) {
            writer.writeString(this.keyAlgorithmName, 'ascii');
            writer.writeBinary(this.publicKey || Buffer.alloc(0));
            writer.writeString((_a = this.clientHostname) !== null && _a !== void 0 ? _a : '', 'ascii');
            writer.writeString((_b = this.clientUsername) !== null && _b !== void 0 ? _b : '', 'ascii');
            if (!this.hasSignature) {
                throw new Error('A signature is required for a host-based authentcation request.');
            }
            writer.writeBinary(this.signature);
        }
        else {
            writer.writeBoolean(this.hasSignature);
            writer.writeString(this.keyAlgorithmName, 'ascii');
            writer.writeBinary(this.publicKey || Buffer.alloc(0));
            if (this.hasSignature) {
                writer.writeBinary(this.signature);
            }
        }
    }
}
exports.PublicKeyRequestMessage = PublicKeyRequestMessage;
class AuthenticationInfoRequestMessage extends AuthenticationMessage {
    get messageType() {
        return 60;
    }
    onRead(reader) {
        this.name = reader.readString('utf8');
        this.instruction = reader.readString('utf8');
        this.language = reader.readString('ascii');
        this.prompts = [];
        const promptsCount = reader.readUInt32();
        const promptStrings = [];
        for (let i = 0; i < promptsCount; i++) {
            promptStrings.push(reader.readString('utf8'));
        }
        for (let i = 0; i < promptsCount; i++) {
            this.prompts.push({
                prompt: promptStrings[i],
                echo: reader.readBoolean(),
            });
        }
    }
    onWrite(writer) {
        var _a, _b;
        writer.writeString(this.name || '', 'utf8');
        writer.writeString(this.instruction || '', 'utf8');
        writer.writeString(this.language || '', 'ascii');
        const promptsCount = (_b = (_a = this.prompts) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
        writer.writeUInt32(promptsCount);
        for (let i = 0; i < promptsCount; i++) {
            writer.writeString(this.prompts[i].prompt || '', 'utf8');
        }
        for (let i = 0; i < promptsCount; i++) {
            writer.writeBoolean(this.prompts[i].echo);
        }
    }
}
exports.AuthenticationInfoRequestMessage = AuthenticationInfoRequestMessage;
class AuthenticationInfoResponseMessage extends AuthenticationMessage {
    get messageType() {
        return 61;
    }
    onRead(reader) {
        this.responses = [];
        const responseCount = reader.readUInt32();
        for (let i = 0; i < responseCount; i++) {
            this.responses.push(reader.readString('utf8'));
        }
    }
    onWrite(writer) {
        var _a, _b;
        const responseCount = (_b = (_a = this.responses) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
        writer.writeUInt32(responseCount);
        for (let i = 0; i < responseCount; i++) {
            writer.writeString(this.responses[i] || '', 'utf8');
        }
    }
}
exports.AuthenticationInfoResponseMessage = AuthenticationInfoResponseMessage;
class PublicKeyOKMessage extends AuthenticationMessage {
    get messageType() {
        return 60;
    }
    onRead(reader) {
        this.keyAlgorithmName = reader.readString('ascii');
        this.publicKey = reader.readBinary();
    }
    onWrite(writer) {
        if (!this.keyAlgorithmName)
            throw new Error('Key algorithm name not set.');
        if (!this.publicKey)
            throw new Error('Public key not set.');
        writer.writeString(this.keyAlgorithmName, 'ascii');
        writer.writeBinary(this.publicKey);
    }
}
exports.PublicKeyOKMessage = PublicKeyOKMessage;
class PasswordRequestMessage extends AuthenticationRequestMessage {
    constructor() {
        super();
        this.methodName = "password" /* AuthenticationMethod.password */;
    }
    onRead(reader) {
        super.onRead(reader);
        reader.readBoolean();
        this.password = reader.readString('utf8');
    }
    onWrite(writer) {
        super.onWrite(writer);
        writer.writeBoolean(false);
        writer.writeString(this.password || '', 'utf8');
    }
}
exports.PasswordRequestMessage = PasswordRequestMessage;
class AuthenticationFailureMessage extends AuthenticationMessage {
    constructor() {
        super(...arguments);
        this.partialSuccess = false;
    }
    get messageType() {
        return 51;
    }
    onRead(reader) {
        this.methodNames = reader.readList('ascii');
        this.partialSuccess = reader.readBoolean();
    }
    onWrite(writer) {
        writer.writeList(this.methodNames || [], 'ascii');
        writer.writeBoolean(this.partialSuccess);
    }
}
exports.AuthenticationFailureMessage = AuthenticationFailureMessage;
class AuthenticationSuccessMessage extends AuthenticationMessage {
    get messageType() {
        return 52;
    }
    onRead(reader) { }
    onWrite(writer) { }
}
exports.AuthenticationSuccessMessage = AuthenticationSuccessMessage;
sshMessage_1.SshMessage.index.set(50, AuthenticationRequestMessage);
sshMessage_1.SshMessage.index.set(51, AuthenticationFailureMessage);
sshMessage_1.SshMessage.index.set(52, AuthenticationSuccessMessage);
sshMessage_1.SshMessage.index.set([60, "publickey" /* AuthenticationMethod.publicKey */], PublicKeyRequestMessage);
sshMessage_1.SshMessage.index.set([60, "keyboard-interactive" /* AuthenticationMethod.keyboardInteractive */], AuthenticationInfoRequestMessage);
sshMessage_1.SshMessage.index.set([61, "keyboard-interactive" /* AuthenticationMethod.keyboardInteractive */], AuthenticationInfoResponseMessage);
//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-4c0b0c6cc561d2d3636d1ec250935431876ce4dc/node_modules-@microsoft-dev-tunnels-ssh-messages-authenticationMessages.js.map