diff --git a/src/Horse.BasicAuthentication.pas b/src/Horse.BasicAuthentication.pas index ed1ecf8..bf0724e 100644 --- a/src/Horse.BasicAuthentication.pas +++ b/src/Horse.BasicAuthentication.pas @@ -54,22 +54,31 @@ THorseBasicAuthenticationConfig = class(TInterfacedObject, IHorseBasicAuthenti type THorseBasicAuthentication = {$IF NOT DEFINED(FPC)} reference to {$ENDIF} function(const AUsername, APassword: string): Boolean; + THorseBasicAuthenticationWithResponse = {$IF NOT DEFINED(FPC)} reference to {$ENDIF} function(const AUsername, APassword: string; ARes: THorseResponse): Boolean; procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF}); function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication): THorseCallback; overload; +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthenticationWithResponse): THorseCallback; overload; function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication; const AConfig: IHorseBasicAuthenticationConfig): THorseCallback; overload; +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthenticationWithResponse; const AConfig: IHorseBasicAuthenticationConfig): THorseCallback; overload; implementation var Config: IHorseBasicAuthenticationConfig; Authenticate: THorseBasicAuthentication; + AuthenticateWithResponse: THorseBasicAuthenticationWithResponse; function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication): THorseCallback; begin Result := HorseBasicAuthentication(AAuthenticate, THorseBasicAuthenticationConfig.New); end; +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthenticationWithResponse): THorseCallback; overload; +begin + Result := HorseBasicAuthentication(AAuthenticate, THorseBasicAuthenticationConfig.New); +end; + function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication; const AConfig: IHorseBasicAuthenticationConfig): THorseCallback; begin Config := AConfig; @@ -77,6 +86,13 @@ function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication Result := Middleware; end; +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthenticationWithResponse; const AConfig: IHorseBasicAuthenticationConfig): THorseCallback; overload; +begin + Config := AConfig; + AuthenticateWithResponse := AAuthenticate; + Result := Middleware; +end; + procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF}); const BASIC_AUTH = 'basic '; @@ -126,7 +142,14 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED LBasicAuthenticationDecode.DelimitedText := {$IF DEFINED(FPC)}DecodeStringBase64(LBase64String){$ELSE}TBase64Encoding.base64.Decode(LBase64String){$ENDIF}; try - LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0], LBasicAuthenticationDecode.Strings[1]); + if Assigned(AuthenticateWithResponse) then + begin + LIsAuthenticated := AuthenticateWithResponse(LBasicAuthenticationDecode.Strings[0], LBasicAuthenticationDecode.Strings[1], Res); + end + else + begin + LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0], LBasicAuthenticationDecode.Strings[1]); + end; except on E: exception do begin