2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 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.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2016-05-23 17:21:33 +02:00
|
|
|
|
|
|
|
unit MVCFramework.Middleware.JWT;
|
|
|
|
|
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
|
|
|
|
2016-05-23 17:21:33 +02:00
|
|
|
uses
|
2017-03-23 18:51:25 +01:00
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
System.Generics.Collections,
|
2016-05-23 17:21:33 +02:00
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
2019-03-06 17:38:26 +01:00
|
|
|
MVCFramework.JWT,
|
|
|
|
JsonDataObjects;
|
2016-05-23 17:21:33 +02:00
|
|
|
|
|
|
|
type
|
2019-06-11 19:42:03 +02:00
|
|
|
TMVCJWTDefaults = class sealed
|
|
|
|
public const
|
|
|
|
/// <summary>
|
2019-07-24 14:35:28 +02:00
|
|
|
/// Default authorization header name (RFC 6750)
|
2019-06-11 19:42:03 +02:00
|
|
|
/// </summary>
|
2019-07-24 14:35:28 +02:00
|
|
|
AUTHORIZATION_HEADER = 'Authorization';
|
2019-06-11 19:42:03 +02:00
|
|
|
/// <summary>
|
2019-07-07 17:25:11 +02:00
|
|
|
/// Default username header name
|
2019-06-11 19:42:03 +02:00
|
|
|
/// </summary>
|
|
|
|
USERNAME_HEADER = 'jwtusername';
|
|
|
|
/// <summary>
|
2019-07-07 17:25:11 +02:00
|
|
|
/// Default password header name
|
2019-06-11 19:42:03 +02:00
|
|
|
/// </summary>
|
|
|
|
PASSWORD_HEADER = 'jwtpassword';
|
|
|
|
end;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
2016-06-23 11:42:16 +02:00
|
|
|
TJWTClaimsSetup = reference to procedure(const JWT: TJWT);
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
TMVCJWTAuthenticationMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
2016-05-23 17:21:33 +02:00
|
|
|
private
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler: IMVCAuthenticationHandler;
|
2016-05-23 17:21:33 +02:00
|
|
|
FClaimsToChecks: TJWTCheckableClaims;
|
2016-06-23 11:42:16 +02:00
|
|
|
FSetupJWTClaims: TJWTClaimsSetup;
|
2017-03-23 18:51:25 +01:00
|
|
|
FSecret: string;
|
2017-07-12 00:31:49 +02:00
|
|
|
FLeewaySeconds: Cardinal;
|
2017-09-07 00:10:21 +02:00
|
|
|
FLoginURLSegment: string;
|
2019-06-11 19:42:03 +02:00
|
|
|
FAuthorizationHeaderName: string;
|
|
|
|
FUserNameHeaderName: string;
|
|
|
|
FPasswordHeaderName: string;
|
2017-03-30 21:22:54 +02:00
|
|
|
protected
|
2018-05-17 21:55:32 +02:00
|
|
|
function NeedsToBeExtended(const JWTValue: TJWT): Boolean;
|
|
|
|
procedure ExtendExpirationTime(const JWTValue: TJWT);
|
2019-09-25 14:54:02 +02:00
|
|
|
procedure InternalRender(AJSONOb: TJDOJsonObject; AContentType: string; AContentEncoding: string;
|
|
|
|
AContext: TWebContext; AInstanceOwner: Boolean = True);
|
2018-05-17 21:55:32 +02: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);
|
2016-05-23 17:21:33 +02:00
|
|
|
public
|
2019-07-24 14:35:28 +02:00
|
|
|
/// <remarks>
|
2019-10-30 00:21:13 +01:00
|
|
|
/// WARNING! The AAuthorizationHeaderName, AUserNameHeaderName, and APasswordHeaderName parameters do not follow
|
2019-09-25 14:54:02 +02:00
|
|
|
/// the IETF national convention - RFC 6750;
|
2019-10-30 00:21:13 +01:00
|
|
|
/// Please use the other constructor!
|
2019-07-24 14:35:28 +02:00
|
|
|
/// </remarks>
|
2019-09-25 14:54:02 +02:00
|
|
|
constructor Create(AAuthenticationHandler: IMVCAuthenticationHandler; AConfigClaims: TJWTClaimsSetup;
|
|
|
|
ASecret: string = 'D3lph1MVCFram3w0rk'; ALoginURLSegment: string = '/login';
|
|
|
|
AClaimsToCheck: TJWTCheckableClaims = []; ALeewaySeconds: Cardinal = 300;
|
2019-06-11 19:42:03 +02:00
|
|
|
AAuthorizationHeaderName: string = TMVCJWTDefaults.AUTHORIZATION_HEADER;
|
|
|
|
AUserNameHeaderName: string = TMVCJWTDefaults.USERNAME_HEADER;
|
2019-07-26 13:51:11 +02:00
|
|
|
APasswordHeaderName: string = TMVCJWTDefaults.PASSWORD_HEADER); overload; virtual;
|
|
|
|
deprecated 'Issue #244: IETF (RFC-6750) - This constructor will be removed soon, please use the new one';
|
2019-09-25 14:54:02 +02:00
|
|
|
constructor Create(AAuthenticationHandler: IMVCAuthenticationHandler; ASecret: string = 'D3lph1MVCFram3w0rk';
|
|
|
|
ALoginURLSegment: string = '/login'; AConfigClaims: TJWTClaimsSetup = nil;
|
|
|
|
AClaimsToCheck: TJWTCheckableClaims = []; ALeewaySeconds: Cardinal = 300); overload; virtual;
|
2019-11-19 02:22:39 +01:00
|
|
|
property AuthorizationHeaderName: string read FAuthorizationHeaderName;
|
|
|
|
property UserNameHeaderName: string read FUserNameHeaderName;
|
|
|
|
property PasswordHeaderName: string read FPasswordHeaderName;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2018-05-17 21:55:32 +02:00
|
|
|
uses
|
|
|
|
System.NetEncoding,
|
|
|
|
System.DateUtils,
|
2019-07-07 17:25:11 +02:00
|
|
|
System.Math,
|
|
|
|
MVCFramework.Logger;
|
2017-07-12 00:31:49 +02:00
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TMVCJWTAuthenticationMiddleware }
|
2016-05-23 17:21:33 +02:00
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
constructor TMVCJWTAuthenticationMiddleware.Create(AAuthenticationHandler: IMVCAuthenticationHandler;
|
2019-09-25 14:54:02 +02:00
|
|
|
AConfigClaims: TJWTClaimsSetup; ASecret: string = 'D3lph1MVCFram3w0rk'; ALoginURLSegment: string = '/login';
|
|
|
|
AClaimsToCheck: TJWTCheckableClaims = []; ALeewaySeconds: Cardinal = 300;
|
2019-06-11 19:42:03 +02:00
|
|
|
AAuthorizationHeaderName: string = TMVCJWTDefaults.AUTHORIZATION_HEADER;
|
|
|
|
AUserNameHeaderName: string = TMVCJWTDefaults.USERNAME_HEADER;
|
|
|
|
APasswordHeaderName: string = TMVCJWTDefaults.PASSWORD_HEADER);
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2017-03-23 18:51:25 +01:00
|
|
|
FAuthenticationHandler := AAuthenticationHandler;
|
|
|
|
FSetupJWTClaims := AConfigClaims;
|
|
|
|
FClaimsToChecks := AClaimsToCheck;
|
|
|
|
FSecret := ASecret;
|
2017-07-14 17:38:51 +02:00
|
|
|
FLoginURLSegment := ALoginURLSegment;
|
2017-07-12 00:31:49 +02:00
|
|
|
FLeewaySeconds := ALeewaySeconds;
|
2019-06-11 19:42:03 +02:00
|
|
|
FAuthorizationHeaderName := AAuthorizationHeaderName;
|
|
|
|
FUserNameHeaderName := AUserNameHeaderName;
|
|
|
|
FPasswordHeaderName := APasswordHeaderName;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
|
2019-09-25 14:54:02 +02:00
|
|
|
constructor TMVCJWTAuthenticationMiddleware.Create(AAuthenticationHandler: IMVCAuthenticationHandler;
|
|
|
|
ASecret, ALoginURLSegment: string; AConfigClaims: TJWTClaimsSetup; AClaimsToCheck: TJWTCheckableClaims;
|
|
|
|
ALeewaySeconds: Cardinal);
|
2019-07-26 13:51:11 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FAuthenticationHandler := AAuthenticationHandler;
|
|
|
|
FSetupJWTClaims := AConfigClaims;
|
|
|
|
FClaimsToChecks := AClaimsToCheck;
|
|
|
|
FSecret := ASecret;
|
|
|
|
FLoginURLSegment := ALoginURLSegment;
|
|
|
|
FLeewaySeconds := ALeewaySeconds;
|
|
|
|
FAuthorizationHeaderName := TMVCJWTDefaults.AUTHORIZATION_HEADER;
|
|
|
|
FUserNameHeaderName := TMVCJWTDefaults.USERNAME_HEADER;
|
|
|
|
FPasswordHeaderName := TMVCJWTDefaults.PASSWORD_HEADER;
|
|
|
|
end;
|
|
|
|
|
2018-05-17 21:55:32 +02:00
|
|
|
procedure TMVCJWTAuthenticationMiddleware.ExtendExpirationTime(const JWTValue: TJWT);
|
|
|
|
begin
|
|
|
|
JWTValue.Claims.ExpirationTime := Max(JWTValue.Claims.ExpirationTime, Now) +
|
|
|
|
(JWTValue.LeewaySeconds + JWTValue.LiveValidityWindowInSeconds) * OneSecond;
|
|
|
|
end;
|
|
|
|
|
2019-03-06 22:28:22 +01:00
|
|
|
procedure TMVCJWTAuthenticationMiddleware.InternalRender(AJSONOb: TJDOJsonObject;
|
|
|
|
AContentType, AContentEncoding: string; AContext: TWebContext; AInstanceOwner: Boolean);
|
2017-03-20 19:08:01 +01:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
Encoding: TEncoding;
|
|
|
|
ContentType, JValue: string;
|
2017-03-20 19:08:01 +01:00
|
|
|
begin
|
2019-03-06 17:38:26 +01:00
|
|
|
JValue := AJSONOb.ToJSON;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
|
|
|
AContext.Response.RawWebResponse.ContentType := AContentType + '; charset=' + AContentEncoding;
|
2017-03-23 18:51:25 +01:00
|
|
|
ContentType := AContentType + '; charset=' + AContentEncoding;
|
|
|
|
|
|
|
|
Encoding := TEncoding.GetEncoding(AContentEncoding);
|
2017-03-20 19:08:01 +01:00
|
|
|
try
|
2018-05-17 21:55:32 +02:00
|
|
|
AContext.Response.SetContentStream(TBytesStream.Create(TEncoding.Convert(TEncoding.Default, Encoding,
|
|
|
|
TEncoding.Default.GetBytes(JValue))), ContentType);
|
2017-03-20 19:08:01 +01:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
Encoding.Free;
|
2017-03-20 19:08:01 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
if AInstanceOwner then
|
2019-03-06 17:38:26 +01:00
|
|
|
FreeAndNil(AJSONOb)
|
2017-03-20 19:08:01 +01:00
|
|
|
end;
|
|
|
|
|
2018-05-17 21:55:32 +02:00
|
|
|
function TMVCJWTAuthenticationMiddleware.NeedsToBeExtended(const JWTValue: TJWT): Boolean;
|
|
|
|
var
|
|
|
|
lWillExpireIn: Int64;
|
|
|
|
begin
|
|
|
|
lWillExpireIn := SecondsBetween(Now, JWTValue.Claims.ExpirationTime);
|
|
|
|
Result := lWillExpireIn <= JWTValue.LiveValidityWindowInSeconds;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCJWTAuthenticationMiddleware.OnAfterControllerAction(AContext: TWebContext; const AActionName: string;
|
2017-03-23 18:51:25 +01:00
|
|
|
const AHandled: Boolean);
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
// Implement as needed
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
|
2018-05-17 21:55:32 +02:00
|
|
|
procedure TMVCJWTAuthenticationMiddleware.OnBeforeControllerAction(AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionName: string; var AHandled: Boolean);
|
2016-05-23 17:21:33 +02:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
AuthRequired: Boolean;
|
|
|
|
IsAuthorized: Boolean;
|
|
|
|
JWTValue: TJWT;
|
|
|
|
AuthHeader: string;
|
|
|
|
AuthToken: string;
|
2017-07-12 00:31:49 +02:00
|
|
|
ErrorMsg: string;
|
2019-07-24 20:48:22 +02:00
|
|
|
const
|
|
|
|
AUTH_SCHEMA = 'Bearer';
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
|
|
|
// check if the resource is protected
|
2019-07-26 13:51:11 +02:00
|
|
|
if Assigned(FAuthenticationHandler) then
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
2019-07-26 13:51:11 +02:00
|
|
|
FAuthenticationHandler.OnRequest(AContext, AControllerQualifiedClassName, AActionName, AuthRequired);
|
|
|
|
if not AuthRequired then
|
|
|
|
begin
|
|
|
|
AHandled := False;
|
|
|
|
Exit;
|
|
|
|
end;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
// Checking token in subsequent requests
|
|
|
|
// ***************************************************
|
2017-07-12 00:31:49 +02:00
|
|
|
JWTValue := TJWT.Create(FSecret, FLeewaySeconds);
|
2016-05-23 17:21:33 +02:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
JWTValue.RegClaimsToChecks := Self.FClaimsToChecks;
|
2019-06-11 19:42:03 +02:00
|
|
|
AuthHeader := AContext.Request.Headers[FAuthorizationHeaderName];
|
2017-03-23 18:51:25 +01:00
|
|
|
if AuthHeader.IsEmpty then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, 'Authorization Required');
|
2016-05-23 17:21:33 +02:00
|
|
|
|
2019-07-24 20:48:22 +02:00
|
|
|
// retrieve the token from the "authentication Bearer" header
|
2017-03-23 18:51:25 +01:00
|
|
|
AuthToken := '';
|
2019-07-24 20:48:22 +02:00
|
|
|
if AuthHeader.Substring(0, AUTH_SCHEMA.Length).ToLower = 'bearer' then
|
2017-05-17 22:32:45 +02:00
|
|
|
begin
|
2019-07-24 20:48:22 +02:00
|
|
|
AuthToken := AuthHeader.Remove(0, AUTH_SCHEMA.Length).Trim;
|
2019-01-18 18:11:27 +01:00
|
|
|
AuthToken := Trim(TNetEncoding.URL.Decode(AuthToken));
|
2017-05-17 22:32:45 +02:00
|
|
|
end;
|
2016-05-23 17:21:33 +02:00
|
|
|
|
2017-09-22 09:33:21 +02:00
|
|
|
if not JWTValue.LoadToken(AuthToken, ErrorMsg) then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, ErrorMsg);
|
2017-09-22 09:33:21 +02:00
|
|
|
|
|
|
|
if JWTValue.CustomClaims['username'].IsEmpty then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, 'Invalid Token, Authorization Required');
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2019-07-26 13:51:11 +02:00
|
|
|
AContext.LoggedUser.UserName := JWTValue.CustomClaims['username'];
|
|
|
|
AContext.LoggedUser.Roles.AddRange(JWTValue.CustomClaims['roles'].Split([',']));
|
|
|
|
AContext.LoggedUser.LoggedSince := JWTValue.Claims.IssuedAt;
|
|
|
|
AContext.LoggedUser.CustomData := JWTValue.CustomClaims.AsCustomData;
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2019-07-26 13:51:11 +02:00
|
|
|
if Assigned(FAuthenticationHandler) then
|
|
|
|
begin
|
2019-08-13 16:34:37 +02:00
|
|
|
FAuthenticationHandler.OnAuthorization(AContext, AContext.LoggedUser.Roles, AControllerQualifiedClassName,
|
|
|
|
AActionName, IsAuthorized);
|
2019-07-26 13:51:11 +02:00
|
|
|
if not IsAuthorized then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Forbidden, 'Authorization Forbidden');
|
2019-07-26 13:51:11 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
if JWTValue.LiveValidityWindowInSeconds > 0 then
|
|
|
|
begin
|
|
|
|
if NeedsToBeExtended(JWTValue) then
|
|
|
|
begin
|
|
|
|
ExtendExpirationTime(JWTValue);
|
|
|
|
AContext.Response.SetCustomHeader(FAuthorizationHeaderName, 'Bearer ' + JWTValue.GetToken);
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
end;
|
2019-07-26 13:51:11 +02:00
|
|
|
|
|
|
|
AHandled := False;
|
2016-05-23 17:21:33 +02:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
JWTValue.Free;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-05-17 21:55:32 +02:00
|
|
|
procedure TMVCJWTAuthenticationMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
2016-05-23 17:21:33 +02:00
|
|
|
var
|
2019-08-13 16:34:37 +02:00
|
|
|
LUsername: string;
|
|
|
|
LPassword: string;
|
2019-08-13 16:50:56 +02:00
|
|
|
LBasicAuthHeader: string;
|
2019-08-13 16:34:37 +02:00
|
|
|
LBasicAuthParts: TArray<string>;
|
2019-07-24 14:35:28 +02:00
|
|
|
LRolesList: TList<string>;
|
|
|
|
LSessionData: TSessionData;
|
|
|
|
LIsValid: Boolean;
|
|
|
|
LJWTValue: TJWT;
|
|
|
|
LCustomPair: TPair<string, string>;
|
|
|
|
LJsonObject: TJDOJsonObject;
|
2019-09-25 14:54:02 +02:00
|
|
|
lJObj: TJsonObject;
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
2019-07-22 21:51:28 +02:00
|
|
|
if SameText(AContext.Request.PathInfo, FLoginURLSegment) then
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
2019-08-13 16:50:56 +02:00
|
|
|
LBasicAuthHeader := AContext.Request.Headers[FAuthorizationHeaderName];
|
|
|
|
if LBasicAuthHeader.IsEmpty then
|
2016-05-23 17:21:33 +02:00
|
|
|
begin
|
2019-09-25 14:54:02 +02:00
|
|
|
// read from headers
|
2019-07-24 14:35:28 +02:00
|
|
|
LUsername := TNetEncoding.URL.Decode(AContext.Request.Headers[FUserNameHeaderName]);
|
|
|
|
LPassword := TNetEncoding.URL.Decode(AContext.Request.Headers[FPasswordHeaderName]);
|
2019-09-25 14:54:02 +02:00
|
|
|
|
|
|
|
// read from content
|
|
|
|
if LUsername.IsEmpty then
|
|
|
|
begin
|
|
|
|
LUsername := AContext.Request.ContentFields[FUserNameHeaderName];
|
|
|
|
LPassword := AContext.Request.ContentFields[FPasswordHeaderName];
|
|
|
|
end;
|
|
|
|
|
|
|
|
// read from json content
|
|
|
|
if LUsername.IsEmpty then
|
|
|
|
begin
|
|
|
|
lJObj := nil;
|
|
|
|
try
|
|
|
|
lJObj := TJsonBaseObject.Parse(AContext.Request.Body) as TJsonObject;
|
|
|
|
except
|
|
|
|
end;
|
|
|
|
try
|
|
|
|
if Assigned(lJObj) then
|
|
|
|
begin
|
|
|
|
LUsername := lJObj.S[FUserNameHeaderName];
|
|
|
|
LPassword := lJObj.S[FPasswordHeaderName];
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lJObj.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-07-24 14:35:28 +02:00
|
|
|
if (LUsername.IsEmpty) or (LPassword.IsEmpty) then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, 'Username and password required');
|
2019-07-24 14:35:28 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2019-08-13 16:50:56 +02:00
|
|
|
if not LBasicAuthHeader.StartsWith('basic', True) then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, 'Invalid authorization type');
|
|
|
|
|
2019-08-13 16:50:56 +02:00
|
|
|
LBasicAuthHeader := LBasicAuthHeader.Remove(0, 'basic'.Length).Trim;
|
|
|
|
LBasicAuthParts := TBase64Encoding.Base64.Decode(LBasicAuthHeader).Split([':']);
|
2019-08-13 16:34:37 +02:00
|
|
|
|
2019-08-13 16:50:56 +02:00
|
|
|
if Length(LBasicAuthParts) <> 2 then
|
2019-08-13 16:34:37 +02:00
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Unauthorized, 'Invalid authorization type');
|
|
|
|
|
2019-09-25 14:54:02 +02:00
|
|
|
LUsername := LBasicAuthParts[0];
|
2019-08-13 16:34:37 +02:00
|
|
|
LPassword := LBasicAuthParts[1];
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
// check the authorization for the requested resource
|
2019-07-24 14:35:28 +02:00
|
|
|
LRolesList := TList<string>.Create;
|
2016-05-23 17:21:33 +02:00
|
|
|
try
|
2019-07-24 14:35:28 +02:00
|
|
|
LSessionData := TSessionData.Create;
|
2016-05-23 17:21:33 +02:00
|
|
|
try
|
2019-08-13 16:34:37 +02:00
|
|
|
if Assigned(FAuthenticationHandler) then
|
|
|
|
begin
|
|
|
|
FAuthenticationHandler.OnAuthentication(AContext, LUsername, LPassword, LRolesList, LIsValid, LSessionData);
|
|
|
|
if not LIsValid then
|
|
|
|
raise EMVCJWTException.Create(HTTP_STATUS.Forbidden, 'Forbidden');
|
|
|
|
end;
|
2019-07-26 13:51:11 +02:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
LJWTValue := TJWT.Create(FSecret, FLeewaySeconds);
|
|
|
|
try
|
|
|
|
// let's user config claims and custom claims
|
|
|
|
if not Assigned(FSetupJWTClaims) then
|
|
|
|
raise EMVCJWTException.Create('SetupJWTClaims not set');
|
2019-03-06 17:38:26 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
FSetupJWTClaims(LJWTValue);
|
2019-03-06 17:38:26 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
// these claims are mandatory and managed by the middleware
|
|
|
|
if not LJWTValue.CustomClaims['username'].IsEmpty then
|
2019-09-25 14:54:02 +02:00
|
|
|
raise EMVCJWTException.Create
|
|
|
|
('Custom claim "username" is reserved and cannot be modified in the JWT setup');
|
2019-03-06 17:38:26 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
if not LJWTValue.CustomClaims['roles'].IsEmpty then
|
|
|
|
raise EMVCJWTException.Create('Custom claim "roles" is reserved and cannot be modified in the JWT setup');
|
2019-03-06 17:38:26 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
LJWTValue.CustomClaims['username'] := LUsername;
|
|
|
|
LJWTValue.CustomClaims['roles'] := string.Join(',', LRolesList.ToArray);
|
2019-03-06 17:38:26 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
if LJWTValue.LiveValidityWindowInSeconds > 0 then
|
|
|
|
if NeedsToBeExtended(LJWTValue) then
|
|
|
|
ExtendExpirationTime(LJWTValue);
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
// setup the current logged user from the JWT
|
|
|
|
AContext.LoggedUser.Roles.AddRange(LRolesList);
|
|
|
|
AContext.LoggedUser.UserName := LJWTValue.CustomClaims['username'];
|
|
|
|
AContext.LoggedUser.LoggedSince := LJWTValue.Claims.IssuedAt;
|
|
|
|
AContext.LoggedUser.Realm := LJWTValue.Claims.Subject;
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
if LSessionData.Count > 0 then
|
|
|
|
begin
|
|
|
|
AContext.LoggedUser.CustomData := TMVCCustomData.Create;
|
|
|
|
for LCustomPair in LSessionData do
|
2019-07-26 13:51:11 +02:00
|
|
|
begin
|
2019-08-13 16:34:37 +02:00
|
|
|
AContext.LoggedUser.CustomData.AddOrSetValue(LCustomPair.Key, LCustomPair.Value);
|
|
|
|
if not LJWTValue.CustomClaims.Items[LCustomPair.Key].IsEmpty then
|
|
|
|
raise EMVCJWTException.CreateFmt('JWT Error: "%s" is a reserved key name', [LCustomPair.Key]);
|
|
|
|
LJWTValue.CustomClaims.Items[LCustomPair.Key] := LCustomPair.Value;
|
2019-07-26 13:51:11 +02:00
|
|
|
end;
|
2019-08-13 16:34:37 +02:00
|
|
|
end;
|
2017-09-07 00:10:21 +02:00
|
|
|
|
2019-08-13 16:34:37 +02:00
|
|
|
LJsonObject := TJDOJsonObject.Create;
|
|
|
|
try
|
|
|
|
LJsonObject.S['token'] := LJWTValue.GetToken;
|
2019-09-25 14:54:02 +02:00
|
|
|
InternalRender(LJsonObject, TMVCMediaType.APPLICATION_JSON, TMVCConstants.DEFAULT_CONTENT_CHARSET,
|
|
|
|
AContext, False);
|
2019-07-26 13:51:11 +02:00
|
|
|
finally
|
2019-08-13 16:34:37 +02:00
|
|
|
LJsonObject.Free;
|
2019-03-06 17:38:26 +01:00
|
|
|
end;
|
2019-08-13 16:34:37 +02:00
|
|
|
AHandled := True;
|
|
|
|
finally
|
|
|
|
LJWTValue.Free;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
finally
|
2019-07-24 14:35:28 +02:00
|
|
|
LSessionData.Free;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
finally
|
2019-07-24 14:35:28 +02:00
|
|
|
LRolesList.Free;
|
2016-05-23 17:21:33 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|