@@ -27,37 +27,43 @@ function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication
2727end ;
2828
2929procedure Middleware (Req: THorseRequest; Res: THorseResponse; Next: TProc);
30- const
31- UNAUTHORIZED = ' Unauthorized' ;
3230var
3331 LBasicAuthenticationEncode: string;
3432 LBasicAuthenticationDecode: TStringList;
33+ LIsAuthenticated: Boolean;
3534begin
3635 if not Req.Headers.TryGetValue(Header, LBasicAuthenticationEncode) and not Req.Query.TryGetValue(Header, LBasicAuthenticationEncode) then
3736 begin
38- Res.Send(' Basic Authentication not found' ).Status(401 );
37+ Res.Send(' Authorization not found' ).Status(401 );
38+ raise EHorseCallbackInterrupted.Create;
39+ end ;
40+ if Pos(' basic' , LowerCase(LBasicAuthenticationEncode)) = 0 then
41+ begin
42+ Res.Send(' Invalid authorization type' ).Status(401 );
3943 raise EHorseCallbackInterrupted.Create;
4044 end ;
4145 LBasicAuthenticationDecode := TStringList.Create;
4246 try
4347 LBasicAuthenticationDecode.Delimiter := ' :' ;
4448 LBasicAuthenticationDecode.DelimitedText := TBase64Encoding.Base64.Decode(LBasicAuthenticationEncode.Replace(' basic ' , ' ' , [rfIgnoreCase]));
4549 try
46- if not Authenticate(LBasicAuthenticationDecode.Strings[0 ], LBasicAuthenticationDecode.Strings[1 ]) then
47- Res.Send(UNAUTHORIZED).Status(401 );
48- Next();
50+ LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0 ], LBasicAuthenticationDecode.Strings[1 ]);
4951 except
5052 on E: exception do
5153 begin
52- if E.InheritsFrom(EHorseCallbackInterrupted) then
53- raise EHorseCallbackInterrupted(E);
54- Res.Send(UNAUTHORIZED).Status(401 );
54+ Res.Send(E.Message).Status(500 );
5555 raise EHorseCallbackInterrupted.Create;
5656 end ;
5757 end ;
5858 finally
5959 LBasicAuthenticationDecode.Free;
6060 end ;
61+ if not LIsAuthenticated then
62+ begin
63+ Res.Send(' Unauthorized' ).Status(401 );
64+ raise EHorseCallbackInterrupted.Create;
65+ end ;
66+ Next();
6167end ;
6268
6369end .
0 commit comments