| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- /// Unit UAuth: Authentication-Service mit Interface-Implementierung.
- /// Testet: uses, interface (mit GUID), Klasse mit Vererbung + Interface,
- /// Sichtbarkeitsbereiche (private/public), Felder, Properties,
- /// Methoden, Constructor, Destructor, Funktionsaufrufe.
- unit UAuth;
- interface
- uses
- System.SysUtils,
- System.Classes;
- type
- /// Interface für Token-Validierung
- ITokenValidator = interface
- ['{11111111-1111-1111-1111-111111111111}']
- function Validate(const AToken: string): Boolean;
- end;
- /// Auth-Service mit Token-Validierung
- TAuthService = class(TInterfacedObject, ITokenValidator)
- private
- FToken: string;
- FLoginCount: Integer;
- procedure IncLoginCount;
- protected
- function GetToken: string;
- public
- constructor Create;
- destructor Destroy; override;
- function Validate(const AToken: string): Boolean;
- function Login(const AUser, APass: string): string;
- property Token: string read GetToken;
- property LoginCount: Integer read FLoginCount;
- end;
- implementation
- { TAuthService }
- constructor TAuthService.Create;
- begin
- inherited Create;
- FToken := '';
- FLoginCount := 0;
- end;
- destructor TAuthService.Destroy;
- begin
- FToken := '';
- inherited Destroy;
- end;
- procedure TAuthService.IncLoginCount;
- begin
- Inc(FLoginCount);
- end;
- function TAuthService.GetToken: string;
- begin
- Result := FToken;
- end;
- function TAuthService.Validate(const AToken: string): Boolean;
- begin
- Result := AToken <> '';
- end;
- function TAuthService.Login(const AUser, APass: string): string;
- begin
- IncLoginCount;
- if Validate(AUser + ':' + APass) then
- begin
- FToken := AUser;
- Result := 'ok';
- end
- else
- Result := '';
- end;
- end.
|