|
| 1 | +unit Horse.BasicAuthentication; |
| 2 | +{$IF DEFINED(FPC)} |
| 3 | +{$MODE DELPHI}{$H+} |
| 4 | +{$ENDIF} |
| 5 | +interface |
| 6 | + |
| 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; |
| 15 | + |
| 16 | +const |
| 17 | + AUTHORIZATION = 'authorization'; |
| 18 | + |
| 19 | +type |
| 20 | + THorseBasicAuthentication = {$IF NOT DEFINED(FPC)} reference to {$ENDIF} function(const AUsername, APassword: string): Boolean; |
| 21 | + |
| 22 | +procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF} ); |
| 23 | +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication; const AHeader: string = AUTHORIZATION; const ARealmMessage: string = 'Enter credentials'): THorseCallback; |
| 24 | + |
| 25 | +implementation |
| 26 | + |
| 27 | +var |
| 28 | + Header: string; |
| 29 | + RealmMessage: string; |
| 30 | + Authenticate: THorseBasicAuthentication; |
| 31 | + |
| 32 | +function HorseBasicAuthentication(const AAuthenticate: THorseBasicAuthentication; const AHeader: string = AUTHORIZATION; const ARealmMessage: string = 'Enter credentials'): THorseCallback; |
| 33 | +begin |
| 34 | + Header := AHeader; |
| 35 | + RealmMessage := ARealmMessage; |
| 36 | + Authenticate := AAuthenticate; |
| 37 | + Result := Middleware; |
| 38 | +end; |
| 39 | + |
| 40 | +procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)} TNextProc {$ELSE} TProc {$ENDIF}); |
| 41 | +const |
| 42 | + BASIC_AUTH = 'basic '; |
| 43 | +var |
| 44 | + LBasicAuthenticationEncode: string; |
| 45 | + LBase64String: string; |
| 46 | + LBasicAuthenticationDecode: TStringList; |
| 47 | + LIsAuthenticated: Boolean; |
| 48 | +begin |
| 49 | + LBasicAuthenticationEncode := Req.Headers[Header]; |
| 50 | + if LBasicAuthenticationEncode.Trim.IsEmpty and not Req.Query.TryGetValue(Header, LBasicAuthenticationEncode) then |
| 51 | + begin |
| 52 | + Res.Send('Authorization not found').Status(THTTPStatus.Unauthorized).RawWebResponse{$IF DEFINED(FPC)}.WWWAuthenticate := Format('Basic realm=%s', [RealmMessage]); {$ELSE}.Realm := RealmMessage; {$ENDIF}) |
| 53 | + raise EHorseCallbackInterrupted.Create; |
| 54 | + end; |
| 55 | + if not LBasicAuthenticationEncode.ToLower.StartsWith(BASIC_AUTH) then |
| 56 | + begin |
| 57 | + Res.Send('Invalid authorization type').Status(THTTPStatus.Unauthorized); |
| 58 | + raise EHorseCallbackInterrupted.Create; |
| 59 | + end; |
| 60 | + LBasicAuthenticationDecode := TStringList.Create; |
| 61 | + try |
| 62 | + LBasicAuthenticationDecode.Delimiter := ':'; |
| 63 | + LBase64String := LBasicAuthenticationEncode.Replace(BASIC_AUTH, '', [rfIgnoreCase]); |
| 64 | + LBasicAuthenticationDecode.DelimitedText := {$IF DEFINED(FPC)}DecodeStringBase64(LBase64String){$ELSE}TBase64Encoding.Base64.Decode(LBase64String){$ENDIF}; |
| 65 | + try |
| 66 | + LIsAuthenticated := Authenticate(LBasicAuthenticationDecode.Strings[0], LBasicAuthenticationDecode.Strings[1]); |
| 67 | + except |
| 68 | + on E: exception do |
| 69 | + begin |
| 70 | + Res.Send(E.Message).Status(THTTPStatus.InternalServerError); |
| 71 | + raise EHorseCallbackInterrupted.Create; |
| 72 | + end; |
| 73 | + end; |
| 74 | + finally |
| 75 | + LBasicAuthenticationDecode.Free; |
| 76 | + end; |
| 77 | + if not LIsAuthenticated then |
| 78 | + begin |
| 79 | + Res.Send('Unauthorized').Status(THTTPStatus.Unauthorized); |
| 80 | + raise EHorseCallbackInterrupted.Create; |
| 81 | + end; |
| 82 | + Next(); |
| 83 | +end; |
| 84 | + |
| 85 | +end. |
0 commit comments