UAuth.pas 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /// Unit UAuth: Authentication-Service mit Interface-Implementierung.
  2. /// Testet: uses, interface (mit GUID), Klasse mit Vererbung + Interface,
  3. /// Sichtbarkeitsbereiche (private/public), Felder, Properties,
  4. /// Methoden, Constructor, Destructor, Funktionsaufrufe.
  5. unit UAuth;
  6. interface
  7. uses
  8. System.SysUtils,
  9. System.Classes;
  10. type
  11. /// Interface für Token-Validierung
  12. ITokenValidator = interface
  13. ['{11111111-1111-1111-1111-111111111111}']
  14. function Validate(const AToken: string): Boolean;
  15. end;
  16. /// Auth-Service mit Token-Validierung
  17. TAuthService = class(TInterfacedObject, ITokenValidator)
  18. private
  19. FToken: string;
  20. FLoginCount: Integer;
  21. procedure IncLoginCount;
  22. protected
  23. function GetToken: string;
  24. public
  25. constructor Create;
  26. destructor Destroy; override;
  27. function Validate(const AToken: string): Boolean;
  28. function Login(const AUser, APass: string): string;
  29. property Token: string read GetToken;
  30. property LoginCount: Integer read FLoginCount;
  31. end;
  32. implementation
  33. { TAuthService }
  34. constructor TAuthService.Create;
  35. begin
  36. inherited Create;
  37. FToken := '';
  38. FLoginCount := 0;
  39. end;
  40. destructor TAuthService.Destroy;
  41. begin
  42. FToken := '';
  43. inherited Destroy;
  44. end;
  45. procedure TAuthService.IncLoginCount;
  46. begin
  47. Inc(FLoginCount);
  48. end;
  49. function TAuthService.GetToken: string;
  50. begin
  51. Result := FToken;
  52. end;
  53. function TAuthService.Validate(const AToken: string): Boolean;
  54. begin
  55. Result := AToken <> '';
  56. end;
  57. function TAuthService.Login(const AUser, APass: string): string;
  58. begin
  59. IncLoginCount;
  60. if Validate(AUser + ':' + APass) then
  61. begin
  62. FToken := AUser;
  63. Result := 'ok';
  64. end
  65. else
  66. Result := '';
  67. end;
  68. end.