Recent

Author Topic: [SOLVED] HTTP Authentication  (Read 10752 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] HTTP Authentication
« on: October 10, 2020, 03:37:18 pm »
I am using Synapse and would like a simple example of how to authenticate / login to a web server. I know I need headers but I don't know how to prepare them.

For example

URL http:\\www.abc.def
Username jbloggs
Password somesecret

Thanks in advance.
« Last Edit: October 14, 2020, 04:56:26 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: HTTP Authentication
« Reply #1 on: October 10, 2020, 04:33:50 pm »
Authentication is not a one answer topic. There are various authentication methods that have been invented and implemented. If you're trying to act as a client to some existing website that's not yours, read first what method they support. It could range from just a basic auth to a 3-legged OAuth v2.0.

Otherwise, I'm not using Synapse, but here is a simple client/server example of basic auth.

Server:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  
  3. uses
  4.   classes,
  5.   fphttpapp,
  6.   httpdefs,
  7.   httproute,
  8.   base64;
  9.  
  10. procedure Whatever(ARequest: TRequest; AResponse: TResponse);
  11. var
  12.   LAuth: String;
  13. begin
  14.   LAuth := ARequest.Authorization;
  15.   if (Copy(LAuth,1,5) <> 'Basic') or (DecodeStringBase64(Copy(LAuth,7,Length(LAuth) - 7 + 1)) <> 'jbloggs:somesecret') then begin
  16.     AResponse.Code := 401;
  17.     Exit;
  18.   end;
  19.  
  20.   AResponse.Content := 'This is my precious content';
  21. end;
  22.  
  23. begin
  24.   HTTPRouter.RegisterRoute('/accessmethroughbasicauth',@Whatever);
  25.   Application.Port := 9000;
  26.   Application.Initialize;
  27.   Application.Run;
  28. end.
  29.  
Client:
Code: Pascal  [Select][+][-]
  1. uses
  2.   fphttpclient;
  3.  
  4. begin
  5.   WriteLn(TFPHTTPClient.SimpleGet('http://jbloggs:somesecret@localhost:9000/accessmethroughbasicauth'));
  6. end.
  7.  
Or you can use any of these cURL commands:
Code: [Select]
$ curl 'jbloggs:somesecret@localhost:9000/accessmethroughbasicauth'
$ curl -u 'jbloggs:somesecret' 'localhost:9000/accessmethroughbasicauth'

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: HTTP Authentication
« Reply #2 on: October 10, 2020, 05:44:51 pm »
I have tried that method before and it didn't work.

For example how would I login to this site?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: HTTP Authentication
« Reply #3 on: October 10, 2020, 06:09:41 pm »
There are two things wrong.
You should not try http://
But https://
For that you need a server certificate but these are free from e.g. https://letsencrypt.org
Authentication for http is not recommended. (Basically: does not exist!!!!)

You are a beginner, start from the right place.

Then again, provided you use httpS, leledumbo's example should work.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: HTTP Authentication
« Reply #4 on: October 10, 2020, 07:05:44 pm »
Is it possible to login to this site using rhat method?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

circular

  • Hero Member
  • *****
  • Posts: 4469
    • Personal webpage
Re: HTTP Authentication
« Reply #5 on: October 10, 2020, 07:18:19 pm »
You are a beginner, start from the right place.
That was unnecessary.

Is it possible to login to this site using rhat method?
Which site are you trying to log on?
Conscience is the debugger of the mind

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: HTTP Authentication
« Reply #6 on: October 10, 2020, 07:25:32 pm »
You can always test against https://example.com
It it there for that purpose.
« Last Edit: October 10, 2020, 07:27:07 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: HTTP Authentication
« Reply #7 on: October 10, 2020, 07:26:11 pm »
There are lots of authentication methods.
So we indeed need to know the site you are trying to login to.
(Or you need to specify exactly what authentication method is used)

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: HTTP Authentication
« Reply #8 on: October 10, 2020, 07:28:24 pm »
There are rules. Follow my  link Rik.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: HTTP Authentication
« Reply #9 on: October 10, 2020, 07:30:23 pm »
Try this site. https://forum.lazarus.freepascal.org

How can I discover what method is required?
« Last Edit: October 10, 2020, 07:35:40 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: HTTP Authentication
« Reply #10 on: October 10, 2020, 07:32:12 pm »
There are rules. Follow my  link Rik.
You REALLY think following that link will help TS with his question from the opening post?
(please read that one again)

This wasn't about https versus http but about authentication itself (of which there are many variants).

« Last Edit: October 10, 2020, 07:50:36 pm by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: HTTP Authentication
« Reply #11 on: October 10, 2020, 07:43:14 pm »
Try this site. https://forum.lazarus.freepascal.org
How can I discover what method is required?
This site doesn't use HTTP authentication via headers.

You can logout and go back to the login page and look at the source.
You see that posting password and username (sometimes along with some session id) will let you login to the site.
Cookies are set and the site knows you are logged-in.

So if you need to login to this site from code you can follow the same path (posting to the login-form page and saving the cookies).

You can follow the examples here.
https://forum.lazarus.freepascal.org/index.php?topic=28008.0

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: HTTP Authentication
« Reply #12 on: October 10, 2020, 08:37:51 pm »
How do web browsers login to different sites and differentiate between authentication methods?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: HTTP Authentication
« Reply #13 on: October 10, 2020, 08:50:33 pm »
How do web browsers login to different sites and differentiate between authentication methods?
Webbrowser follow instruction in code (html).
So for this site, it is a login form and cookies.
For Google sites it is OAuth2.
For other sites it could be a authentication via headers.
For example see https://en.m.wikipedia.org/wiki/Basic_access_authentication
Of that there are again multiple versions.

If you pick the wrong one as client, login won't work.

So, look in the html of the login site to see if you can spot the method.
(Header authentication is seldom used for forum sites.)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: HTTP Authentication
« Reply #14 on: October 10, 2020, 09:12:00 pm »
Thanks. Any idea on how to do it for a vBulletin based site?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018