#1 2022-10-04 19:14:54

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

AuthenticationNone

I'm trying to implement simple authentication based on username and password:

  TMyAuthenticator = class(TRestServerAuthenticationNone)
  protected
    function Auth(Ctxt: TRestServerUriContext): boolean; override;
  end;

  Server.AuthenticationRegister(TMyAuthenticator);


function TMyAuthenticator.Auth(Ctxt: TSQLRestServerURIContext): boolean;
begin
  Result := True;
end;

but the url http://localhost/auth?UserName=xxx&Password=yyy displays an error:

{
"errorCode":400,
"errorText":"Bad Request"
}

How to get the UserName and Password from Ctxt?

Offline

#2 2022-10-04 19:32:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: AuthenticationNone

You get them from Ctxt, using InputUtf8[] getter method.

See how the other schemes are implemented.
And perhaps don't inherit from TRestServerAuthenticationNone, but its parent class.

Offline

#3 2022-10-04 20:24:23

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: AuthenticationNone

It does not help:

  TMyAuthenticator = class(TRestServerAuthenticationUri)
  protected
    function Auth(Ctxt: TRestServerUriContext): boolean; override;
  end;

function TMyAuthenticator.Auth(Ctxt: TSQLRestServerURIContext): boolean;
begin
  Result := Ctxt.InputUtf8['UserName']<>'xyz'  // -> TRUE under debug
end;

what I am doing wrong?

Offline

#4 2022-10-05 09:31:56

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: AuthenticationNone

I've added:

function TMyAuthenticator.Auth(Ctxt: TSQLRestServerURIContext): boolean;
begin
  Result := True;
  var body: TDocVariantData;
  body.InitFast(10,dvObject);
  Ctxt.ReturnsJson(variant(body));
end;

I'm not sure if self-made response like that is a good practice.

Offline

Board footer

Powered by FluxBB