2016-05-23 17:26:00 +02:00
|
|
|
unit MainClientFormU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2019-07-08 19:25:56 +02:00
|
|
|
Winapi.Windows,
|
|
|
|
Winapi.Messages,
|
|
|
|
System.SysUtils,
|
|
|
|
System.Variants,
|
|
|
|
System.Classes,
|
2019-07-24 20:48:22 +02:00
|
|
|
MVCFramework.Middleware.JWT,
|
2019-07-08 19:25:56 +02:00
|
|
|
Vcl.Graphics,
|
|
|
|
Vcl.Controls,
|
|
|
|
Vcl.Forms,
|
|
|
|
Vcl.Dialogs,
|
|
|
|
Vcl.StdCtrls,
|
|
|
|
Vcl.ExtCtrls;
|
2016-05-23 17:26:00 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TForm5 = class(TForm)
|
2016-06-23 11:42:16 +02:00
|
|
|
Memo1: TMemo;
|
|
|
|
Memo2: TMemo;
|
|
|
|
Panel1: TPanel;
|
2016-05-23 17:26:00 +02:00
|
|
|
btnGet: TButton;
|
|
|
|
btnLOGIN: TButton;
|
2016-06-23 11:42:16 +02:00
|
|
|
Splitter1: TSplitter;
|
2017-03-10 10:37:29 +01:00
|
|
|
Memo3: TMemo;
|
|
|
|
Splitter2: TSplitter;
|
2019-07-08 19:25:56 +02:00
|
|
|
btnLoginWithException: TButton;
|
2020-04-03 11:56:14 +02:00
|
|
|
btnLoginJsonObject: TButton;
|
2016-05-23 17:26:00 +02:00
|
|
|
procedure btnGetClick(Sender: TObject);
|
|
|
|
procedure btnLOGINClick(Sender: TObject);
|
2019-07-08 19:25:56 +02:00
|
|
|
procedure btnLoginWithExceptionClick(Sender: TObject);
|
2020-04-03 11:56:14 +02:00
|
|
|
procedure btnLoginJsonObjectClick(Sender: TObject);
|
2016-05-23 17:26:00 +02:00
|
|
|
private
|
2017-05-17 22:32:45 +02:00
|
|
|
FJWT: string;
|
|
|
|
procedure SetJWT(const Value: string);
|
|
|
|
property JWT: string read FJWT write SetJWT;
|
2016-05-23 17:26:00 +02:00
|
|
|
public
|
|
|
|
{ Public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form5: TForm5;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
2017-07-05 00:17:46 +02:00
|
|
|
MVCFramework.RESTClient,
|
2020-08-26 22:13:18 +02:00
|
|
|
MVCFramework.RESTClient.Intf,
|
2017-05-17 22:32:45 +02:00
|
|
|
MVCFramework.SystemJSONUtils,
|
2018-12-09 23:03:06 +01:00
|
|
|
System.JSON;
|
2016-05-23 17:26:00 +02:00
|
|
|
|
|
|
|
procedure TForm5.btnGetClick(Sender: TObject);
|
|
|
|
var
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient: IMVCRESTClient;
|
|
|
|
lResp: IMVCRESTResponse;
|
|
|
|
|
2016-05-23 17:26:00 +02:00
|
|
|
begin
|
2017-03-10 10:37:29 +01:00
|
|
|
{ Getting JSON response }
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient := TMVCRESTClient.New.BaseURL('localhost', 8080);
|
|
|
|
lClient.ReadTimeOut(0);
|
|
|
|
if not FJWT.IsEmpty then
|
|
|
|
begin
|
|
|
|
lClient.SetBearerAuthorization(FJWT);
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
2020-08-26 22:13:18 +02:00
|
|
|
lResp := lClient
|
|
|
|
.AddQueryStringParam('firstname', 'Daniele')
|
|
|
|
.AddQueryStringParam('lastname', 'Teti')
|
|
|
|
.Get('/admin/role1');
|
|
|
|
if not lResp.Success then
|
|
|
|
ShowMessage(lResp.Content);
|
|
|
|
Memo2.Lines.Text := lResp.Content;
|
2017-03-10 10:37:29 +01:00
|
|
|
|
|
|
|
{ Getting HTML response }
|
2019-06-11 19:42:03 +02:00
|
|
|
// when the JWT authorization header is named "Authorization", the basic authorization must be disabled
|
2020-08-26 22:13:18 +02:00
|
|
|
if not FJWT.IsEmpty then
|
|
|
|
begin
|
|
|
|
lClient.SetBearerAuthorization(FJWT);
|
2017-03-10 10:37:29 +01:00
|
|
|
end;
|
2020-08-26 22:13:18 +02:00
|
|
|
lResp := lClient
|
|
|
|
.AddQueryStringParam('firstname', 'Daniele')
|
|
|
|
.AddQueryStringParam('lastname', 'Teti')
|
|
|
|
.Accept('text/html')
|
|
|
|
.Get('/admin/role1');
|
|
|
|
if not lResp.Success then
|
|
|
|
ShowMessage(lResp.Content);
|
|
|
|
Memo3.Lines.Text := lResp.Content;
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm5.btnLOGINClick(Sender: TObject);
|
|
|
|
var
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient: IMVCRESTClient;
|
|
|
|
lRest: IMVCRESTResponse;
|
2017-05-17 22:32:45 +02:00
|
|
|
lJSON: TJSONObject;
|
2016-05-23 17:26:00 +02:00
|
|
|
begin
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient := TMVCRESTClient.New
|
|
|
|
.BaseURL('localhost', 8080)
|
|
|
|
.ReadTimeOut(0)
|
|
|
|
.SetBasicAuthorization('user1', 'user1');
|
|
|
|
lRest := lClient.Post('/login');
|
|
|
|
if not lRest.Success then
|
|
|
|
begin
|
|
|
|
ShowMessage(
|
|
|
|
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
|
|
|
|
'CONTENT MESSAGE: ' + lRest.Content);
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
|
2016-05-23 17:26:00 +02:00
|
|
|
try
|
2020-08-26 22:13:18 +02:00
|
|
|
JWT := lJSON.GetValue('token').Value;
|
2020-04-03 11:56:14 +02:00
|
|
|
finally
|
2020-08-26 22:13:18 +02:00
|
|
|
lJSON.Free;
|
2020-04-03 11:56:14 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm5.btnLoginJsonObjectClick(Sender: TObject);
|
|
|
|
var
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient: IMVCRESTClient;
|
|
|
|
lRest: IMVCRESTResponse;
|
2020-04-03 11:56:14 +02:00
|
|
|
lJSON: TJSONObject;
|
|
|
|
begin
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient := TMVCRESTClient.New
|
|
|
|
.BaseURL('localhost', 8080)
|
|
|
|
.ReadTimeOut(0);
|
|
|
|
lRest := lClient.Post('/login', '{"jwtusername":"user1","jwtpassword":"user1"}');
|
|
|
|
if not lRest.Success then
|
|
|
|
begin
|
|
|
|
ShowMessage(
|
|
|
|
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
|
|
|
|
'CONTENT MESSAGE: ' + lRest.Content);
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
|
2020-04-03 11:56:14 +02:00
|
|
|
try
|
2020-08-26 22:13:18 +02:00
|
|
|
JWT := lJSON.GetValue('token').Value;
|
2019-07-08 19:25:56 +02:00
|
|
|
finally
|
2020-08-26 22:13:18 +02:00
|
|
|
lJSON.Free;
|
2019-07-08 19:25:56 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm5.btnLoginWithExceptionClick(Sender: TObject);
|
|
|
|
var
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient: IMVCRESTClient;
|
|
|
|
lRest: IMVCRESTResponse;
|
2019-07-08 19:25:56 +02:00
|
|
|
lJSON: TJSONObject;
|
|
|
|
begin
|
2020-08-26 22:13:18 +02:00
|
|
|
lClient := TMVCRESTClient.New
|
|
|
|
.BaseURL('localhost', 8080)
|
|
|
|
.ReadTimeOut(0)
|
|
|
|
.SetBasicAuthorization('user_raise_exception', 'user_raise_exception');
|
|
|
|
|
|
|
|
lRest := lClient.Post('/login');
|
|
|
|
if not lRest.Success then
|
|
|
|
begin
|
|
|
|
ShowMessage(
|
|
|
|
'HTTP ERROR: ' + lRest.StatusCode.ToString + sLineBreak +
|
|
|
|
'CONTENT MESSAGE: ' + lRest.Content);
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
lJSON := TSystemJSON.StringAsJSONObject(lRest.Content);
|
2019-07-08 19:25:56 +02:00
|
|
|
try
|
2020-08-26 22:13:18 +02:00
|
|
|
JWT := lJSON.GetValue('token').Value;
|
2016-05-23 17:26:00 +02:00
|
|
|
finally
|
2020-08-26 22:13:18 +02:00
|
|
|
lJSON.Free;
|
2016-05-23 17:26:00 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-05-17 22:32:45 +02:00
|
|
|
procedure TForm5.SetJWT(const Value: string);
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
FJWT := Value;
|
|
|
|
Memo1.Lines.Text := Value;
|
|
|
|
end;
|
|
|
|
|
2016-05-23 17:26:00 +02:00
|
|
|
end.
|