https://github.com/hashload/horse-basic-auth
Middleware for Basic Authentication in HORSE
https://github.com/hashload/horse-basic-auth
auth authentication basic basic-authentication horse middleware
Last synced: 4 months ago
JSON representation
Middleware for Basic Authentication in HORSE
- Host: GitHub
- URL: https://github.com/hashload/horse-basic-auth
- Owner: HashLoad
- License: mit
- Created: 2019-07-16T01:21:28.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-08-04T13:57:44.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T00:32:24.973Z (about 1 year ago)
- Topics: auth, authentication, basic, basic-authentication, horse, middleware
- Language: Pascal
- Homepage: https://horse.hashload.com
- Size: 43.9 KB
- Stars: 53
- Watchers: 12
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# horse-basic-auth
horse-basic-auth is a official middleware for working with basic authentication in APIs developed with the Horse framework.
We created a channel on Telegram for questions and support:
![]()
## ⚙️ Installation
Installation is done using the [`boss install`](https://github.com/HashLoad/boss) command:
``` sh
boss install horse-basic-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-basic-auth/src
```## ✔️ Compatibility
This middleware is compatible with projects developed in:
- [X] Delphi
- [X] Lazarus## ⚡️ Quickstart Delphi
```delphi
uses
Horse,
Horse.BasicAuthentication, // It's necessary to use the unit
System.SysUtils;begin
// It's necessary to add the middleware in the Horse:
THorse.Use(HorseBasicAuthentication(
function(const AUsername, APassword: string): Boolean
begin
// Here inside you can access your database and validate if username and password are valid
Result := AUsername.Equals('user') and APassword.Equals('password');
end));
// The default header for receiving credentials is "Authorization".
// You can change, if necessary:
// THorse.Use(HorseBasicAuthentication(MyCallbackValidation, THorseBasicAuthenticationConfig.New.Header('X-My-Header-Authorization')));// You can also ignore routes:
// THorse.Use(HorseBasicAuthentication(MyCallbackValidation, THorseBasicAuthenticationConfig.New.SkipRoutes(['/ping'])));THorse.Get('/ping',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Res.Send('pong');
end);THorse.Listen(9000);
end;
```## ⚡️ Quickstart Lazarus
```delphi
{$MODE DELPHI}{$H+}uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Horse,
Horse.BasicAuthentication, // It's necessary to use the unit
SysUtils;procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
begin
Res.Send('Pong');
end;function DoLogin(const AUsername, APassword: string): Boolean;
begin
// Here inside you can access your database and validate if username and password are valid
Result := AUsername.Equals('user') and APassword.Equals('password');
end;begin
// It's necessary to add the middleware in the Horse:
THorse.Use(HorseBasicAuthentication(DoLogin));// The default header for receiving credentials is "Authorization".
// You can change, if necessary:
// THorse.Use(HorseBasicAuthentication(MyCallbackValidation, THorseBasicAuthenticationConfig.New.Header('X-My-Header-Authorization')));// You can also ignore routes:
// THorse.Use(HorseBasicAuthentication(MyCallbackValidation, THorseBasicAuthenticationConfig.New.SkipRoutes(['/ping'])));THorse.Get('/ping', GetPing);
THorse.Listen(9000);
end.
```## 📌 Status Code
This middleware can return the following status code:
* [401](https://httpstatuses.com/401) - Unauthorized
* [500](https://httpstatuses.com/500) - InternalServerError## ⚠️ License
`horse-basic-auth` is free and open-source middleware licensed under the [MIT License](https://github.com/HashLoad/horse-basic-auth/blob/master/LICENSE).