Einzelnen Beitrag anzeigen

Thomasl

Registriert seit: 19. Jun 2006
Ort: Vreden
66 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: Indy und OAuth / Microsoft365

  Alt 6. Sep 2023, 09:24
ist die TIdSASLXOAuth2 in Indy10\Protocols mitlerweile schon enthalten?

diese geht
Delphi-Quellcode:
unit IdSASLXOAUTH;

interface

uses
  Classes,
  IdSASL
  ;

type
  TIdSASLXOAuth = class(TIdSASL)
  private
    FToken: string;
    FUser: string;
    FTwoLinePopFormat: Boolean;
  public
    property Token: string read FToken write FToken;
    property User: string read FUser write FUser;
    property TwoLinePopFormat: Boolean read FTwoLinePopFormat write FTwoLinePopFormat;
    class function ServiceName: TIdSASLServiceName; override;
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
    function TryStartAuthenticate(const AHost, AProtocolName : String; var VInitialResponse: String): Boolean; override;
    function ContinueAuthenticate(const ALastResponse, AHost, AProtocolName : string): string; override;
    function StartAuthenticate(const AChallenge, AHost, AProtocolName: string): string; override;
    { For cleaning up after Authentication }
    procedure FinishAuthenticate; override;
  end;

implementation

{ TIdSASLXOAuth }

class function TIdSASLXOAuth.ServiceName: TIdSASLServiceName;
begin
  Result := 'XOAUTH2';
end;

constructor TIdSASLXOAuth.Create(AOwner: TComponent);
begin
  inherited;
end;

destructor TIdSASLXOAuth.Destroy;
begin
  inherited;
end;

function TIdSASLXOAuth.TryStartAuthenticate(const AHost, AProtocolName: String; var VInitialResponse: String): Boolean;
begin
  if (AProtocolName = 'pop') and FTwoLinePopFormat then
  begin
    // Don't send anything yet
  end
  else
  begin
    VInitialResponse := 'user=' + FUser + Chr($01) + 'auth=Bearer ' + FToken + Chr($01) + Chr($01);
  end;
  Result := True;
end;

function TIdSASLXOAuth.StartAuthenticate(const AChallenge, AHost, AProtocolName: string): string;
begin
  Result := 'user=' + FUser + Chr($01) + 'auth=Bearer ' + FToken + Chr($01) + Chr($01);
end;

function TIdSASLXOAuth.ContinueAuthenticate(const ALastResponse, AHost, AProtocolName: string): string;
begin
  // Nothing to do
end;

procedure TIdSASLXOAuth.FinishAuthenticate;
begin
  // Nothing to do
end;

end.

Delphi-Quellcode:
          IdSASLXOAUTH := TIdSASLXOAuth.Create(SMTP);
          IdSASLXOAUTH.Token := FToken; // ca. 2300 Zeichen lang bei Microsoft
          IdSASLXOAUTH.User := FBenutzername;
          IdSASLXOAUTH.TwoLinePopFormat := true;
          SMTP.SASLMechanisms.Add.SASL := IdSASLXOAUTH;
Thomas Levering
  Mit Zitat antworten Zitat