11unit Horse.BasicAuthentication;
2-
2+ { $IF DEFINED(FPC)}
3+ { $MODE DELPHI}{ $H+}
4+ { $ENDIF}
35interface
46
5- uses Horse, Horse.Commons, System.SysUtils, System.NetEncoding, System.Classes;
7+ uses
8+ { $IF DEFINED(FPC)}
9+ SysUtils, base64, Classes,
10+ { $ELSE}
11+ System.SysUtils, System.NetEncoding, System.Classes,
12+ { $ENDIF}
13+
14+ Horse, Horse.Commons;
615
716const
817 AUTHORIZATION = ' authorization' ;
918
1019type
11- THorseBasicAuthentication = reference to function(const AUsername, APassword: string): Boolean;
20+ THorseBasicAuthentication = { $IF NOT DEFINED(FPC) } reference to { $ENDIF } function(const AUsername, APassword: string): Boolean;
1221
13- procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: TProc);
22+ procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: { $IF DEFINED(FPC) } TNextProc { $ELSE } TProc { $ENDIF } );
1423function HorseBasicAuthentication (const AAuthenticate: THorseBasicAuthentication; const AHeader: string = AUTHORIZATION): THorseCallback;
1524
1625implementation
@@ -26,11 +35,12 @@ function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication
2635 Result := Middleware;
2736end ;
2837
29- procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: TProc);
38+ procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: { $IF DEFINED(FPC) } TNextProc { $ELSE } TProc { $ENDIF } );
3039const
3140 BASIC_AUTH = ' basic ' ;
3241var
3342 LBasicAuthenticationEncode: string;
43+ LBase64String: string;
3444 LBasicAuthenticationDecode: TStringList;
3545 LIsAuthenticated: Boolean;
3646begin
@@ -48,7 +58,8 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: TProc);
4858 LBasicAuthenticationDecode := TStringList.Create;
4959 try
5060 LBasicAuthenticationDecode.Delimiter := ' :' ;
51- LBasicAuthenticationDecode.DelimitedText := TBase64Encoding.Base64.Decode(LBasicAuthenticationEncode.Replace(BASIC_AUTH, ' ' , [rfIgnoreCase]));
61+ LBase64String := LBasicAuthenticationEncode.Replace(BASIC_AUTH, ' ' , [rfIgnoreCase]);
62+ LBasicAuthenticationDecode.DelimitedText := { $IF DEFINED(FPC)} DecodeStringBase64(LBase64String){ $ELSE} TBase64Encoding.Base64.Decode(LBase64String){ $ENDIF} ;
5263 try
5364 LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0 ], LBasicAuthenticationDecode.Strings[1 ]);
5465 except
0 commit comments