From 167fbf3877b5740a80487e6147b949b5817e1bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Duarte?= Date: Fri, 15 May 2020 10:53:13 -0300 Subject: [PATCH] Added example of using JWT authentication with Roles authorization --- .../jsonwebtoken_roleauth/AppControllerU.pas | 141 +++ .../jsonwebtoken_roleauth/AuthenticationU.pas | 69 ++ samples/jsonwebtoken_roleauth/Group.groupproj | 48 + .../JWTRoleAuthServer.dpr | 50 + .../JWTRoleAuthServer.dproj | 982 ++++++++++++++++ .../jsonwebtoken_roleauth/WebModuleUnit1.dfm | 13 + .../jsonwebtoken_roleauth/WebModuleUnit1.pas | 73 ++ .../vclclient/JWTClient.dpr | 14 + .../vclclient/JWTClient.dproj | 1029 +++++++++++++++++ .../vclclient/MainClientFormU.dfm | 136 +++ .../vclclient/MainClientFormU.pas | 212 ++++ samples/jsonwebtoken_roleauth/www/index.html | 18 + 12 files changed, 2785 insertions(+) create mode 100644 samples/jsonwebtoken_roleauth/AppControllerU.pas create mode 100644 samples/jsonwebtoken_roleauth/AuthenticationU.pas create mode 100644 samples/jsonwebtoken_roleauth/Group.groupproj create mode 100644 samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dpr create mode 100644 samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dproj create mode 100644 samples/jsonwebtoken_roleauth/WebModuleUnit1.dfm create mode 100644 samples/jsonwebtoken_roleauth/WebModuleUnit1.pas create mode 100644 samples/jsonwebtoken_roleauth/vclclient/JWTClient.dpr create mode 100644 samples/jsonwebtoken_roleauth/vclclient/JWTClient.dproj create mode 100644 samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.dfm create mode 100644 samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.pas create mode 100644 samples/jsonwebtoken_roleauth/www/index.html diff --git a/samples/jsonwebtoken_roleauth/AppControllerU.pas b/samples/jsonwebtoken_roleauth/AppControllerU.pas new file mode 100644 index 00000000..0aa9d08b --- /dev/null +++ b/samples/jsonwebtoken_roleauth/AppControllerU.pas @@ -0,0 +1,141 @@ +unit AppControllerU; + +interface + +uses + MVCFramework, + MVCFramework.Commons, + MVCFramework.Logger, + Web.HTTPApp, + MVCFramework.Middleware.Authentication.RoleBasedAuthHandler; + +type + + [MVCPath('/')] + TApp1MainController = class(TMVCController) + public + [MVCPath('/public')] + [MVCHTTPMethod([httpGET])] + procedure PublicSection(ctx: TWebContext); + [MVCPath('/')] + [MVCHTTPMethod([httpGET])] + procedure Index(ctx: TWebContext); + end; + + [MVCPath('/admin')] + [MVCRequiresAuthentication] { Defines that all control methods require authentication } + TAdminController = class(TMVCController) + protected + procedure OnBeforeAction(AContext: TWebContext; const AActionName: string; var AHandled: Boolean); override; + public + [MVCPath('/role1')] + [MVCProduces('text/html')] + [MVCHTTPMethod([httpGET])] + [MVCRequiresRole('role1')] { Define the role required to access this method } + procedure OnlyRole1(ctx: TWebContext); + + [MVCPath('/role1')] + [MVCProduces('application/json')] + [MVCHTTPMethod([httpGET])] + [MVCRequiresRole('role1')] + procedure OnlyRole1EmittingJSON; + + [MVCPath('/role2')] + [MVCProduces('text/html')] + [MVCHTTPMethod([httpGET])] + [MVCRequiresRole('role2')] + procedure OnlyRole2(ctx: TWebContext); + end; + +implementation + +uses + System.SysUtils, + System.JSON, + System.Classes, + System.Generics.Collections; + +{ TApp1MainController } + +procedure TApp1MainController.Index(ctx: TWebContext); +begin + Redirect('/index.html'); +end; + +procedure TApp1MainController.PublicSection(ctx: TWebContext); +begin + Render('This is a public section'); +end; + +{ TAdminController } + +procedure TAdminController.OnBeforeAction(AContext: TWebContext; + const AActionName: string; var AHandled: Boolean); +begin + inherited; + Assert(AContext.LoggedUser.CustomData['customkey1'] = 'customvalue1', 'customkey1 not valid'); + Assert(AContext.LoggedUser.CustomData['customkey2'] = 'customvalue2', 'customkey2 not valid'); + AHandled := False; +end; + +procedure TAdminController.OnlyRole1(ctx: TWebContext); +var + lPair: TPair; +begin + ContentType := TMVCMediaType.TEXT_PLAIN; + ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName + + ', now you are a logged user and this is a protected content!'); + ResponseStream.AppendLine('As logged user you have the following roles: ' + + sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray)); + ResponseStream.AppendLine('You CustomClaims are: ' + + sLineBreak); + for lPair in Context.LoggedUser.CustomData do + begin + ResponseStream.AppendFormat('%s = %s' + sLineBreak, [lPair.Key, lPair.Value]); + end; + RenderResponseStream; +end; + +procedure TAdminController.OnlyRole1EmittingJSON; +var + lJObj: TJSONObject; + lJArr: TJSONArray; + lQueryParams: TStrings; + I: Integer; + lPair: TPair; +begin + ContentType := TMVCMediaType.APPLICATION_JSON; + lJObj := TJSONObject.Create; + lJObj.AddPair('message', 'This is protected content accessible only by user1'); + lJArr := TJSONArray.Create; + lJObj.AddPair('querystringparameters', lJArr); + + lQueryParams := Context.Request.QueryStringParams; + for I := 0 to lQueryParams.Count - 1 do + begin + lJArr.AddElement(TJSONObject.Create(TJSONPair.Create( + lQueryParams.Names[I], + lQueryParams.ValueFromIndex[I]))); + end; + + lJArr := TJSONArray.Create; + lJObj.AddPair('customclaims', lJArr); + for lPair in Context.LoggedUser.CustomData do + begin + lJArr.AddElement(TJSONObject.Create(TJSONPair.Create(lPair.Key, lPair.Value))); + end; + + Render(lJObj); +end; + +procedure TAdminController.OnlyRole2(ctx: TWebContext); +begin + ContentType := TMVCMediaType.TEXT_PLAIN; + ResponseStream.AppendLine('Hey! Hello ' + ctx.LoggedUser.UserName + + ', now you are a logged user and this is a protected content!'); + ResponseStream.AppendLine('As logged user you have the following roles: ' + + sLineBreak + string.Join(sLineBreak, Context.LoggedUser.Roles.ToArray)); + RenderResponseStream; +end; + +end. diff --git a/samples/jsonwebtoken_roleauth/AuthenticationU.pas b/samples/jsonwebtoken_roleauth/AuthenticationU.pas new file mode 100644 index 00000000..ed14ed10 --- /dev/null +++ b/samples/jsonwebtoken_roleauth/AuthenticationU.pas @@ -0,0 +1,69 @@ +unit AuthenticationU; + +interface + +uses + System.SysUtils, + MVCFramework.Commons, + System.Generics.Collections, + MVCFramework, + MVCFramework.Middleware.Authentication.RoleBasedAuthHandler; + +type + TAuthenticationSample = class(TRoleBasedAuthHandler) + public + procedure OnAuthentication( + const AContext: TWebContext; + const UserName: string; + const Password: string; + UserRoles: TList; + var IsValid: Boolean; + const SessionData: TDictionary + ); override; + end; + +implementation + + +{ TAuthenticationSample } + +procedure TAuthenticationSample.OnAuthentication(const AContext: TWebContext; const UserName, Password: string; + UserRoles: TList; var IsValid: Boolean; const SessionData: TDictionary); +begin + IsValid := (not UserName.IsEmpty) and UserName.Equals(Password); // hey!, this is just a demo!!! + if IsValid then + begin + if UserName = 'user_raise_exception' then + begin + raise EMVCException.Create(500, 1024, 'This is a custom exception raised in "TAuthenticationSample.OnAuthentication"'); + end; + + // Add here all the roles that the user has. These roles will be added to the JWT token + + if UserName = 'user1' then + begin + UserRoles.Add('role1'); + end; + if UserName = 'user2' then + begin + UserRoles.Add('role2'); + end; + if UserName = 'user3' then // all the roles + begin + UserRoles.Add('role1'); + UserRoles.Add('role2'); + end; + + // You can add custom data to the logged user + SessionData.AddOrSetValue('customkey1', 'customvalue1'); + SessionData.AddOrSetValue('customkey2', 'customvalue2'); + + end + else + begin + UserRoles.Clear; + end; + +end; + +end. diff --git a/samples/jsonwebtoken_roleauth/Group.groupproj b/samples/jsonwebtoken_roleauth/Group.groupproj new file mode 100644 index 00000000..391e90d1 --- /dev/null +++ b/samples/jsonwebtoken_roleauth/Group.groupproj @@ -0,0 +1,48 @@ + + + {094E1EEA-8729-4125-A068-57B1296432CB} + + + + + + + + + + + Default.Personality.12 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dpr b/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dpr new file mode 100644 index 00000000..8ae4d4db --- /dev/null +++ b/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dpr @@ -0,0 +1,50 @@ +program JWTRoleAuthServer; + +{$APPTYPE CONSOLE} + + +uses + System.SysUtils, + Winapi.Windows, + Winapi.ShellAPI, + Web.WebReq, + Web.WebBroker, + IdHTTPWebBrokerBridge, + MVCFramework.Commons, + IdContext, + WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule}, + AppControllerU in 'AppControllerU.pas', + AuthenticationU in 'AuthenticationU.pas'; + +{$R *.res} + +procedure RunServer(APort: Integer); +var + LServer: TIdHTTPWebBrokerBridge; +begin + Writeln(Format('Starting HTTP Server or port %d', [APort])); + LServer := TIdHTTPWebBrokerBridge.Create(nil); + try + LServer.OnParseAuthentication := TMVCParseAuthentication.OnParseAuthentication; + LServer.DefaultPort := APort; + LServer.Active := True; + Writeln('Press RETURN to stop the server'); + ReadLn; + finally + LServer.Free; + end; +end; + +begin + ReportMemoryLeaksOnShutdown := True; + try + if WebRequestHandler <> nil then + WebRequestHandler.WebModuleClass := WebModuleClass; + WebRequestHandlerProc.MaxConnections := 1024; + RunServer(8080); + except + on E: Exception do + Writeln(E.ClassName, ': ', E.Message); + end + +end. diff --git a/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dproj b/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dproj new file mode 100644 index 00000000..954d2fdc --- /dev/null +++ b/samples/jsonwebtoken_roleauth/JWTRoleAuthServer.dproj @@ -0,0 +1,982 @@ + + + {7B54055A-5749-4136-9FE2-35FDBEEA874C} + 18.8 + VCL + JWTRoleAuthServer.dpr + True + Debug + Win32 + 1 + Console + + + true + + + true + Base + true + + + true + Base + true + + + true + Base + true + + + true + Cfg_1 + true + true + + + true + Base + true + + + true + Cfg_2 + true + true + + + false + $(BDS)\bin\delphi_PROJECTICNS.icns + JWTRoleAuthServer + ..\..\sources;..\..\lib\delphistompclient;..\..\lib\loggerpro;..\..\lib\dmustache;$(DCC_UnitSearchPath) + CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments= + None + 1040 + $(BDS)\bin\delphi_PROJECTICON.ico + System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace) + .\$(Platform)\$(Config) + .\$(Platform)\$(Config) + false + false + false + false + false + + + Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) + 1033 + cxPivotGridChartRS17;JvMM;dxSkinSevenRS17;dxSkinBlueprintRS17;dxSkinHighContrastRS17;dxSkinOffice2007BlackRS17;dxCoreRS17;cxPageControldxBarPopupMenuRS17;dxSkinXmas2008BlueRS17;dxPSDBTeeChartRS17;JvCrypt;dxPSTeeChartRS17;dxSkinSummer2008RS17;dxPScxSchedulerLnkRS17;dxSkinBlueRS17;dxSkinDarkRoomRS17;DBXInterBaseDriver;DataSnapServer;DataSnapCommon;dxPScxTLLnkRS17;JvNet;JvDotNetCtrls;dxRibbonRS17;DbxCommonDriver;cxDataRS17;vclimg;dxSkinsdxBarPainterRS17;dxPSdxDBTVLnkRS17;dbxcds;DatasnapConnectorsFreePascal;NxDBGridDsgn_dxe3;JvXPCtrls;dxSkinMoneyTwinsRS17;vcldb;cxExportRS17;dxPSCoreRS17;dxBarExtItemsRS17;dxGDIPlusRS17;FMXfrx17;dxNavBarRS17;CustomIPTransport;cxLibraryRS17;cxGridRS17;dxSkinOffice2010BlackRS17;dsnap;IndyIPServer;IndyCore;dxSkinMcSkinRS17;CloudService;dxPScxCommonRS17;FmxTeeUI;frxDB17;AnyDAC_PhysDb2_D17;dxSkinsdxDLPainterRS17;dxSkiniMaginaryRS17;JvDB;JvRuntimeDesign;dxPScxVGridLnkRS17;JclDeveloperTools;dxSkinSevenClassicRS17;dxPScxExtCommonRS17;MyFrameTestPackage;dxPScxSSLnkRS17;NxGridRun_dxe3;dxSkinLilianRS17;fs17;dxPSdxLCLnkRS17;dxSkinOffice2010BlueRS17;NxCommonRun_dxe3;bindcompfmx;DataBindingsVCL170;dxSkinOffice2010SilverRS17;vcldbx;cxSchedulerGridRS17;dbrtl;bindcomp;inetdb;JvPluginSystem;dxBarRS17;DataBindings;DBXOdbcDriver;IcsCommonDXE3Run;JvCmp;dxBarDBNavRS17;dxSkinWhiteprintRS17;JvTimeFramework;xmlrtl;dxSkinsdxRibbonPainterRS17;ibxpress;dxDockingRS17;vclactnband;bindengine;soaprtl;FMXTee;dxADOServerModeRS17;bindcompvcl;dxBarExtDBItemsRS17;dxPSPrVwRibbonRS17;Jcl;vclie;dxSkinOffice2007PinkRS17;cxPageControlRS17;dxSkinscxPCPainterRS17;AnyDAC_PhysADS_D17;AnyDAC_PhysIB_D17;dxmdsRS17;dxSkinTheAsphaltWorldRS17;DBXInformixDriver;Intraweb;dxPsPrVwAdvRS17;NxInspectorRun_dxe3;dxSkinSilverRS17;dxdborRS17;dsnapcon;DBXFirebirdDriver;fsDB17;inet;dorm_runtime_xe3;JvPascalInterpreter;vclx;dxSkinStardustRS17;cxEditorsRS17;DBXSybaseASADriver;NxInspectorDsgn_dxe3;dbexpress;IndyIPClient;AnyDAC_PhysMySQL_D17;cxTreeListdxBarPopupMenuRS17;dxSkinVS2010RS17;NxGridDsgn_dxe3;dxThemeRS17;DBXSqliteDriver;dxPScxGridLnkRS17;fmx;JvDlgs;IndySystem;TeeDB;dxSkinValentineRS17;vclib;inetdbbde;DataSnapClient;dxSkinDevExpressStyleRS17;DataSnapProviderClient;DBXSybaseASEDriver;cxBarEditItemRS17;AnyDAC_PhysMSAcc_D17;dxServerModeRS17;cxPivotGridOLAPRS17;cxSchedulerRS17;MetropolisUILiveTile;AnyDAC_PhysSQLITE_D17;dxPSLnksRS17;dxSkinPumpkinRS17;dxPSdxDBOCLnkRS17;cxVerticalGridRS17;dxSkinSpringTimeRS17;vcldsnap;dxSkinDevExpressDarkStyleRS17;DBXDb2Driver;AnyDAC_ComI_D17;DBXOracleDriver;AnyDAC_PhysMSSQL_D17;JvCore;NxDBGridRun_dxe3;vclribbon;AnyDAC_Comp_D17;cxSpreadSheetRS17;dxSkinLiquidSkyRS17;AnyDAC_PhysODBC_D17;fmxase;vcl;dxSkinOffice2007SilverRS17;AnyDAC_PhysPg_D17;IndyIPCommon;DBXMSSQLDriver;CodeSiteExpressPkg;dxPSdxOCLnkRS17;dcldxSkinsCoreRS17;JvAppFrm;AnyDAC_PhysASA_D17;inetdbxpress;webdsnap;NxCollectionRun_dxe3;AnyDAC_PhysOracle_D17;dxSkinCoffeeRS17;JvDocking;adortl;dxSkinscxSchedulerPainterRS17;JvWizards;NxCollectionDsgn_dxe3;frx17;NxCommonDsgn_dxe3;dxtrmdRS17;dxPScxPCProdRS17;AnyDAC_GUIxForms_D17;JvBands;rtl;DbxClientDriver;AnyDAC_PhysTDBX_D17;dxTabbedMDIRS17;dxComnRS17;dxSkinSharpPlusRS17;dxSkinsCoreRS17;dxSkinLondonLiquidSkyRS17;dxdbtrRS17;Tee;JclContainers;NxAddonsRun_dxe3;CPortLibDXE;JvSystem;dxorgcRS17;svnui;dxSkinBlackRS17;JvControls;NxSheetRun_dxe3;IndyProtocols;DBXMySQLDriver;dxLayoutControlRS17;bindcompdbx;TeeUI;JvJans;JvPrintPreview;JvPageComps;JvStdCtrls;JvCustom;dxSkinOffice2007BlueRS17;dxPScxPivotGridLnkRS17;dxSpellCheckerRS17;vcltouch;dxSkinOffice2007GreenRS17;dxSkinSharpRS17;websnap;dxSkinFoggyRS17;dxTileControlRS17;VclSmp;FMXfrxDB17;dxSkinDarkSideRS17;cxPivotGridRS17;DataSnapConnectors;AnyDAC_Phys_D17;fmxobj;SynEdit_RXE3;JclVcl;cxTreeListRS17;dxPSdxFCLnkRS17;dxSkinGlassOceansRS17;frxe17;svn;dxFlowChartRS17;fmxdae;dxSkinsdxNavBarPainterRS17;bdertl;VirtualTreesR;DataSnapIndy10ServerTransport;dxDBXServerModeRS17;dxSkinCaramelRS17;$(DCC_UsePackage) + + + cxPivotGridChartRS17;JvMM;dxSkinSevenRS17;dxSkinBlueprintRS17;dxSkinHighContrastRS17;dxSkinOffice2007BlackRS17;dxCoreRS17;cxPageControldxBarPopupMenuRS17;dxSkinXmas2008BlueRS17;dxPSDBTeeChartRS17;JvCrypt;dxPSTeeChartRS17;dxSkinSummer2008RS17;dxPScxSchedulerLnkRS17;dxSkinBlueRS17;dxSkinDarkRoomRS17;DBXInterBaseDriver;DataSnapServer;DataSnapCommon;dxPScxTLLnkRS17;JvNet;dxRibbonRS17;DbxCommonDriver;cxDataRS17;vclimg;dxSkinsdxBarPainterRS17;dxPSdxDBTVLnkRS17;dbxcds;DatasnapConnectorsFreePascal;NxDBGridDsgn_dxe3;dxSkinMoneyTwinsRS17;vcldb;cxExportRS17;dxPSCoreRS17;dxBarExtItemsRS17;dxGDIPlusRS17;dxNavBarRS17;CustomIPTransport;cxLibraryRS17;cxGridRS17;dxSkinOffice2010BlackRS17;dsnap;IndyIPServer;IndyCore;dxSkinMcSkinRS17;dxPScxCommonRS17;AnyDAC_PhysDb2_D17;dxSkinsdxDLPainterRS17;dxSkiniMaginaryRS17;JvDB;dxPScxVGridLnkRS17;dxSkinSevenClassicRS17;dxPScxExtCommonRS17;dxPScxSSLnkRS17;NxGridRun_dxe3;dxSkinLilianRS17;dxPSdxLCLnkRS17;dxSkinOffice2010BlueRS17;NxCommonRun_dxe3;bindcompfmx;dxSkinOffice2010SilverRS17;cxSchedulerGridRS17;dbrtl;bindcomp;inetdb;JvPluginSystem;dxBarRS17;DBXOdbcDriver;JvCmp;dxBarDBNavRS17;dxSkinWhiteprintRS17;JvTimeFramework;xmlrtl;dxSkinsdxRibbonPainterRS17;ibxpress;dxDockingRS17;vclactnband;bindengine;soaprtl;dxADOServerModeRS17;bindcompvcl;dxBarExtDBItemsRS17;dxPSPrVwRibbonRS17;vclie;dxSkinOffice2007PinkRS17;cxPageControlRS17;dxSkinscxPCPainterRS17;AnyDAC_PhysADS_D17;AnyDAC_PhysIB_D17;dxmdsRS17;dxSkinTheAsphaltWorldRS17;DBXInformixDriver;dxPsPrVwAdvRS17;NxInspectorRun_dxe3;dxSkinSilverRS17;dxdborRS17;dsnapcon;DBXFirebirdDriver;inet;JvPascalInterpreter;vclx;dxSkinStardustRS17;cxEditorsRS17;DBXSybaseASADriver;NxInspectorDsgn_dxe3;dbexpress;IndyIPClient;AnyDAC_PhysMySQL_D17;cxTreeListdxBarPopupMenuRS17;dxSkinVS2010RS17;NxGridDsgn_dxe3;dxThemeRS17;DBXSqliteDriver;dxPScxGridLnkRS17;fmx;JvDlgs;IndySystem;TeeDB;dxSkinValentineRS17;vclib;DataSnapClient;dxSkinDevExpressStyleRS17;DataSnapProviderClient;DBXSybaseASEDriver;cxBarEditItemRS17;AnyDAC_PhysMSAcc_D17;dxServerModeRS17;cxPivotGridOLAPRS17;cxSchedulerRS17;AnyDAC_PhysSQLITE_D17;dxPSLnksRS17;dxSkinPumpkinRS17;dxPSdxDBOCLnkRS17;cxVerticalGridRS17;dxSkinSpringTimeRS17;vcldsnap;dxSkinDevExpressDarkStyleRS17;DBXDb2Driver;AnyDAC_ComI_D17;DBXOracleDriver;AnyDAC_PhysMSSQL_D17;JvCore;NxDBGridRun_dxe3;AnyDAC_Comp_D17;cxSpreadSheetRS17;dxSkinLiquidSkyRS17;AnyDAC_PhysODBC_D17;fmxase;vcl;dxSkinOffice2007SilverRS17;AnyDAC_PhysPg_D17;IndyIPCommon;DBXMSSQLDriver;dxPSdxOCLnkRS17;dcldxSkinsCoreRS17;JvAppFrm;AnyDAC_PhysASA_D17;inetdbxpress;webdsnap;NxCollectionRun_dxe3;AnyDAC_PhysOracle_D17;dxSkinCoffeeRS17;adortl;dxSkinscxSchedulerPainterRS17;JvWizards;NxCollectionDsgn_dxe3;NxCommonDsgn_dxe3;dxtrmdRS17;dxPScxPCProdRS17;AnyDAC_GUIxForms_D17;JvBands;rtl;DbxClientDriver;AnyDAC_PhysTDBX_D17;dxTabbedMDIRS17;dxComnRS17;dxSkinSharpPlusRS17;dxSkinsCoreRS17;dxSkinLondonLiquidSkyRS17;dxdbtrRS17;Tee;NxAddonsRun_dxe3;JvSystem;dxorgcRS17;dxSkinBlackRS17;JvControls;NxSheetRun_dxe3;IndyProtocols;DBXMySQLDriver;dxLayoutControlRS17;bindcompdbx;TeeUI;JvJans;JvPrintPreview;JvPageComps;JvStdCtrls;JvCustom;dxSkinOffice2007BlueRS17;dxPScxPivotGridLnkRS17;dxSpellCheckerRS17;vcltouch;dxSkinOffice2007GreenRS17;dxSkinSharpRS17;websnap;dxSkinFoggyRS17;dxTileControlRS17;VclSmp;dxSkinDarkSideRS17;cxPivotGridRS17;DataSnapConnectors;AnyDAC_Phys_D17;fmxobj;SynEdit_RXE3;cxTreeListRS17;dxPSdxFCLnkRS17;dxSkinGlassOceansRS17;dxFlowChartRS17;fmxdae;dxSkinsdxNavBarPainterRS17;DataSnapIndy10ServerTransport;dxDBXServerModeRS17;dxSkinCaramelRS17;$(DCC_UsePackage) + + + DEBUG;$(DCC_Define) + true + false + true + true + true + + + 3 + true + 1033 + false + + + false + RELEASE;$(DCC_Define) + 0 + 0 + + + 1033 + + + + MainSource + + +
WebModule1
+ TWebModule +
+ + + + Cfg_2 + Base + + + Base + + + Cfg_1 + Base + +
+ + Delphi.Personality.12 + + + + + False + False + 1 + 0 + 0 + 0 + False + False + False + False + False + 1040 + 1252 + + + + + 1.0.0.0 + + + + + + 1.0.0.0 + + + + + + + + + + + + JWTRoleAuthServer.dpr + + + Embarcadero C++Builder Office 2000 Servers Package + Embarcadero C++Builder Office XP Servers Package + Microsoft Office 2000 Sample Automation Server Wrapper Components + Microsoft Office XP Sample Automation Server Wrapper Components + + + + + + JWTRoleAuthServer.exe + true + + + + + 1 + + + 0 + + + + + classes + 1 + + + classes + 1 + + + + + res\xml + 1 + + + res\xml + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\armeabi + 1 + + + library\lib\armeabi + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\mips + 1 + + + library\lib\mips + 1 + + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\values-v21 + 1 + + + res\values-v21 + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-ldpi + 1 + + + res\drawable-ldpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-xxxhdpi + 1 + + + res\drawable-xxxhdpi + 1 + + + + + res\drawable-small + 1 + + + res\drawable-small + 1 + + + + + res\drawable-normal + 1 + + + res\drawable-normal + 1 + + + + + res\drawable-large + 1 + + + res\drawable-large + 1 + + + + + res\drawable-xlarge + 1 + + + res\drawable-xlarge + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + 1 + + + 1 + + + 0 + + + + + 1 + .framework + + + 1 + .framework + + + 0 + + + + + 1 + .dylib + + + 1 + .dylib + + + 0 + .dll;.bpl + + + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + 0 + .bpl + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + + + + + + 1 + + + 1 + + + 1 + + + + + + + + Contents\Resources + 1 + + + Contents\Resources + 1 + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + 0 + + + + + library\lib\armeabi-v7a + 1 + + + + + 1 + + + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + + + + + + + + + + + True + False + + + 12 + + + + +
diff --git a/samples/jsonwebtoken_roleauth/WebModuleUnit1.dfm b/samples/jsonwebtoken_roleauth/WebModuleUnit1.dfm new file mode 100644 index 00000000..64ffdbbd --- /dev/null +++ b/samples/jsonwebtoken_roleauth/WebModuleUnit1.dfm @@ -0,0 +1,13 @@ +object WebModule1: TWebModule1 + OldCreateOrder = False + OnCreate = WebModuleCreate + OnDestroy = WebModuleDestroy + Actions = < + item + Default = True + Name = 'DefaultHandler' + PathInfo = '/' + end> + Height = 230 + Width = 415 +end diff --git a/samples/jsonwebtoken_roleauth/WebModuleUnit1.pas b/samples/jsonwebtoken_roleauth/WebModuleUnit1.pas new file mode 100644 index 00000000..5af0b8bf --- /dev/null +++ b/samples/jsonwebtoken_roleauth/WebModuleUnit1.pas @@ -0,0 +1,73 @@ +unit WebModuleUnit1; + +interface + +uses + System.SysUtils, + System.Classes, + Web.HTTPApp, + MVCFramework, + MVCFramework.Commons; + +type + TWebModule1 = class(TWebModule) + procedure WebModuleCreate(Sender: TObject); + procedure WebModuleDestroy(Sender: TObject); + + private + FEngine: TMVCEngine; + + public + { Public declarations } + end; + +var + WebModuleClass: TComponentClass = TWebModule1; + +implementation + +{$R *.dfm} + + +uses + AppControllerU, + System.Generics.Collections, + AuthenticationU, + MVCFramework.Middleware.JWT, + MVCFramework.Middleware.StaticFiles, + MVCFramework.JWT, + 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.ExpirationTime := Now + OneHour; // valid for 1 hour + JWT.Claims.NotBefore := Now - OneMinute * 5; // valid since 5 minutes ago + JWT.Claims.IssuedAt := Now; + JWT.CustomClaims['mycustomvalue'] := 'hello there'; + end; + + FEngine := TMVCEngine.Create(Self); + + FEngine + .AddController(TApp1MainController) + .AddController(TAdminController) + .AddMiddleware(TMVCJWTAuthenticationMiddleware.Create(TAuthenticationSample.Create, 'mys3cr37', '/login', + lClaimsSetup, + [TJWTCheckableClaim.ExpirationTime, TJWTCheckableClaim.NotBefore, TJWTCheckableClaim.IssuedAt], 300)) + .AddMiddleware(TMVCStaticFilesMiddleware.Create( + '/', { StaticFilesPath } + '..\..\www' { DocumentRoot } + )); +end; + +procedure TWebModule1.WebModuleDestroy(Sender: TObject); +begin + FEngine.Free; +end; + +end. diff --git a/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dpr b/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dpr new file mode 100644 index 00000000..3f5b4252 --- /dev/null +++ b/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dpr @@ -0,0 +1,14 @@ +program JWTClient; + +uses + Vcl.Forms, + MainClientFormU in 'MainClientFormU.pas' {Form5}; + +{$R *.res} + +begin + Application.Initialize; + Application.MainFormOnTaskbar := True; + Application.CreateForm(TForm5, Form5); + Application.Run; +end. diff --git a/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dproj b/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dproj new file mode 100644 index 00000000..3518ee45 --- /dev/null +++ b/samples/jsonwebtoken_roleauth/vclclient/JWTClient.dproj @@ -0,0 +1,1029 @@ + + + {E7317702-64D3-4A65-8734-030F3AE3DBBC} + 18.8 + VCL + JWTClient.dpr + True + Debug + Win32 + 1 + Application + + + true + + + true + Base + true + + + true + Base + true + + + true + Base + true + + + true + Cfg_1 + true + true + + + true + Base + true + + + true + Cfg_2 + true + true + + + CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName);FileDescription=$(MSBuildProjectName);ProductName=$(MSBuildProjectName) + 1040 + ..\..\..\sources;..\..\..\lib\delphistompclient;..\..\..\lib\loggerpro;..\..\..\lib\dmustache;$(DCC_UnitSearchPath) + System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace) + $(BDS)\bin\delphi_PROJECTICON.ico + JWTClient + .\$(Platform)\$(Config) + .\$(Platform)\$(Config) + false + false + false + false + false + + + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png + true + $(BDS)\bin\default_app.manifest + Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) + 1033 + DBXSqliteDriver;DBXDb2Driver;dxCoreRS23;vclactnband;frxe23;vclFireDAC;dxPSLnksRS23;dxPSdxLCLnkRS23;tethering;cxDataRS23;dxPSdxOCLnkRS23;dxTabbedMDIRS23;FireDACADSDriver;dxSkinBlackRS23;dxSkinLondonLiquidSkyRS23;JvPluginSystem;dxDBXServerModeRS23;dxHttpIndyRequestRS23;dxPScxGridLnkRS23;cxSchedulerRS23;FireDACMSSQLDriver;dclRBDBE1723;vcltouch;JvBands;vcldb;rbDB1723;svn;dxWizardControlRS23;dxSkinMcSkinRS23;dxPScxCommonRS23;JvJans;Intraweb;dxSkinOffice2007BlueRS23;rbIBE1723;dxBarRS23;cxSchedulerRibbonStyleEventEditorRS23;dxSkinOffice2013WhiteRS23;JvDotNetCtrls;dxPSTeeChartRS23;cxLibraryRS23;dxSkinVisualStudio2013LightRS23;vclib;cxPivotGridChartRS23;rbDBE1723;dxSkinSummer2008RS23;dxPSdxDBOCLnkRS23;dxGDIPlusRS23;dxSkinDarkSideRS23;FireDACDBXDriver;dxSkinFoggyRS23;dxSkinSevenRS23;vclx;rbCIDE1723;dxSkinOffice2010SilverRS23;dxdborRS23;RESTBackendComponents;dxLayoutControlRS23;dxPSPrVwRibbonRS23;VCLRESTComponents;dxSkinDevExpressStyleRS23;dxSkinWhiteprintRS23;vclie;bindengine;CloudService;rbRAP1723;JvHMI;FireDACMySQLDriver;dxSkinOffice2013DarkGrayRS23;DataSnapClient;dxPScxPCProdRS23;bindcompdbx;DBXSybaseASEDriver;IndyIPServer;dxSkinPumpkinRS23;IndySystem;dsnapcon;cxTreeListdxBarPopupMenuRS23;dclRBIBE1723;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;dxSkinLilianRS23;Jcl;rbADO1723;dxBarDBNavRS23;dxFlowChartRS23;dxSkinOffice2016ColorfulRS23;rbUSER1723;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;dxSpreadSheetRS23;AsyncProDR;JvManagedThreads;dxSkinOffice2007PinkRS23;dxPSdxSpreadSheetLnkRS23;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;DTKANPRPackage;dxSkinHighContrastRS23;rtl;dxSkinSevenClassicRS23;DbxClientDriver;dxSkinDevExpressDarkStyleRS23;DBXSybaseASADriver;dxNavBarRS23;dxSkinMetropolisDarkRS23;CodeSiteExpressPkg;dxSkinTheAsphaltWorldRS23;JvSystem;SampleListViewMultiDetailAppearancePackage;dxRichEditControlRS23;JvStdCtrls;JvTimeFramework;ipstudiowin;appanalytics;cxPivotGridRS23;rbUSERDesign1723;dxSkinsdxDLPainterRS23;IndyIPClient;dxRibbonRS23;dxPScxVGridLnkRS23;bindcompvcl;frxDB23;vcldbx;dxSkinOffice2007SilverRS23;dxPScxTLLnkRS23;dxMapControlRS23;TeeUI;rbDIDE1723;JvPascalInterpreter;JvDocking;VclSmp;dxPScxSchedulerLnkRS23;cxTreeListRS23;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dxRibbonCustomizationFormRS23;dxPSRichEditControlLnkRS23;dxBarExtDBItemsRS23;DataSnapProviderClient;FireDACMongoDBDriver;dxSkiniMaginaryRS23;frx23;dxSpellCheckerRS23;JvControls;dxSkinsdxBarPainterRS23;JvPrintPreview;dxSkinCoffeeRS23;DataSnapServerMidas;RESTComponents;DBXInterBaseDriver;rbRTL1723;dxADOServerModeRS23;emsclientfiredac;DataSnapFireDAC;svnui;dxmdsRS23;dxSkinLiquidSkyRS23;dxdbtrRS23;dxSkinSpringTimeRS23;dxPSDBTeeChartRS23;JvGlobus;dxSkinscxPCPainterRS23;dxPSCoreRS23;DBXMSSQLDriver;JvMM;dxSkinXmas2008BlueRS23;rbDAD1723;DatasnapConnectorsFreePascal;bindcompfmx;JvNet;DBXOracleDriver;dxSkinSilverRS23;dxSkinValentineRS23;inetdb;JvAppFrm;ipstudiowinwordxp;rbTC1723;FmxTeeUI;dxBarExtItemsRS23;FireDACIBDriver;fmx;fmxdae;DelphiCookbookListViewAppearance;dxServerModeRS23;dxPsPrVwAdvRS23;dxSkinOffice2010BlackRS23;JvWizards;cxPageControlRS23;dxSkinStardustRS23;cxSchedulerGridRS23;dbexpress;IndyCore;dxSkinSharpPlusRS23;UIBD21Win32R;JvPageComps;dsnap;DataSnapCommon;emsclient;FireDACCommon;dxSkinOffice2010BlueRS23;bdertl;JvDB;dxSkinVS2010RS23;dxSkinMetropolisRS23;DataSnapConnectors;cxVerticalGridRS23;soapserver;dxSkinCaramelRS23;frxTee23;dxTileControlRS23;JclDeveloperTools;cxGridRS23;CPortLibDXE;FireDACOracleDriver;DBXMySQLDriver;JvCmp;rbFireDAC1723;DBXFirebirdDriver;FireDACCommonDriver;rbTCUI1723;LockBoxDR;inet;IndyIPCommon;JvCustom;dxSkinDarkRoomRS23;dxDockingRS23;vcl;dxSkinOffice2007GreenRS23;dxPScxExtCommonRS23;JvXPCtrls;dxSkinsCoreRS23;FireDACDb2Driver;dxThemeRS23;dxSkinsdxRibbonPainterRS23;dxSkinVisualStudio2013BlueRS23;rbRest1723;TSG5201;dxSkinMoneyTwinsRS23;dxPSdxFCLnkRS23;dxtrmdRS23;TeeDB;FireDAC;cxSchedulerTreeBrowserRS23;JvCore;dxFireDACServerModeRS23;dxSkinBlueRS23;OverbyteIcsD10SRun;JvCrypt;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;cxEditorsRS23;dxSkinGlassOceansRS23;JvDlgs;JvRuntimeDesign;dxSkinsdxNavBarPainterRS23;dxGaugeControlRS23;ibxpress;Tee;dxSkinSharpRS23;DataSnapServer;ibxbindings;cxPivotGridOLAPRS23;rbIDE1723;vclwinx;FireDACDSDriver;dxSkinBlueprintRS23;dxSkinOffice2007BlackRS23;CustomIPTransport;vcldsnap;rbBDE1723;dxSkinOffice2013LightGrayRS23;bindcomp;DBXInformixDriver;officeXPrt;dxPSdxGaugeControlLnkRS23;dxPScxPivotGridLnkRS23;dxorgcRS23;dxPSdxDBTVLnkRS23;dclRBADO1723;vclribbon;dbxcds;KernowSoftwareFMX;adortl;dclRBFireDAC1723;dclRBE1723;dxComnRS23;dsnapxml;dbrtl;inetdbxpress;IndyProtocols;cxExportRS23;dxSkinOffice2016DarkRS23;JclContainers;dxSkinVisualStudio2013DarkRS23;rbRCL1723;dxSkinscxSchedulerPainterRS23;rbRIDE1723;fmxase;$(DCC_UsePackage) + CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(ModuleName);FileDescription=$(ModuleName);ProductName=$(ModuleName) + + + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png + $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png + DBXSqliteDriver;DBXDb2Driver;dxCoreRS23;vclactnband;vclFireDAC;dxPSLnksRS23;dxPSdxLCLnkRS23;tethering;cxDataRS23;dxPSdxOCLnkRS23;dxTabbedMDIRS23;FireDACADSDriver;dxSkinBlackRS23;dxSkinLondonLiquidSkyRS23;dxDBXServerModeRS23;dxHttpIndyRequestRS23;dxPScxGridLnkRS23;cxSchedulerRS23;FireDACMSSQLDriver;vcltouch;vcldb;dxWizardControlRS23;dxSkinMcSkinRS23;dxPScxCommonRS23;Intraweb;dxSkinOffice2007BlueRS23;dxBarRS23;cxSchedulerRibbonStyleEventEditorRS23;dxSkinOffice2013WhiteRS23;dxPSTeeChartRS23;cxLibraryRS23;dxSkinVisualStudio2013LightRS23;vclib;cxPivotGridChartRS23;dxSkinSummer2008RS23;dxPSdxDBOCLnkRS23;dxGDIPlusRS23;dxSkinDarkSideRS23;FireDACDBXDriver;dxSkinFoggyRS23;dxSkinSevenRS23;vclx;dxSkinOffice2010SilverRS23;dxdborRS23;RESTBackendComponents;dxLayoutControlRS23;dxPSPrVwRibbonRS23;VCLRESTComponents;dxSkinDevExpressStyleRS23;dxSkinWhiteprintRS23;vclie;bindengine;CloudService;FireDACMySQLDriver;dxSkinOffice2013DarkGrayRS23;DataSnapClient;dxPScxPCProdRS23;bindcompdbx;DBXSybaseASEDriver;IndyIPServer;dxSkinPumpkinRS23;IndySystem;dsnapcon;cxTreeListdxBarPopupMenuRS23;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;dxSkinLilianRS23;dxBarDBNavRS23;dxFlowChartRS23;dxSkinOffice2016ColorfulRS23;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;dxSpreadSheetRS23;AsyncProDR;dxSkinOffice2007PinkRS23;dxPSdxSpreadSheetLnkRS23;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;dxSkinHighContrastRS23;rtl;dxSkinSevenClassicRS23;DbxClientDriver;dxSkinDevExpressDarkStyleRS23;DBXSybaseASADriver;dxNavBarRS23;dxSkinMetropolisDarkRS23;dxSkinTheAsphaltWorldRS23;dxRichEditControlRS23;ipstudiowin;appanalytics;cxPivotGridRS23;dxSkinsdxDLPainterRS23;IndyIPClient;dxRibbonRS23;dxPScxVGridLnkRS23;bindcompvcl;dxSkinOffice2007SilverRS23;dxPScxTLLnkRS23;dxMapControlRS23;TeeUI;VclSmp;dxPScxSchedulerLnkRS23;cxTreeListRS23;FireDACODBCDriver;DataSnapIndy10ServerTransport;dxRibbonCustomizationFormRS23;dxPSRichEditControlLnkRS23;dxBarExtDBItemsRS23;DataSnapProviderClient;FireDACMongoDBDriver;dxSkiniMaginaryRS23;dxSpellCheckerRS23;dxSkinsdxBarPainterRS23;dxSkinCoffeeRS23;DataSnapServerMidas;RESTComponents;DBXInterBaseDriver;dxADOServerModeRS23;emsclientfiredac;DataSnapFireDAC;dxmdsRS23;dxSkinLiquidSkyRS23;dxdbtrRS23;dxSkinSpringTimeRS23;dxPSDBTeeChartRS23;dxSkinscxPCPainterRS23;dxPSCoreRS23;DBXMSSQLDriver;dxSkinXmas2008BlueRS23;DatasnapConnectorsFreePascal;bindcompfmx;DBXOracleDriver;dxSkinSilverRS23;dxSkinValentineRS23;inetdb;FmxTeeUI;dxBarExtItemsRS23;FireDACIBDriver;fmx;fmxdae;dxServerModeRS23;dxPsPrVwAdvRS23;dxSkinOffice2010BlackRS23;cxPageControlRS23;dxSkinStardustRS23;cxSchedulerGridRS23;dbexpress;IndyCore;dxSkinSharpPlusRS23;dsnap;DataSnapCommon;emsclient;FireDACCommon;dxSkinOffice2010BlueRS23;dxSkinVS2010RS23;dxSkinMetropolisRS23;DataSnapConnectors;cxVerticalGridRS23;soapserver;dxSkinCaramelRS23;dxTileControlRS23;cxGridRS23;FireDACOracleDriver;DBXMySQLDriver;DBXFirebirdDriver;FireDACCommonDriver;LockBoxDR;inet;IndyIPCommon;dxSkinDarkRoomRS23;dxDockingRS23;vcl;dxSkinOffice2007GreenRS23;dxPScxExtCommonRS23;dxSkinsCoreRS23;FireDACDb2Driver;dxThemeRS23;dxSkinsdxRibbonPainterRS23;dxSkinVisualStudio2013BlueRS23;dxSkinMoneyTwinsRS23;dxPSdxFCLnkRS23;dxtrmdRS23;TeeDB;FireDAC;cxSchedulerTreeBrowserRS23;dxFireDACServerModeRS23;dxSkinBlueRS23;OverbyteIcsD10SRun;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;cxEditorsRS23;dxSkinGlassOceansRS23;dxSkinsdxNavBarPainterRS23;dxGaugeControlRS23;ibxpress;Tee;dxSkinSharpRS23;DataSnapServer;ibxbindings;cxPivotGridOLAPRS23;vclwinx;FireDACDSDriver;dxSkinBlueprintRS23;dxSkinOffice2007BlackRS23;CustomIPTransport;vcldsnap;dxSkinOffice2013LightGrayRS23;bindcomp;DBXInformixDriver;officeXPrt;dxPSdxGaugeControlLnkRS23;dxPScxPivotGridLnkRS23;dxorgcRS23;dxPSdxDBTVLnkRS23;vclribbon;dbxcds;adortl;dxComnRS23;dsnapxml;dbrtl;inetdbxpress;IndyProtocols;cxExportRS23;dxSkinOffice2016DarkRS23;dxSkinVisualStudio2013DarkRS23;dxSkinscxSchedulerPainterRS23;fmxase;$(DCC_UsePackage) + + + DEBUG;$(DCC_Define) + true + false + true + true + true + + + true + CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(MSBuildProjectName);FileDescription=$(MSBuildProjectName);ProductName=$(MSBuildProjectName) + 1033 + Debug + true + false + PerMonitor + + + false + RELEASE;$(DCC_Define) + 0 + 0 + + + true + PerMonitor + + + + MainSource + + +
Form5
+ dfm +
+ + Cfg_2 + Base + + + Base + + + Cfg_1 + Base + +
+ + Delphi.Personality.12 + Application + + + + JWTClient.dpr + + + Embarcadero C++Builder Office 2000 Servers Package + Embarcadero C++Builder Office XP Servers Package + Microsoft Office 2000 Sample Automation Server Wrapper Components + Microsoft Office XP Sample Automation Server Wrapper Components + + + + + + JWTClient.exe + true + + + + + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + classes + 1 + + + classes + 1 + + + + + res\xml + 1 + + + res\xml + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\armeabi + 1 + + + library\lib\armeabi + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + library\lib\mips + 1 + + + library\lib\mips + 1 + + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + + + library\lib\armeabi-v7a + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\values-v21 + 1 + + + res\values-v21 + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + res\drawable + 1 + + + res\drawable + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-ldpi + 1 + + + res\drawable-ldpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-mdpi + 1 + + + res\drawable-mdpi + 1 + + + + + res\drawable-hdpi + 1 + + + res\drawable-hdpi + 1 + + + + + res\drawable-xhdpi + 1 + + + res\drawable-xhdpi + 1 + + + + + res\drawable-xxhdpi + 1 + + + res\drawable-xxhdpi + 1 + + + + + res\drawable-xxxhdpi + 1 + + + res\drawable-xxxhdpi + 1 + + + + + res\drawable-small + 1 + + + res\drawable-small + 1 + + + + + res\drawable-normal + 1 + + + res\drawable-normal + 1 + + + + + res\drawable-large + 1 + + + res\drawable-large + 1 + + + + + res\drawable-xlarge + 1 + + + res\drawable-xlarge + 1 + + + + + res\values + 1 + + + res\values + 1 + + + + + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + Contents\MacOS + 1 + .framework + + + Contents\MacOS + 1 + .framework + + + 0 + + + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + 0 + .dll;.bpl + + + + + 1 + .dylib + + + 1 + .dylib + + + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + Contents\MacOS + 1 + .dylib + + + 0 + .bpl + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + Contents\Resources\StartUp\ + 0 + + + Contents\Resources\StartUp\ + 0 + + + 0 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + + + 1 + + + 1 + + + + + ..\ + 1 + + + ..\ + 1 + + + + + 1 + + + 1 + + + 1 + + + + + 1 + + + 1 + + + 1 + + + + + ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF + 1 + + + + + ..\ + 1 + + + ..\ + 1 + + + + + Contents + 1 + + + Contents + 1 + + + + + Contents\Resources + 1 + + + Contents\Resources + 1 + + + + + library\lib\armeabi-v7a + 1 + + + library\lib\arm64-v8a + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + Contents\MacOS + 1 + + + Contents\MacOS + 1 + + + 0 + + + + + library\lib\armeabi-v7a + 1 + + + + + 1 + + + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + Assets + 1 + + + Assets + 1 + + + + + + + + + + + + + + + True + False + + + 12 + + + + +
diff --git a/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.dfm b/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.dfm new file mode 100644 index 00000000..c181d5cd --- /dev/null +++ b/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.dfm @@ -0,0 +1,136 @@ +object Form5: TForm5 + Left = 0 + Top = 0 + Caption = 'Form5' + ClientHeight = 460 + ClientWidth = 647 + Color = clBtnFace + Font.Charset = DEFAULT_CHARSET + Font.Color = clWindowText + Font.Height = -11 + Font.Name = 'Tahoma' + Font.Style = [] + OldCreateOrder = False + PixelsPerInch = 96 + TextHeight = 13 + object Splitter1: TSplitter + Left = 0 + Top = 309 + Width = 647 + Height = 3 + Cursor = crVSplit + Align = alBottom + ExplicitLeft = -16 + ExplicitTop = 302 + ExplicitWidth = 513 + end + object Splitter2: TSplitter + Left = 0 + Top = 147 + Width = 647 + Height = 3 + Cursor = crVSplit + Align = alTop + ExplicitLeft = -8 + ExplicitTop = 302 + ExplicitWidth = 513 + end + object Memo1: TMemo + Left = 0 + Top = 49 + Width = 647 + Height = 98 + Align = alTop + Font.Charset = ANSI_CHARSET + Font.Color = clWindowText + Font.Height = -13 + Font.Name = 'Courier New' + Font.Style = [] + ParentFont = False + ReadOnly = True + TabOrder = 1 + end + object Memo2: TMemo + Left = 0 + Top = 150 + Width = 647 + Height = 159 + Align = alClient + Font.Charset = ANSI_CHARSET + Font.Color = clWindowText + Font.Height = -13 + Font.Name = 'Courier New' + Font.Style = [] + ParentFont = False + ReadOnly = True + TabOrder = 2 + end + object Panel1: TPanel + Left = 0 + Top = 0 + Width = 647 + Height = 49 + Align = alTop + TabOrder = 0 + object btnGet: TButton + AlignWithMargins = True + Left = 223 + Top = 4 + Width = 154 + Height = 41 + Align = alLeft + Caption = 'Get a protected resource' + TabOrder = 1 + OnClick = btnGetClick + end + object btnLOGIN: TButton + AlignWithMargins = True + Left = 4 + Top = 4 + Width = 101 + Height = 41 + Align = alLeft + Caption = 'Login (mode 1)' + TabOrder = 0 + OnClick = btnLOGINClick + end + object btnLoginWithException: TButton + AlignWithMargins = True + Left = 512 + Top = 4 + Width = 131 + Height = 41 + Align = alRight + Caption = 'Custom Exception in OnAuthenticate' + TabOrder = 2 + WordWrap = True + OnClick = btnLoginWithExceptionClick + end + object btnLoginJsonObject: TButton + AlignWithMargins = True + Left = 111 + Top = 4 + Width = 106 + Height = 41 + Align = alLeft + Caption = 'Login (mode 2)' + TabOrder = 3 + OnClick = btnLoginJsonObjectClick + end + end + object Memo3: TMemo + Left = 0 + Top = 312 + Width = 647 + Height = 148 + Align = alBottom + Font.Charset = ANSI_CHARSET + Font.Color = clWindowText + Font.Height = -13 + Font.Name = 'Courier New' + Font.Style = [] + ParentFont = False + ReadOnly = True + TabOrder = 3 + end +end diff --git a/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.pas b/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.pas new file mode 100644 index 00000000..6c6de04e --- /dev/null +++ b/samples/jsonwebtoken_roleauth/vclclient/MainClientFormU.pas @@ -0,0 +1,212 @@ +unit MainClientFormU; + +interface + +uses + Winapi.Windows, + Winapi.Messages, + System.SysUtils, + System.Variants, + System.Classes, + MVCFramework.Middleware.JWT, + Vcl.Graphics, + Vcl.Controls, + Vcl.Forms, + Vcl.Dialogs, + Vcl.StdCtrls, + Vcl.ExtCtrls; + +type + TForm5 = class(TForm) + Memo1: TMemo; + Memo2: TMemo; + Panel1: TPanel; + btnGet: TButton; + btnLOGIN: TButton; + Splitter1: TSplitter; + Memo3: TMemo; + Splitter2: TSplitter; + btnLoginWithException: TButton; + btnLoginJsonObject: TButton; + procedure btnGetClick(Sender: TObject); + procedure btnLOGINClick(Sender: TObject); + procedure btnLoginWithExceptionClick(Sender: TObject); + procedure btnLoginJsonObjectClick(Sender: TObject); + private + FJWT: string; + procedure SetJWT(const Value: string); + property JWT: string read FJWT write SetJWT; + public + { Public declarations } + end; + +var + Form5: TForm5; + +implementation + +{$R *.dfm} + + +uses + MVCFramework.RESTClient, + MVCFramework.SystemJSONUtils, + System.JSON; + +procedure TForm5.btnGetClick(Sender: TObject); +var + lClient: TRESTClient; + lResp: IRESTResponse; + lQueryStringParams: TStringList; +begin + { Getting JSON response } + lClient := TRESTClient.Create('localhost', 8080); + try + lClient.UseBasicAuthentication := False; + lClient.ReadTimeOut(0); + if not FJWT.IsEmpty then + begin + lClient.RequestHeaders.Values[TMVCJWTDefaults.AUTHORIZATION_HEADER] := 'Bearer ' + FJWT; + end; + lQueryStringParams := TStringList.Create; + try + lQueryStringParams.Values['firstname'] := 'Daniele'; + lQueryStringParams.Values['lastname'] := 'Teti'; + lResp := lClient.doGET('/admin/role1', [], lQueryStringParams); + if lResp.HasError then + ShowMessage(lResp.Error.ExceptionMessage); + finally + lQueryStringParams.Free; + end; + Memo2.Lines.Text := lResp.BodyAsString; + finally + lClient.Free; + end; + + { Getting HTML response } + lClient := TRESTClient.Create('localhost', 8080); + try + // when the JWT authorization header is named "Authorization", the basic authorization must be disabled + lClient.UseBasicAuthentication := False; + + lClient.ReadTimeOut(0); + if not FJWT.IsEmpty then + lClient.RequestHeaders.Values[TMVCJWTDefaults.AUTHORIZATION_HEADER] := 'Bearer ' + FJWT; + lQueryStringParams := TStringList.Create; + try + lQueryStringParams.Values['firstname'] := 'Daniele'; + lQueryStringParams.Values['lastname'] := 'Teti'; + lResp := lClient.Accept('text/html').doGET('/admin/role1', [], lQueryStringParams); + if lResp.HasError then + ShowMessage(lResp.Error.ExceptionMessage); + finally + lQueryStringParams.Free; + end; + Memo3.Lines.Text := lResp.BodyAsString; + finally + lClient.Free; + end; + +end; + +procedure TForm5.btnLOGINClick(Sender: TObject); +var + lClient: TRESTClient; + lRest: IRESTResponse; + lJSON: TJSONObject; +begin + lClient := TRESTClient.Create('localhost', 8080); + try + lClient.ReadTimeOut(0); + lClient.Authentication('user1', 'user1'); + lRest := lClient.doPOST('/login', []); + if lRest.HasError then + begin + ShowMessage( + 'HTTP ERROR: ' + lRest.Error.HTTPError.ToString + sLineBreak + + 'APPLICATION ERROR CODE: ' + lRest.Error.ErrorNumber.ToString + sLineBreak + + 'EXCEPTION MESSAGE: ' + lRest.Error.ExceptionMessage); + + Exit; + end; + + lJSON := TSystemJSON.StringAsJSONObject(lRest.BodyAsString); + try + JWT := lJSON.GetValue('token').Value; + finally + lJSON.Free; + end; + finally + lClient.Free; + end; +end; + +procedure TForm5.btnLoginJsonObjectClick(Sender: TObject); +var + lClient: TRESTClient; + lRest: IRESTResponse; + lJSON: TJSONObject; +begin + lClient := TRESTClient.Create('localhost', 8080); + try + lClient.ReadTimeOut(0); + lRest := lClient.doPOST('/login', [], '{"jwtusername":"user1","jwtpassword":"user1"}'); + if lRest.HasError then + begin + ShowMessage( + 'HTTP ERROR: ' + lRest.Error.HTTPError.ToString + sLineBreak + + 'APPLICATION ERROR CODE: ' + lRest.Error.ErrorNumber.ToString + sLineBreak + + 'EXCEPTION MESSAGE: ' + lRest.Error.ExceptionMessage); + + Exit; + end; + + lJSON := TSystemJSON.StringAsJSONObject(lRest.BodyAsString); + try + JWT := lJSON.GetValue('token').Value; + finally + lJSON.Free; + end; + finally + lClient.Free; + end; +end; + +procedure TForm5.btnLoginWithExceptionClick(Sender: TObject); +var + lClient: TRESTClient; + lRest: IRESTResponse; + lJSON: TJSONObject; +begin + lClient := TRESTClient.Create('localhost', 8080); + try + lClient.ReadTimeOut(0); + lClient.Authentication('user_raise_exception', 'user_raise_exception'); + lRest := lClient.doPOST('/login', []); + if lRest.HasError then + begin + ShowMessage( + 'HTTP ERROR: ' + lRest.Error.HTTPError.ToString + sLineBreak + + 'APPLICATION ERROR CODE: ' + lRest.Error.ErrorNumber.ToString + sLineBreak + + 'EXCEPTION MESSAGE: ' + lRest.Error.ExceptionMessage); + Exit; + end; + + lJSON := TSystemJSON.StringAsJSONObject(lRest.BodyAsString); + try + JWT := lJSON.GetValue('token').Value; + finally + lJSON.Free; + end; + finally + lClient.Free; + end; +end; + +procedure TForm5.SetJWT(const Value: string); +begin + FJWT := Value; + Memo1.Lines.Text := Value; +end; + +end. diff --git a/samples/jsonwebtoken_roleauth/www/index.html b/samples/jsonwebtoken_roleauth/www/index.html new file mode 100644 index 00000000..e79e5c34 --- /dev/null +++ b/samples/jsonwebtoken_roleauth/www/index.html @@ -0,0 +1,18 @@ + + +

Here's the public and the private sections on this application server

+ +

+ The available users are shown below: +

    +
  • user1/user1 (role1)
  • +
  • user2/user2 (role2)
  • +
  • user3/user3 (role1, role2)
  • +
+

+ + \ No newline at end of file