2017-07-12 00:31:49 +02:00
|
|
|
unit WebModuleUnit1;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.Classes,
|
|
|
|
Web.HTTPApp,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons;
|
|
|
|
|
|
|
|
type
|
|
|
|
TWebModule1 = class(TWebModule)
|
|
|
|
procedure WebModuleCreate(Sender: TObject);
|
|
|
|
|
|
|
|
private
|
|
|
|
MVC: TMVCEngine;
|
|
|
|
|
|
|
|
public
|
|
|
|
{ Public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
WebModuleClass: TComponentClass = TWebModule1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
uses
|
|
|
|
AppControllerU,
|
|
|
|
System.Generics.Collections,
|
|
|
|
AuthenticationU,
|
|
|
|
MVCFramework.Middleware.JWT,
|
|
|
|
MVCFramework.JWT,
|
2020-04-29 01:59:41 +02:00
|
|
|
MVCFramework.Middleware.StaticFiles,
|
2017-07-12 00:31:49 +02:00
|
|
|
System.DateUtils;
|
|
|
|
|
|
|
|
procedure TWebModule1.WebModuleCreate(Sender: TObject);
|
|
|
|
var
|
|
|
|
lClaimsSetup: TJWTClaimsSetup;
|
|
|
|
begin
|
|
|
|
lClaimsSetup := procedure(const JWT: TJWT)
|
|
|
|
begin
|
|
|
|
JWT.Claims.Issuer := 'Delphi MVC Framework JWT Middleware Sample';
|
|
|
|
JWT.Claims.NotBefore := Now - OneMinute * 5; // valid since 5 minutes ago
|
|
|
|
JWT.Claims.IssuedAt := Now;
|
2018-05-17 21:55:32 +02:00
|
|
|
JWT.Claims.ExpirationTime := Now + OneSecond * 30;
|
2017-07-12 00:31:49 +02:00
|
|
|
JWT.CustomClaims['mycustomvalue'] := 'hello there';
|
|
|
|
// Here we dont use a fixed ExpirationTime but a LiveValidityWindowInSeconds
|
|
|
|
// to make the ExpirationTime dynamic, incrementing the
|
|
|
|
// ExpirationTime by LiveValidityWindowInSeconds seconds at each request
|
2018-05-17 21:55:32 +02:00
|
|
|
JWT.LiveValidityWindowInSeconds := 10; // 60 * 60; // 1 hour
|
2017-07-12 00:31:49 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
MVC := TMVCEngine.Create(Self);
|
|
|
|
MVC.Config[TMVCConfigKey.SessionTimeout] := '30';
|
|
|
|
MVC.Config[TMVCConfigKey.DefaultContentType] := 'text/html';
|
|
|
|
MVC.AddController(TApp1MainController).AddController(TAdminController)
|
2018-05-17 21:55:32 +02:00
|
|
|
.AddMiddleware(TMVCJWTAuthenticationMiddleware.Create(TAuthenticationSample.Create, lClaimsSetup, 'mys3cr37',
|
|
|
|
'/login', [TJWTCheckableClaim.ExpirationTime, TJWTCheckableClaim.NotBefore, TJWTCheckableClaim.IssuedAt], 0
|
|
|
|
// just for test, Leeway seconds is zero.
|
2020-04-29 01:59:41 +02:00
|
|
|
))
|
|
|
|
.AddMiddleware(TMVCStaticFilesMiddleware.Create(
|
|
|
|
'/', { StaticFilesPath }
|
|
|
|
'..\..\www' { DocumentRoot }
|
2017-07-14 17:38:51 +02:00
|
|
|
));
|
2017-07-12 00:31:49 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|