Skip to content

andre-djsystem/horse-bearer-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

horse-bearer-auth

horse-bearer-auth is a middleware for working with bearer authentication in APIs developed with the Horse framework.

⚙️ Installation

Installation is done using the boss install command:

boss install https://github.com/andre-djsystem/horse-bearer-auth

If you choose to install manually, simply add the following folders to your project, in Project > Options > Resource Compiler > Directories and Conditionals > Include file search path

../horse-bearer-auth/src

✔️ Compatibility

This middleware is compatible with projects developed in:

  • Delphi
  • Lazarus

⚡️ Quickstart Delphi

uses 
  Horse, 
  Horse.BearerAuthentication, // It's necessary to use the unit
  System.SysUtils;

begin
  // It's necessary to add the middleware in the Horse:
  THorse.Use(HorseBearerAuthentication(
    function(const AToken: string): Boolean
    begin
      // Here inside you can access your database and validate if username and password are valid
      Result := AToken.Equals('token');
    end));
    
  // The default header for receiving credentials is "Authorization".
  // You can change, if necessary:
  // THorse.Use(HorseBearerAuthentication(MyCallbackValidation, THorseBearerAuthenticationConfig.New.Header('X-My-Header-Authorization')));

  // You can also ignore routes:
  // THorse.Use(HorseBearerAuthentication(MyCallbackValidation, THorseBearerAuthenticationConfig.New.SkipRoutes(['/ping'])));

  THorse.Get('/ping',
    procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
    begin
      Res.Send('pong');
    end);

  THorse.Listen(9000);
end;

⚡️ Quickstart Lazarus

{$MODE DELPHI}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Horse,
  Horse.BearerAuthentication, // It's necessary to use the unit
  SysUtils;

procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
begin
  Res.Send('Pong');
end;

function CheckToken(const AToken: string): Boolean;
begin
  // Here inside you can access your database and validate if token is valid
  Result := AToken.Equals('token');
end;

begin
  // It's necessary to add the middleware in the Horse:
  THorse.Use(HorseBearerAuthentication(CheckToken));

  // The default header for receiving credentials is "Authorization".
  // You can change, if necessary:
  // THorse.Use(HorseBearerAuthentication(MyCallbackValidation, THorseBearerAuthenticationConfig.New.Header('X-My-Header-Authorization')));

  // You can also ignore routes:
  // THorse.Use(HorseBearerAuthentication(MyCallbackValidation, THorseBearerAuthenticationConfig.New.SkipRoutes(['/ping'])));

  THorse.Get('/ping', GetPing);

  THorse.Listen(9000);
end.

📌 Status Code

This middleware can return the following status code:

  • 401 - Unauthorized
  • 500 - InternalServerError

⚠️ License

horse-bearer-auth is free and open-source middleware licensed under the MIT License.

About

Middleware for Bearer Authentication in HORSE

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages