TsgcWSPClient_MQTTEvents › OnMQTTAuth

OnMQTTAuth Event

Fires during the MQTT 5 enhanced authentication exchange when the broker sends an AUTH packet.

Syntax

property OnMQTTAuth: TsgcWSMQTTAuthEvent;
// TsgcWSMQTTAuthEvent = procedure(Connection: TsgcWSConnection; ReasonCode: Integer; const ReasonName: String; AuthProperties: TsgcWSMQTTAUTHProperties) of object

Default Value

Remarks

Handler for the MQTT 5 AUTH packet used during the enhanced (challenge / response) authentication exchange. A typical flow is: Client sends CONNECT with AuthenticationMethod and AuthenticationData, broker replies with AUTH (ReasonCode = Continue authentication) carrying its challenge, client answers with another AUTH, and the broker finally sends CONNACK. The handler receives Connection, the broker ReasonCode (integer) and human-readable ReasonName, plus AuthProperties which exposes AuthenticationMethod, AuthenticationData, ReasonString and UserProperties. Use this event to read the broker challenge and then call the Auth method to post the next step of the exchange. Available only when MQTTVersion is 5.0.

Example

procedure TForm1.MQTTAuth(Connection: TsgcWSConnection;
  ReasonCode: Integer; const ReasonName: String;
  AuthProperties: TsgcWSMQTTAUTHProperties);
var
  vMethod, vData: String;
begin
  vMethod := AuthProperties.AuthenticationMethod;
  vData   := AuthProperties.AuthenticationData;
  Memo1.Lines.Add(Format('AUTH %d (%s) method=%s',
    [ReasonCode, ReasonName, vMethod]));

  // continue the exchange with the next client step
  if ReasonCode = 24 then // Continue authentication
    MQTT.Auth(False, vMethod, ComputeClientResponse(vData));
end;

Back to Events