unit Server.Container;

interface

uses
  System.SysUtils, System.Classes, Sparkle.HttpServer.Module,
  Sparkle.HttpServer.Context, Sparkle.Comp.Server,
  Sparkle.Comp.HttpSysDispatcher, Aurelius.Drivers.Interfaces,
  Aurelius.Comp.Connection, XData.Comp.ConnectionPool, XData.Server.Module,
  XData.Comp.Server, Sparkle.Comp.GenericMiddleware, Sparkle.Comp.JwtMiddleware,
  Sparkle.Comp.BasicAuthMiddleware, Sparkle.Comp.CorsMiddleware,
  Sparkle.Comp.CompressMiddleware, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient,
  IdSMTPBase, IdSMTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
  IdSSLOpenSSL;

type
  TFServerContainer = class(TDataModule)
    SparkleHttpSysDispatcher: TSparkleHttpSysDispatcher;
    XDataServer: TXDataServer;
    XDataServerBasicauth: TSparkleBasicAuthMiddleware;
    XDataServerCompress: TSparkleCompressMiddleware;
    XDataServerCORS: TSparkleCorsMiddleware;
    XDataServerGeneric: TSparkleGenericMiddleware;
    procedure XDataServerBasicauthAuthenticate(Sender: TObject; const UserName,
      Password: string; var User: IUserIdentity);
    procedure XDataServerGenericRequest(Sender: TObject;
      Context: THttpServerContext; Next: THttpServerProc);
  end;

var
  FServerContainer: TFServerContainer;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


procedure TFServerContainer.XDataServerBasicauthAuthenticate(Sender: TObject;
  const UserName, Password: string; var User: IUserIdentity);
begin
  if (UserName = 'admin') and (Password = 'password') then
  begin
    User := TUserIdentity.Create;
    User.Claims.AddOrSet('username', UserName);
    User.Claims.AddOrSet('roles', 'admin'); // arbitrary info you might want to add
  end;
end;

procedure TFServerContainer.XDataServerGenericRequest(Sender: TObject;
  Context: THttpServerContext; Next: THttpServerProc);
begin
  // does not allow anonymous requests
  if Context.Request.User = nil then
    Context.Response.StatusCode := 401
  else
    Next(Context);
end;

end.
