delphimvcframework/samples/jsonwebtoken/WebModuleUnit1.pas

87 lines
1.9 KiB
ObjectPascal
Raw Normal View History

2016-05-23 17:26:00 +02:00
unit WebModuleUnit1;
interface
uses
System.SysUtils,
2016-05-23 17:26:00 +02:00
System.Classes,
Web.HTTPApp,
MVCFramework,
MVCFramework.Commons;
2016-05-23 17:26:00 +02:00
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,
2016-05-23 17:26:00 +02:00
AuthenticationU,
MVCFramework.Middleware.JWT,
MVCFramework.Middleware.StaticFiles,
MVCFramework.JWT,
System.DateUtils;
2016-05-23 17:26:00 +02:00
procedure TWebModule1.WebModuleCreate(Sender: TObject);
var
lClaimsSetup: TJWTClaimsSetup;
2016-05-23 17:26:00 +02:00
begin
lClaimsSetup := procedure(const JWT: TJWT)
begin
JWT.Claims.Issuer := 'Delphi MVC Framework JWT Middleware Sample';
if TMVCWebRequest(JWT.Data).QueryStringParamExists('rememberme') then
begin
JWT.Claims.ExpirationTime := Now + (OneHour * 10); // valid for 10 hour
end
else
begin
JWT.Claims.ExpirationTime := Now + OneHour; // valid for 1 hour
end;
JWT.Claims.NotBefore := Now - OneMinute * 5; // valid since 5 minutes ago
JWT.Claims.IssuedAt := Now;
JWT.CustomClaims['mycustomvalue'] := 'hello there';
end;
MVC := TMVCEngine.Create(Self,
procedure(Config: TMVCConfig)
begin
Config[TMVCConfigKey.SessionTimeout] := '30';
Config[TMVCConfigKey.DefaultContentType] := 'text/html';
end);
MVC
.AddController(TApp1MainController)
.AddController(TAdminController)
2020-10-24 14:21:02 +02:00
.AddMiddleware(
TMVCJWTAuthenticationMiddleware.Create(
TAuthenticationSample.Create,
lClaimsSetup,
2020-10-24 14:21:02 +02:00
'mys3cr37',
'/login',
[
TJWTCheckableClaim.ExpirationTime,
TJWTCheckableClaim.NotBefore,
TJWTCheckableClaim.IssuedAt
], 300))
.AddMiddleware(TMVCStaticFilesMiddleware.Create(
2020-10-22 09:32:10 +02:00
'/static', { StaticFilesPath }
'..\..\www' { DocumentRoot }
));
2016-05-23 17:26:00 +02:00
end;
end.