How to get request header by passing Authorization Bearer using Datasnap

Asked

Viewed 2,976 times

4

I’m using Delphi XE7, have a WebService REST made from the DataSnap REST, I need to get the bearer-token past Header by the customer, only that the DataSnap REST by default it executes as Basic.

From what I could observe he runs the DoParseAuthentication class TIdCustomHTTPServer, but the function is private.

Someone has some idea how to do it, they’ve been through the same problem, how to solve it?

1 answer

5


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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.