Added unit to contains auth handlers for basicauth and customauth

This commit is contained in:
danieleteti 2016-09-18 12:02:24 +02:00
parent f0670af618
commit 3157e6e018
4 changed files with 104 additions and 94 deletions

View File

@ -0,0 +1,89 @@
unit AuthHandlersU;
interface
uses
MVCFramework.Commons, System.Generics.Collections;
type
TAuthHandlerBase = class abstract(TInterfacedObject, IMVCAuthenticationHandler)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); virtual; abstract;
procedure OnAuthentication(const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>;
var IsValid: Boolean; const SessionData: TDictionary<string, string>); virtual;
procedure OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const ControllerQualifiedClassName: string; const ActionName: string;
var IsAuthorized: Boolean); virtual;
end;
TBasicAuthHandler = class(TAuthHandlerBase)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); override;
end;
TCustomAuthHandler = class(TAuthHandlerBase)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); override;
end;
implementation
uses
System.SysUtils;
procedure TAuthHandlerBase.OnAuthentication(
const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>; var IsValid: Boolean;
const SessionData: TDictionary<string, string>);
begin
UserRoles.Clear;
IsValid := UserName = Password;
if not IsValid then
Exit;
if UserName = 'user1' then
begin
IsValid := True;
UserRoles.Add('role1');
end;
if UserName = 'user2' then
begin
IsValid := True;
UserRoles.Add('role2');
end;
end;
procedure TAuthHandlerBase.OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const
ControllerQualifiedClassName, ActionName: string;
var
IsAuthorized:
Boolean);
begin
IsAuthorized := False;
if (ActionName = 'OnlyRole1') or (ActionName = 'OnlyRole1Session') then
IsAuthorized := UserRoles.Contains('role1');
if ActionName = 'OnlyRole2' then
IsAuthorized := UserRoles.Contains('role2');
end;
procedure TBasicAuthHandler.OnRequest(const ControllerQualifiedClassName, ActionName: string;
var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerController');
end;
procedure TCustomAuthHandler.OnRequest(const ControllerQualifiedClassName,
ActionName: string; var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerControllerCustomAuth');
end;
end.

View File

@ -35,7 +35,8 @@ uses
RTTIUtilsU in '..\..\sources\RTTIUtilsU.pas',
uGlobalVars in '..\..\sources\uGlobalVars.pas',
DuckListU in '..\..\sources\DuckListU.pas',
TestServerControllerPrivateU in 'TestServerControllerPrivateU.pas';
TestServerControllerPrivateU in 'TestServerControllerPrivateU.pas',
AuthHandlersU in 'AuthHandlersU.pas';
{$R *.res}

View File

@ -131,6 +131,7 @@
<DCCReference Include="..\..\sources\uGlobalVars.pas"/>
<DCCReference Include="..\..\sources\DuckListU.pas"/>
<DCCReference Include="TestServerControllerPrivateU.pas"/>
<DCCReference Include="AuthHandlersU.pas"/>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>
@ -200,7 +201,16 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<DeployClass Name="ProjectOSXResource">
<Platform Name="OSX32">
<RemoteDir>Contents\Resources</RemoteDir>
@ -512,16 +522,7 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="DependencyModule">
<Platform Name="Win32">
<Operation>0</Operation>
<Extensions>.dll;.bpl</Extensions>
</Platform>
<Platform Name="OSX32">
<Operation>1</Operation>
<Extensions>.dylib</Extensions>
</Platform>
</DeployClass>
<DeployClass Name="ProjectiOSDeviceResourceRules"/>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>

View File

@ -25,34 +25,7 @@ implementation
uses
TestServerControllerU, TestServerControllerExceptionU, SpeedMiddlewareU,
MVCFramework.Middleware.Authentication, System.Generics.Collections,
MVCFramework.Commons, TestServerControllerPrivateU;
type
TAuthHandlerBase = class abstract(TInterfacedObject, IMVCAuthenticationHandler)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); virtual; abstract;
procedure OnAuthentication(const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>;
var IsValid: Boolean; const SessionData: TDictionary<string, string>); virtual;
procedure OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const ControllerQualifiedClassName: string; const ActionName: string;
var IsAuthorized: Boolean); virtual;
end;
TBasicAuthHandler = class(TAuthHandlerBase)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); override;
end;
TCustomAuthHandler = class(TAuthHandlerBase)
public
procedure OnRequest(const ControllerQualifiedClassName: string;
const ActionName: string; var AuthenticationRequired: Boolean); override;
end;
MVCFramework.Commons, TestServerControllerPrivateU, AuthHandlersU;
procedure Twm.WebModuleCreate(Sender: TObject);
begin
@ -74,61 +47,7 @@ begin
end;
{ TSampleAuth }
procedure TAuthHandlerBase.OnAuthentication(
const UserName: string; const Password: string;
UserRoles: System.Generics.Collections.TList<System.string>; var IsValid: Boolean;
const SessionData: TDictionary<string, string>);
begin
UserRoles.Clear;
IsValid := UserName = Password;
if not IsValid then
Exit;
if UserName = 'user1' then
begin
IsValid := True;
UserRoles.Add('role1');
end;
if UserName = 'user2' then
begin
IsValid := True;
UserRoles.Add('role2');
end;
end;
procedure TAuthHandlerBase.OnAuthorization(UserRoles
: System.Generics.Collections.TList<System.string>;
const
ControllerQualifiedClassName, ActionName: string;
var
IsAuthorized:
Boolean);
begin
IsAuthorized := False;
if (ActionName = 'OnlyRole1') or (ActionName = 'OnlyRole1Session') then
IsAuthorized := UserRoles.Contains('role1');
if ActionName = 'OnlyRole2' then
IsAuthorized := UserRoles.Contains('role2');
end;
{ TBasicAuthHandler }
procedure TBasicAuthHandler.OnRequest(const ControllerQualifiedClassName, ActionName: string;
var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerController');
end;
{ TCustomAuthHandler }
procedure TCustomAuthHandler.OnRequest(const ControllerQualifiedClassName,
ActionName: string; var AuthenticationRequired: Boolean);
begin
AuthenticationRequired := ControllerQualifiedClassName.EndsWith
('TTestPrivateServerControllerCustomAuth');
end;
end.