Datasnap is based on the components Indy. When there is a requisition Http with authentication, the function TIdCustomHTTPServer.DoParseAuthentication is called. If there is no function associated with OnParseAuthentication, He’s gonna try to authenticate the type Basic. So to authenticate with bearer-token i do as described below.
Search for the statement:
FServer := TIdHTTPWebBrokerBridge.Create(Self);
In this case, when we create using the Wizard of Datasnap Rest and choosing the option standalone, this statement above is in the form created as an example.
Just below it, add the following code:
FServer.OnParseAuthentication := DoParseAuthentication;
The first DoParseAuthentication can be done as follows:
procedure TForm1.DoParseAuthentication(AContext: TIdContext; const AAuthType, AAuthData: String; var VUsername, VPassword: String; var VHandled: Boolean);
begin
VHandled := AAuthType.Equals('Bearer') and IsTokenValid(AAuthData);
end;
IsTokenValid is a function you should implement. If the authentication is correct VHandled must return True.
Obs.: I use LifeCycle of the kind Invocation
worked perfectly, thank you very much!
– Jefferson Rudolf