YOUSEFUL / DELPHI / COMPONENTS

TAtlasMqttAuthenticateEvent

The type of TAtlasMqttBroker.OnAuthenticate: decide whether a client may connect.

Type Unit Atlas.MqttBroker · Product Atlas MQTT

Description

pascal
TAtlasMqttAuthenticateEvent = procedure(Sender: TObject; const ClientId, Username, Password: string;
  var Accept: Boolean) of object;

Sender is the broker. ClientId, Username and Password come from the client's CONNECT; they are empty when not sent. ClientId is empty for a client that asked the broker to name it. Accept is True on entry; set it to False to refuse with return code 4. The handler runs on the connection's own broker thread, possibly for several clients at once.

This type has no members of its own beyond those listed in its description.

Examples

A table of users

Filled before Start and never changed afterwards, so the broker threads may read it at the same time.

pascal
uses
  System.SysUtils, System.Generics.Collections, Atlas.MqttBroker;

type
  TUserTable = class
    Users: TDictionary<string, string>;
    procedure Authenticate(Sender: TObject; const ClientId, Username, Password: string;
      var Accept: Boolean);
  end;

procedure TUserTable.Authenticate(Sender: TObject; const ClientId, Username, Password: string;
  var Accept: Boolean);
var
  Expected: string;
begin
  Accept := Users.TryGetValue(Username, Expected) and (Password = Expected)
    and ClientId.StartsWith(Username + '-');
end;

TAtlasMqttBroker

Also in Atlas MQTT

TAtlasMqttClient · TAtlasMqttBroker