Reputation: 2418
In my client application I'm using the following code to add a token in the header:
RESTRequest.Params.AddItem('Authorization', 'Bearer ' + MyToken, TRESTRequestParameterKind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);
I'd like to get this token in my server using datasnap.
I've tried use the answer from here and here but without success.
Is it possible?
How can I do this?
I could verify that Datasnap
executes TIdCustomHTTPServer.DoParseAuthentication
and that DoParseAuthentication
calls FOnParseAuthentication
if it is assigned.
So, how can I hack Datasnap to assign my own OnParseAuthentication
?
I think this solve my problem.
Upvotes: 8
Views: 2616
Reputation: 1
procedure TForm1.DoParseAuthentication(AContext: TIdContext;
const AAuthType, AAuthData: String; var VUsername, VPassword: String;
var VHandled: Boolean);
var
AuthValue: String;
begin
if SameText(AAuthType, 'Bearer') then
begin
// AAuthData should contain the bearer token
// You should validate the token here and set the VUsername or VPassword
// to the corresponding values if the token is valid.
AuthValue := AAuthData;
// Add your token validation logic here. If the token is valid, you could
// set VUsername to the corresponding username and VHandled to True.
// Example:
// if ValidateToken(AuthValue) then
// begin
// VUsername := GetUserNameFromToken(AuthValue);
// VHandled := True;
// end;
VHandled := True;
end;
end;
Upvotes: 0
Reputation: 66
I have the same problem. if the Authentication header is used then we get EIdHTTPUnsupportedAuthorisationScheme error, I need to setup OnParseAuthentication. I just started looking into this today and in the Test App "Desktop" I can do the following.
procedure TMainForm.FormCreate(Sender: TObject);
begin
FServer := TIdHTTPWebBrokerBridge.Create(Self);
FServer.OnParseAuthentication := DoParseAuthentication;// <-added this line
end;
Now I need to figure out how to update the ASAPI dll to do the same.
Upvotes: 5