2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
unit MVCFramework.Middleware.Authentication;
|
|
|
|
|
2016-12-30 20:41:55 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
interface
|
2016-12-30 20:41:55 +01:00
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
uses
|
2017-03-23 18:51:25 +01:00
|
|
|
System.SysUtils,
|
|
|
|
System.StrUtils,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
MVCFramework.TypesAliases;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
|
|
type
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
TMVCBasicAuthenticationMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
2017-03-23 18:51:25 +01:00
|
|
|
private
|
|
|
|
FAuthenticationHandler: IMVCAuthenticationHandler;
|
2015-04-01 17:01:23 +02:00
|
|
|
FRealm: string;
|
2017-03-23 18:51:25 +01:00
|
|
|
protected
|
|
|
|
procedure OnBeforeRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string;
|
|
|
|
const AActionName: string;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean
|
|
|
|
);
|
2015-04-01 17:01:23 +02:00
|
|
|
public
|
2017-03-23 18:51:25 +01:00
|
|
|
constructor Create(
|
|
|
|
const AAuthenticationHandler: IMVCAuthenticationHandler;
|
|
|
|
const ARealm: string = 'DelphiMVCFramework REALM'
|
|
|
|
); virtual;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
TMVCCustomAuthenticationMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
|
|
|
private
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler: IMVCAuthenticationHandler;
|
2016-09-16 23:54:54 +02:00
|
|
|
FLoginUrl: string;
|
|
|
|
protected
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure OnBeforeRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string;
|
|
|
|
const AActionName: string;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure SendResponse(AContext: TWebContext; var AHandled: Boolean; AHttpStatus: Word = HTTP_STATUS.Unauthorized);
|
|
|
|
procedure DoLogin(AContext: TWebContext; var AHandled: Boolean);
|
|
|
|
procedure DoLogout(AContext: TWebContext; var AHandled: Boolean);
|
2016-09-16 23:54:54 +02:00
|
|
|
public
|
|
|
|
constructor Create(
|
2017-03-23 18:51:25 +01:00
|
|
|
const AAuthenticationHandler: IMVCAuthenticationHandler;
|
|
|
|
const ALoginUrl: string = '/system/users/logged'
|
2016-09-16 23:54:54 +02:00
|
|
|
); virtual;
|
|
|
|
end;
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
const
|
2016-04-03 22:35:27 +02:00
|
|
|
CONTENT_HTML_FORMAT = '<html><body><h1>%s</h1><p>%s</p></body></html>';
|
2015-04-01 17:01:23 +02:00
|
|
|
CONTENT_401_NOT_AUTHORIZED = '401: Not authorized';
|
|
|
|
CONTENT_403_FORBIDDEN = '403: Forbidden';
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TMVCBasicAuthenticationMiddleware }
|
2015-04-01 17:01:23 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
constructor TMVCBasicAuthenticationMiddleware.Create(
|
|
|
|
const AAuthenticationHandler: IMVCAuthenticationHandler;
|
|
|
|
const ARealm: string);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler := AAuthenticationHandler;
|
|
|
|
FRealm := ARealm;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCBasicAuthenticationMiddleware.OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCBasicAuthenticationMiddleware.OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionName: string;
|
|
|
|
var AHandled: Boolean);
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
procedure SendWWWAuthenticate;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.Clear;
|
|
|
|
if AContext.Request.ClientPreferHTML then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.ContentType := TMVCMediaType.TEXT_HTML;
|
|
|
|
AContext.Response.RawWebResponse.Content := Format(CONTENT_HTML_FORMAT, [CONTENT_401_NOT_AUTHORIZED, AContext.Config[TMVCConfigKey.ServerName]]);
|
2015-04-01 17:01:23 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
AContext.Response.RawWebResponse.Content := CONTENT_401_NOT_AUTHORIZED + sLineBreak + AContext.Config[TMVCConfigKey.ServerName];
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.Unauthorized;
|
|
|
|
AContext.Response.SetCustomHeader('WWW-Authenticate', 'Basic realm=' + QuotedStr(FRealm));
|
|
|
|
AHandled := True;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure Send403Forbidden;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.Clear;
|
|
|
|
if AContext.Request.ClientPreferHTML then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.ContentType := TMVCMediaType.TEXT_HTML;
|
|
|
|
AContext.Response.RawWebResponse.Content := Format(CONTENT_HTML_FORMAT, [CONTENT_403_FORBIDDEN, AContext.Config[TMVCConfigKey.ServerName]]);
|
2015-04-01 17:01:23 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
AContext.Response.RawWebResponse.Content := CONTENT_403_FORBIDDEN + sLineBreak + AContext.Config[TMVCConfigKey.ServerName];
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.Forbidden;
|
|
|
|
AHandled := True;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
var
|
|
|
|
AuthRequired: Boolean;
|
|
|
|
IsValid, IsAuthorized: Boolean;
|
|
|
|
AuthHeader: string;
|
|
|
|
AuthPieces: TArray<string>;
|
|
|
|
RolesList: TList<string>;
|
|
|
|
SessionData: TSessionData;
|
|
|
|
SessionPair: TPair<string, string>;
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler.OnRequest(AControllerQualifiedClassName, AActionName, AuthRequired);
|
|
|
|
if not AuthRequired then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := False;
|
2015-04-01 17:01:23 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.LoadFromSession(AContext.Session);
|
|
|
|
IsValid := AContext.LoggedUser.IsValid;
|
|
|
|
if not IsValid then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AuthHeader := AContext.Request.Headers['Authorization'];
|
|
|
|
AuthHeader := TMVCSerializerHelpful.DecodeString(AuthHeader.Remove(0, 'Basic'.Length).Trim);
|
|
|
|
AuthPieces := AuthHeader.Split([':']);
|
|
|
|
if AuthHeader.IsEmpty or (Length(AuthPieces) <> 2) then
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
|
|
|
SendWWWAuthenticate;
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
RolesList := TList<string>.Create;
|
2015-04-01 17:01:23 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
SessionData := TSessionData.Create;
|
2016-02-23 22:33:21 +01:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler.OnAuthentication(AuthPieces[0], AuthPieces[1], RolesList, IsValid, SessionData);
|
|
|
|
if IsValid then
|
2016-02-23 22:33:21 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.Roles.AddRange(RolesList);
|
|
|
|
AContext.LoggedUser.UserName := AuthPieces[0];
|
|
|
|
AContext.LoggedUser.LoggedSince := Now;
|
|
|
|
AContext.LoggedUser.Realm := FRealm;
|
|
|
|
AContext.LoggedUser.SaveToSession(AContext.Session);
|
|
|
|
for SessionPair in SessionData do
|
|
|
|
AContext.Session[SessionPair.Key] := SessionPair.Value;
|
2016-02-23 22:33:21 +01:00
|
|
|
end;
|
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
SessionData.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
RolesList.Free;
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
IsAuthorized := False;
|
|
|
|
if IsValid then
|
|
|
|
FAuthenticationHandler.OnAuthorization(AContext.LoggedUser.Roles, AControllerQualifiedClassName, AActionName, IsAuthorized);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
if IsAuthorized then
|
|
|
|
AHandled := False
|
2015-04-01 17:01:23 +02:00
|
|
|
else
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
if IsValid then
|
2015-04-01 17:01:23 +02:00
|
|
|
Send403Forbidden
|
|
|
|
else
|
|
|
|
SendWWWAuthenticate;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCBasicAuthenticationMiddleware.OnBeforeRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean);
|
2015-04-01 17:01:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2015-04-01 17:01:23 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TMVCCustomAuthenticationMiddleware }
|
2016-09-16 23:54:54 +02:00
|
|
|
|
|
|
|
constructor TMVCCustomAuthenticationMiddleware.Create(
|
2017-03-23 18:51:25 +01:00
|
|
|
const AAuthenticationHandler: IMVCAuthenticationHandler;
|
|
|
|
const ALoginUrl: string);
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler := AAuthenticationHandler;
|
|
|
|
FLoginUrl := ALoginUrl.ToLower;
|
2016-09-16 23:54:54 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCustomAuthenticationMiddleware.DoLogin(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean);
|
2016-09-18 19:19:23 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo: TJSONObject;
|
|
|
|
UserName, Password: string;
|
|
|
|
RolesList: TList<string>;
|
|
|
|
SessionPair: TPair<string, string>;
|
|
|
|
SessionData: TSessionData;
|
|
|
|
IsValid: Boolean;
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.SessionStop(False);
|
|
|
|
AContext.LoggedUser.Clear;
|
|
|
|
if not AContext.Request.ThereIsRequestBody then
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := True;
|
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.BadRequest;
|
|
|
|
AContext.Response.ContentType := TMVCMediaType.APPLICATION_JSON;
|
|
|
|
AContext.Response.RawWebResponse.Content := '{"status":"KO", "message":"username and password are mandatory in the body request as json object"}';
|
2016-09-18 19:19:23 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
Jo := TJSONObject.ParseJSONValue(AContext.Request.Body) as TJSONObject;
|
|
|
|
if not Assigned(Jo) then
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := True;
|
|
|
|
SendResponse(AContext, AHandled, HTTP_STATUS.BadRequest);
|
2016-09-18 19:19:23 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
UserName := EmptyStr;
|
|
|
|
if (Jo.Get('username') <> nil) then
|
|
|
|
UserName := Jo.Get('username').JsonValue.Value;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
Password := EmptyStr;
|
|
|
|
if (Jo.Get('password') <> nil) then
|
|
|
|
Password := Jo.Get('password').JsonValue.Value;
|
|
|
|
|
|
|
|
if UserName.IsEmpty or Password.IsEmpty then
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := True;
|
|
|
|
SendResponse(AContext, AHandled);
|
2016-09-18 19:19:23 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
RolesList := TList<string>.Create;
|
2016-09-18 19:19:23 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
SessionData := TSessionData.Create;
|
2016-09-18 19:19:23 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
IsValid := False;
|
|
|
|
FAuthenticationHandler.OnAuthentication(UserName, Password, RolesList, IsValid, SessionData);
|
|
|
|
if not IsValid then
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
SendResponse(AContext, AHandled);
|
2016-09-18 19:19:23 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.Roles.AddRange(RolesList);
|
|
|
|
AContext.LoggedUser.UserName := UserName;
|
|
|
|
AContext.LoggedUser.LoggedSince := Now;
|
|
|
|
AContext.LoggedUser.Realm := 'custom';
|
|
|
|
AContext.LoggedUser.SaveToSession(AContext.Session);
|
2016-09-18 19:19:23 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
for SessionPair in SessionData do
|
|
|
|
AContext.Session[SessionPair.Key] := SessionPair.Value;
|
2016-09-18 19:19:23 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.Response.StatusCode := HTTP_STATUS.OK;
|
|
|
|
AContext.Response.CustomHeaders.Values['X-LOGOUT-URL'] := FLoginUrl;
|
|
|
|
AContext.Response.CustomHeaders.Values['X-LOGOUT-METHOD'] := 'DELETE';
|
|
|
|
AContext.Response.ContentType := TMVCMediaType.APPLICATION_JSON;
|
|
|
|
AContext.Response.RawWebResponse.Content := '{"status":"OK"}';
|
|
|
|
|
|
|
|
AHandled := True;
|
2016-09-18 19:19:23 +02:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
SessionData.Free;
|
2016-09-18 19:19:23 +02:00
|
|
|
end;
|
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
RolesList.Free;
|
2016-09-18 19:19:23 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCustomAuthenticationMiddleware.DoLogout(
|
|
|
|
AContext: TWebContext; var AHandled: Boolean);
|
2016-09-18 19:19:23 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.SessionStop(False);
|
|
|
|
SendResponse(AContext, AHandled, HTTP_STATUS.OK);
|
2016-09-18 19:19:23 +02:00
|
|
|
end;
|
|
|
|
|
2016-09-16 23:54:54 +02:00
|
|
|
procedure TMVCCustomAuthenticationMiddleware.OnAfterControllerAction(
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext: TWebContext; const AActionName: string;
|
|
|
|
const AHandled: Boolean);
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2016-09-16 23:54:54 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCCustomAuthenticationMiddleware.OnBeforeControllerAction(
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionName: string;
|
|
|
|
var AHandled: Boolean);
|
2016-09-16 23:54:54 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
IsValid: Boolean;
|
|
|
|
IsAuthorized: Boolean;
|
|
|
|
AuthRequired: Boolean;
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler.OnRequest(AControllerQualifiedClassName, AActionName, AuthRequired);
|
|
|
|
if not AuthRequired then
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := False;
|
2016-09-16 23:54:54 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.LoggedUser.LoadFromSession(AContext.Session);
|
|
|
|
IsValid := AContext.LoggedUser.IsValid;
|
|
|
|
if not IsValid then
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AContext.SessionStop(False);
|
|
|
|
SendResponse(AContext, AHandled);
|
2016-09-16 23:54:54 +02:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
IsAuthorized := False;
|
|
|
|
FAuthenticationHandler.OnAuthorization(AContext.LoggedUser.Roles, AControllerQualifiedClassName, AActionName, IsAuthorized);
|
|
|
|
if IsAuthorized then
|
|
|
|
AHandled := False
|
2016-09-16 23:54:54 +02:00
|
|
|
else
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
if IsValid then
|
|
|
|
SendResponse(AContext, AHandled, HTTP_STATUS.Forbidden)
|
2016-09-16 23:54:54 +02:00
|
|
|
else
|
2017-03-23 18:51:25 +01:00
|
|
|
SendResponse(AContext, AHandled, HTTP_STATUS.Unauthorized);
|
2016-09-16 23:54:54 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCustomAuthenticationMiddleware.OnBeforeRouting(
|
|
|
|
AContext: TWebContext; var AHandled: Boolean);
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
if (AContext.Request.PathInfo.ToLower = FLoginUrl) then
|
2016-09-16 23:54:54 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
AHandled := False;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
if (AContext.Request.HTTPMethod = httpPOST) and (AContext.Request.ContentType.StartsWith(TMVCMediaType.APPLICATION_JSON)) then
|
|
|
|
DoLogin(AContext, AHandled);
|
2016-09-16 23:54:54 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
if (AContext.Request.HTTPMethod = httpDELETE) then
|
|
|
|
DoLogout(AContext, AHandled);
|
|
|
|
end;
|
|
|
|
end;
|
2016-09-16 23:54:54 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCCustomAuthenticationMiddleware.SendResponse(
|
|
|
|
AContext: TWebContext; var AHandled: Boolean; AHttpStatus: Word);
|
|
|
|
var
|
|
|
|
IsPositive: Boolean;
|
|
|
|
Msg: string;
|
|
|
|
begin
|
|
|
|
AContext.LoggedUser.Clear;
|
|
|
|
AContext.Response.CustomHeaders.Values['X-LOGIN-URL'] := FLoginUrl;
|
|
|
|
AContext.Response.CustomHeaders.Values['X-LOGIN-METHOD'] := 'POST';
|
|
|
|
AContext.Response.StatusCode := AHttpStatus;
|
|
|
|
if AContext.Request.ClientPreferHTML then
|
|
|
|
begin
|
|
|
|
AContext.Response.ContentType := TMVCMediaType.TEXT_HTML;
|
|
|
|
AContext.Response.RawWebResponse.Content := Format(CONTENT_HTML_FORMAT, [IntToStr(AHttpStatus), AContext.Config[TMVCConfigKey.ServerName]]);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
IsPositive := (AHttpStatus div 100) = 2;
|
|
|
|
Msg := IfThen(IsPositive, 'OK', 'KO');
|
|
|
|
AContext.Response.ContentType := TMVCMediaType.APPLICATION_JSON;
|
|
|
|
AContext.Response.RawWebResponse.Content := '{"status":"' + Msg + '", "message":"' + IntToStr(AHttpStatus) + '"}';
|
|
|
|
end;
|
|
|
|
AHandled := True;
|
2016-09-16 23:54:54 +02:00
|
|
|
end;
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
end.
|