diff --git a/horse_basic_auth.dpk b/horse_basic_auth.dpk deleted file mode 100644 index a753560..0000000 --- a/horse_basic_auth.dpk +++ /dev/null @@ -1,38 +0,0 @@ -package horse_basic_auth; - -{$R *.res} -{$IFDEF IMPLICITBUILDING This IFDEF should not be used by users} -{$ALIGN 8} -{$ASSERTIONS ON} -{$BOOLEVAL OFF} -{$DEBUGINFO OFF} -{$EXTENDEDSYNTAX ON} -{$IMPORTEDDATA ON} -{$IOCHECKS ON} -{$LOCALSYMBOLS ON} -{$LONGSTRINGS ON} -{$OPENSTRINGS ON} -{$OPTIMIZATION OFF} -{$OVERFLOWCHECKS OFF} -{$RANGECHECKS OFF} -{$REFERENCEINFO ON} -{$SAFEDIVIDE OFF} -{$STACKFRAMES ON} -{$TYPEDADDRESS OFF} -{$VARSTRINGCHECKS ON} -{$WRITEABLECONST OFF} -{$MINENUMSIZE 1} -{$IMAGEBASE $400000} -{$DEFINE DEBUG} -{$ENDIF IMPLICITBUILDING} -{$RUNONLY} -{$IMPLICITBUILD ON} - -requires - rtl; - -contains - Horse.BasicAuthentication in 'src\Horse.BasicAuthentication.pas'; - -end. - diff --git a/horse_basic_auth.dproj b/horse_basic_auth.dproj deleted file mode 100644 index 7f9dcc7..0000000 --- a/horse_basic_auth.dproj +++ /dev/null @@ -1,999 +0,0 @@ - - - {511A8BA8-60C7-4828-9553-788368AE77BA} - horse_basic_auth.dpk - 18.8 - None - True - Debug - Win32 - 1 - Package - - - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Cfg_1 - true - true - - - true - Base - true - - - .\$(Platform)\$(Config) - .\$(Platform)\$(Config) - false - false - false - false - false - true - true - System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) - All - horse_basic_auth - $(DCC_UnitSearchPath);modules\.dcp;modules\.dcu;modules;modules\horse\src - true - - - None - android-support-v4.dex.jar;cloud-messaging.dex.jar;fmx.dex.jar;google-analytics-v2.dex.jar;google-play-billing.dex.jar;google-play-licensing.dex.jar;google-play-services-ads-7.0.0.dex.jar;google-play-services-analytics-7.0.0.dex.jar;google-play-services-base-7.0.0.dex.jar;google-play-services-gcm-7.0.0.dex.jar;google-play-services-identity-7.0.0.dex.jar;google-play-services-maps-7.0.0.dex.jar;google-play-services-panorama-7.0.0.dex.jar;google-play-services-plus-7.0.0.dex.jar;google-play-services-wallet-7.0.0.dex.jar - - - package=com.embarcadero.$(MSBuildProjectName);label=$(MSBuildProjectName);versionCode=1;versionName=1.0.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey= - Debug - true - Base - true - None - android-support-v4.dex.jar;cloud-messaging.dex.jar;fmx.dex.jar;google-analytics-v2.dex.jar;google-play-billing.dex.jar;google-play-licensing.dex.jar;google-play-services-ads-7.0.0.dex.jar;google-play-services-analytics-7.0.0.dex.jar;google-play-services-base-7.0.0.dex.jar;google-play-services-gcm-7.0.0.dex.jar;google-play-services-identity-7.0.0.dex.jar;google-play-services-maps-7.0.0.dex.jar;google-play-services-panorama-7.0.0.dex.jar;google-play-services-plus-7.0.0.dex.jar;google-play-services-wallet-7.0.0.dex.jar - - - None - - - None - - - None - - - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - Debug - true - CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= - 1033 - - - DEBUG;$(DCC_Define) - true - false - true - true - true - - - false - true - 1033 - - - false - RELEASE;$(DCC_Define) - 0 - 0 - - - - MainSource - - - - - Cfg_2 - Base - - - Base - - - Cfg_1 - Base - - - - Delphi.Personality.12 - Package - - - - horse_basic_auth.dpk - - - Microsoft Office 2000 Sample Automation Server Wrapper Components - Microsoft Office XP Sample Automation Server Wrapper Components - - - - - - true - - - - - true - - - - - true - - - - - true - - - - - true - - - - - horse_basic_auth.bpl - true - - - - - 1 - - - 0 - - - - - classes - 1 - - - classes - 1 - - - - - res\xml - 1 - - - res\xml - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\armeabi - 1 - - - library\lib\armeabi - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\mips - 1 - - - library\lib\mips - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-v21 - 1 - - - res\values-v21 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-small - 1 - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - res\drawable-xlarge - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - 1 - - - 1 - - - 0 - - - - - 1 - .framework - - - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - - - - 1 - - - 1 - - - 1 - - - - - - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 0 - - - - - library\lib\armeabi-v7a - 1 - - - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - - - - - - - - - - - False - False - False - False - False - True - False - - - 12 - - - - - diff --git a/samples/delphi/Samples.dpr b/samples/delphi/Samples.dpr index 9dd28a7..4c6a4c1 100644 --- a/samples/delphi/Samples.dpr +++ b/samples/delphi/Samples.dpr @@ -3,15 +3,24 @@ program Samples; {$APPTYPE CONSOLE} {$R *.res} -uses Horse, Horse.BasicAuthentication, System.SysUtils; +uses + Horse, + Horse.BasicAuthentication, // It's necessary to use the unit + System.SysUtils; begin + // It's necessary to add the middleware in the Horse: THorse.Use(HorseBasicAuthentication( function(const AUsername, APassword: string): Boolean begin + // Here inside you can access your database and validate if username and password are valid Result := AUsername.Equals('user') and APassword.Equals('password'); end)); + // The default header for receiving credentials is "Authorization". + // You can change, if necessary: + // THorse.Use(HorseBasicAuthentication(MyCallbackValidation, 'X-My-Header-Authorization')); + THorse.Get('/ping', procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc) begin diff --git a/samples/lazarus/Console.lpr b/samples/lazarus/Console.lpr index f23edb5..30dca17 100644 --- a/samples/lazarus/Console.lpr +++ b/samples/lazarus/Console.lpr @@ -7,7 +7,7 @@ cthreads, {$ENDIF}{$ENDIF} Horse, - Horse.BasicAuthentication, + Horse.BasicAuthentication, // It's necessary to use the unit SysUtils; procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc); @@ -22,12 +22,18 @@ procedure OnListen(Horse: THorse); function DoLogin(const AUsername, APassword: string): Boolean; begin + // Here inside you can access your database and validate if username and password are valid Result := AUsername.Equals('user') and APassword.Equals('password'); end; begin + // It's necessary to add the middleware in the Horse: THorse.Use(HorseBasicAuthentication(DoLogin)); + // The default header for receiving credentials is "Authorization". + // You can change, if necessary: + // THorse.Use(HorseBasicAuthentication(MyCallbackValidation, 'X-My-Header-Authorization')); + THorse.Get('/ping', GetPing); THorse.Listen(9000, OnListen); diff --git a/src/Horse.BasicAuthentication.pas b/src/Horse.BasicAuthentication.pas index 7e7c1a6..d25362a 100644 --- a/src/Horse.BasicAuthentication.pas +++ b/src/Horse.BasicAuthentication.pas @@ -1,7 +1,9 @@ unit Horse.BasicAuthentication; + {$IF DEFINED(FPC)} -{$MODE DELPHI}{$H+} + {$MODE DELPHI}{$H+} {$ENDIF} + interface uses @@ -10,7 +12,6 @@ interface {$ELSE} System.SysUtils, System.NetEncoding, System.Classes, {$ENDIF} - Horse, Horse.Commons; const @@ -65,6 +66,7 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED LBasicAuthenticationDecode := TStringList.Create; try LBasicAuthenticationDecode.Delimiter := ':'; + LBasicAuthenticationDecode.StrictDelimiter := True; LBase64String := LBasicAuthenticationEncode.Replace(BASIC_AUTH, '', [rfIgnoreCase]); LBasicAuthenticationDecode.DelimitedText := {$IF DEFINED(FPC)}DecodeStringBase64(LBase64String){$ELSE}TBase64Encoding.Base64.Decode(LBase64String){$ENDIF}; try