import { PacketType } from "./PacketType.ts"; import { Encoder, EncoderError } from "./encoder.ts"; import { Decoder, DecoderError, hasEmptyFlags } from "./decoder.ts"; import type { ReasonCode } from "./ReasonCode.ts"; import type { AuthProperties } from "./Properties.ts"; import type { CodecOpts } from "./types.ts"; import { MQTTLevel } from "./protocolLevels.ts"; // AuthPacket does not exist on protocol levels < 5 export type AuthPacketV5 = { type: PacketType; protocolLevel: 5; reasonCode: ReasonCode; properties?: AuthProperties; }; /** * An AUTH packet is sent from Client to Server or Server to Client as part of an extended * authentication exchange, such as challenge / response authentication. * It is a Protocol Error for the Client or Server to send an AUTH packet * if the CONNECT packet did not contain the same Authentication Method. */ export type AuthPacket = AuthPacketV5; export function encode(packet: AuthPacket, codecOpts: CodecOpts): Uint8Array { // deno-coverage-ignore-start if (packet.protocolLevel !== 5) { throw new EncoderError("Invalid protocol level"); } // deno-coverage-ignore-stop const encoder = new Encoder(packet.type); encoder .seReasonCode(packet.reasonCode || 0) .setProperties( packet.properties || {}, packet.type, codecOpts.maxOutgoingPacketSize, ); return encoder.done(0); } export function decode( buffer: Uint8Array, flags: number, codecOpts: CodecOpts, packetType: PacketType, ): AuthPacket { // Bits 3,2,1 and 0 of the Fixed Header of the AUTH packet are reserved and MUST all be set to 0. // The Client or Server MUST treat any other value as malformed and close the Network Connection [MQTT-3.15.1-1]. hasEmptyFlags(flags); if (codecOpts.protocolLevel !== 5) { throw new DecoderError("Invalid protocol level"); } const decoder = new Decoder(packetType, buffer); const reasonCode = decoder.geReasonCode(); const properties = decoder.getProperties(PacketType.auth); decoder.done(); return { type: PacketType.auth, protocolLevel: MQTTLevel.v5, reasonCode, properties, }; }