How to get a token passed as header using datasnap?
Asked Answered
A

2

8

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?

EDIT

I could verify that Datasnap executes TIdCustomHTTPServer.DoParseAuthenticationand 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.

Amie answered 27/1, 2016 at 11:52 Comment(8)
Are you sure the information arrives at the destination, i.e. can you inspect the HTTP coming in?Exudate
Yes, I'm sure @JanDoggen. I can see this information debugging the PreparePostStream Method of the unit IdCustomHTTPServer. It calls the function TIdCustomHTTPServer.DoParseAuthentication. And it uses the default parser for the authentication. Maybe I could get the OnParserAuthentication of the IdHttpServer that Datasnap uses inside, but I don't know how to do this.Amie
Sorry, I said PreparePostStream but it is TIdCustomHTTPServer.DoExecute insteadAmie
@DanielGrillo, i have same problem, u resolved?Innerdirected
@DanielGrillo, se você poder ma ajudar, eu agradeço não estou conseguindo resolver.Innerdirected
@JeffersonRudolf Faça a pergunta no SO em português que eu respondo lá.Amie
@DanielGrillom ta certoInnerdirected
@DanielGrillo, postei a pergunta lá pt.#169114Innerdirected
B
5

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.

Brougham answered 12/4, 2016 at 17:12 Comment(1)
where DoParseAuthentication? its function protectedInnerdirected
P
0
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;
Phiphenomenon answered 4/8, 2023 at 6:56 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Boogeyman

© 2022 - 2024 — McMap. All rights reserved.