TsgcWebSocketServer | Autenticazione del Server

Il server TsgcWebSocket supporta 3 tipi di autenticazione:

 

 

È possibile impostare un elenco di utenti autenticati utilizzando la proprietà AuthUsers; specificare gli utenti nel seguente formato: user=password

OnAuthentication

Ogni volta che il server riceve una richiesta di autenticazione da un client, questo evento viene chiamato per indicare se l'utente è autenticato o meno.

Usare il parametro Authenticated per accettare o rifiutare la connessione.

 


procedure OnAuthentication(Connection: TsgcWSConnection; aUser, aPassword: string; 
  var Authenticated: Boolean);
begin
  if ((aUser = 'user') and (aPassword = 'secret')) then
    Authenticated := true
  else
    Authenticated := false;
end;

 

 

OnUnknownAuthentication

Se il tipo di autenticazione non è supportato per impostazione predefinita, come JWT, è comunque possibile utilizzare questo evento per accettare o rifiutare la connessione. Basta leggere i parametri e decidere se accettare la connessione.

 


procedure OnUnknownAuthentication(Connection: TsgcWSConnection; AuthType, AuthData: string; 
  var aUser, aPassword: string; var Authenticated: Boolean);
begin
  if AuthType = 'Bearer' then
  begin
    if AuthData = 'jwt_token' then
      Authenticated := true
    else
      Authenticated := false;
  end
  else
    Authenticated := false;
end;