Recent

Author Topic: MS Sharepoint Authentication - OAuth2  (Read 644 times)

magu

  • New Member
  • *
  • Posts: 37
MS Sharepoint Authentication - OAuth2
« on: June 27, 2025, 11:44:39 pm »
I have mostly vibe-coded a library to handle OAuth2 authentication to Microsoft Sharepoint, the unit and a sample are attached in case anyone of you finds it useful.
It works by creating a server to receive the authenticated token.

It works well, however the difficulty is in freeing the server, and the following code returns an error when I try to free the server Thread:

Code: Pascal  [Select][+][-]
  1. procedure TMS365Auth.LoginAuth;
  2. var
  3.   ...
  4.   Server: TFPHTTPServer;
  5.   Handler: TRequestHandler;
  6.   ServerThread: TServerThread;
  7. begin
  8.   ...
  9.   Server := TFPHTTPServer.Create(nil);
  10.   Handler := TRequestHandler.Create;
  11.   Server.OnRequest := @Handler.HandleRequest;
  12.   Server.Port := 8080;
  13.  
  14.   ServerThread := TServerThread.Create(Server);
  15.   Sleep(250); // give the server time to bind
  16.   OpenBrowser(AuthURL);
  17.  
  18.   //WriteLn('Waiting for authentication to complete...');
  19.   while (Handler.AuthCodeReceived = '') do
  20.   begin
  21.     Sleep(100);
  22.   end;
  23.  
  24.   //The server is waiting to receive the Access Code.
  25.   if Handler.AuthCodeReceived <> '' then
  26.   begin
  27.     GetAccessToken(Handler.AuthCodeReceived);
  28.     FConnected := True;
  29.   end
  30.   else
  31.   begin
  32.     //WriteLn('Authentication failed or was cancelled.');
  33.     FConnected := False;
  34.   end;
  35.  
  36.   Server.Active := False;
  37.   // this is the failure point!
  38.   //ServerThread.WaitFor;
  39.   //Server.Free;
  40.   //ServerThread.Free;
  41.   Handler.Free;
  42. end;

I would be really grateful if someone could explain how to shutdown and free the server and serverthread without crashing the system.

ps. The system works well enough if the server isn't freed but of course that would not be ideal...

Thank you,
Ref

 

TinyPortal © 2005-2018