Skip to content

Commit fa12996

Browse files
Merge pull request #11 from HashLoad/#224-Adjusted-code-for-ISAPI
Adjusted code for ISAPI
2 parents e85da9d + bdd7153 commit fa12996

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/Horse.BasicAuthentication.pas

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
unit Horse.BasicAuthentication;
22

33
{$IF DEFINED(FPC)}
4-
{$MODE DELPHI}{$H+}
4+
{$MODE DELPHI}{$H+}
55
{$ENDIF}
66

77
interface
88

99
uses
10-
{$IF DEFINED(FPC)}
11-
SysUtils, StrUtils, base64, Classes,
12-
{$ELSE}
13-
System.SysUtils, System.NetEncoding, System.Classes, System.StrUtils,
14-
{$ENDIF}
10+
{$IF DEFINED(FPC)}
11+
SysUtils, StrUtils, base64, Classes,
12+
{$ELSE}
13+
System.SysUtils, System.NetEncoding, System.Classes, System.StrUtils,
14+
{$ENDIF}
1515
Horse, Horse.Commons;
1616

1717
const
18-
AUTHORIZATION = 'authorization';
18+
AUTHORIZATION = 'Authorization';
1919
REALM_MESSAGE = 'Enter credentials';
2020

2121
type
@@ -48,7 +48,7 @@ THorseBasicAuthenticationConfig = class(TInterfacedObject, IHorseBasicAuthenti
4848
type
4949
THorseBasicAuthentication = {$IF NOT DEFINED(FPC)} reference to {$ENDIF} function(const AUsername, APassword: string): Boolean;
5050

51-
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF} );
51+
procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF});
5252
function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication): THorseCallback; overload;
5353
function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication; const AConfig: IHorseBasicAuthenticationConfig): THorseCallback; overload;
5454

@@ -85,28 +85,35 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
8585
Exit;
8686
end;
8787

88-
LBasicAuthenticationEncode := Req.Headers[Config.Header];
88+
LBasicAuthenticationEncode := '';
89+
if Req.Headers.ContainsKey(Config.Header) then
90+
LBasicAuthenticationEncode := Req.Headers.Items[Config.Header];
91+
8992
if LBasicAuthenticationEncode.Trim.IsEmpty and not Req.Query.TryGetValue(Config.Header, LBasicAuthenticationEncode) then
9093
begin
9194
Res.Send('Authorization not found').Status(THTTPStatus.Unauthorized).RawWebResponse
92-
{$IF DEFINED(FPC)}
95+
{$IF DEFINED(FPC)}
9396
.WWWAuthenticate := Format('Basic realm=%s', [Config.RealmMessage]);
94-
{$ELSE}
97+
{$ELSE}
9598
.Realm := Config.RealmMessage;
96-
{$ENDIF}
99+
{$ENDIF}
100+
97101
raise EHorseCallbackInterrupted.Create;
98102
end;
99-
if not LBasicAuthenticationEncode.ToLower.StartsWith(BASIC_AUTH) then
100-
begin
103+
104+
if not LBasicAuthenticationEncode.Trim.ToLower.StartsWith(BASIC_AUTH) then
105+
begin
101106
Res.Send('Invalid authorization type').Status(THTTPStatus.Unauthorized);
102107
raise EHorseCallbackInterrupted.Create;
103108
end;
109+
104110
LBasicAuthenticationDecode := TStringList.Create;
105111
try
106112
LBasicAuthenticationDecode.Delimiter := ':';
107113
LBasicAuthenticationDecode.StrictDelimiter := True;
108-
LBase64String := LBasicAuthenticationEncode.Replace(BASIC_AUTH, '', [rfIgnoreCase]);
109-
LBasicAuthenticationDecode.DelimitedText := {$IF DEFINED(FPC)}DecodeStringBase64(LBase64String){$ELSE}TBase64Encoding.Base64.Decode(LBase64String){$ENDIF};
114+
LBase64String := LBasicAuthenticationEncode.Trim.Replace(BASIC_AUTH, '', [rfIgnoreCase]);
115+
LBasicAuthenticationDecode.DelimitedText := {$IF DEFINED(FPC)}DecodeStringBase64(LBase64String){$ELSE}TBase64Encoding.base64.Decode(LBase64String){$ENDIF};
116+
110117
try
111118
LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0], LBasicAuthenticationDecode.Strings[1]);
112119
except
@@ -119,11 +126,13 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
119126
finally
120127
LBasicAuthenticationDecode.Free;
121128
end;
129+
122130
if not LIsAuthenticated then
123131
begin
124132
Res.Send('Unauthorized').Status(THTTPStatus.Unauthorized);
125133
raise EHorseCallbackInterrupted.Create;
126134
end;
135+
127136
Next();
128137
end;
129138

0 commit comments

Comments
 (0)