Delphi and REST with two methods with and without authenticate

ghz 8months ago ⋅ 77 views

I need REST service whit two methods: first need autorization and second open for all (with no authenticate procedure).

If I put TDSHttpService and bind with TDSAuthenticationManager, then all methods needs authenticate (in authentication event I have no info about method). If I put two TDSHttpService components - one with and second without Auth Manager, and with different contexts (f.ex.), then can work together (on same port). What is valid solution?

Answers

A valid solution for implementing a REST service with two methods, one requiring authorization and the other open for all without authentication, depends on the specific requirements of your application and the capabilities of your development environment.

Here's a common approach using Delphi's DataSnap REST components:

  1. Single TDSHttpService with TDSAuthenticationManager:

    • You can use a single TDSHttpService component and bind it with a TDSAuthenticationManager.
    • In the OnAuthentication event of the TDSAuthenticationManager, you can check the URL or parameters of the request to determine which method is being accessed.
    • If the method requires authorization, you can perform the authentication check. If it's the method that's open for all, you can skip the authentication check.
    • However, as you've noted, in the OnAuthentication event, you might not have access to information about the specific method being accessed, which makes it challenging to implement different authorization requirements for different methods.
  2. Multiple TDSHttpService Components:

    • Another approach is to use multiple TDSHttpService components, each with its own TDSAuthenticationManager.
    • One TDSHttpService can be configured with a TDSAuthenticationManager for methods requiring authorization, while the other TDSHttpService can be configured without authentication for methods open for all.
    • You can bind each TDSHttpService to different URL paths or ports (if necessary) to distinguish between them.
    • This approach allows you to have separate authentication mechanisms for different methods, providing more flexibility in implementing authorization requirements.

Here's a simplified example:

// For authorized methods
procedure TMyAuthService.AuthenticationManager1Authentication(Sender: TObject;
  const Protocol, Context, User, Password: string; var valid: Boolean;
  UserRoles: TStrings);
begin
  // Perform authentication based on the context
  if Context = '/authorized_method' then
    valid := CheckAuthorization(User, Password)
  else
    valid := False;
end;

// For methods open for all
procedure TMyOpenService.AuthenticationManager2Authentication(Sender: TObject;
  const Protocol, Context, User, Password: string; var valid: Boolean;
  UserRoles: TStrings);
begin
  // Allow access without authentication
  valid := True;
end;

Choose the approach that best fits your application's requirements and architecture. If you need different authentication mechanisms for different methods and have control over the URL structure or ports, using multiple TDSHttpService components can offer more flexibility. Otherwise, a single TDSHttpService with conditional authentication logic might suffice.