10000 Merge branch 'develop' · Code4Delphi/DelphiWebCrud@3c0a496 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c0a496

Browse files
committed
Merge branch 'develop'
2 parents e023a9c + fbaee0f commit 3c0a496

14 files changed

+165
-31
lines changed

ClientWeb/ClientWeb.dpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ uses
77
WEBLib.Forms,
88
Main.View in 'Src\Main.View.pas' {MainView: TWebForm} {*.html},
99
Login.View in 'Src\Login.View.pas' {LoginView: TWebForm} {*.html},
10-
Clientes.Cadastrar.View in 'Src\ClientesCadastrar\Clientes.Cadastrar.View.pas' {ClientesCadastrarView: TWebForm} {*.html};
10+
Clientes.Cadastrar.View in 'Src\ClientesCadastrar\Clientes.Cadastrar.View.pas' {ClientesCadastrarView: TWebForm} {*.html},
11+
Configs in 'Src\Configs.pas';
1112

1213
{$R *.res}
1314

ClientWeb/ClientWeb.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<FormType>dfm</FormType>
124124
<DesignClass>TWebForm</DesignClass>
125125
</DCCReference>
126+
<DCCReference Include="Src\Configs.pas"/>
126127
<None Include="Index.html"/>
127128
<None Include="Index.css"/>
128129
<None Include="AdminLTE\dist\css\adminlte.css"/>

ClientWeb/Src/Configs.pas

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
unit Configs;
2+
3+
interface
4+
5+
var
6+
Configs_Token: string;
7+
8+
implementation
9+
10+
end.

ClientWeb/Src/Login.View.dfm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ object LoginView: TLoginView
6161
HeightPercent = 100.000000000000000000
6262
WidthPercent = 100.000000000000000000
6363
end
64+
object XDataWebConnection1: TXDataWebConnection
65+
URL = 'http://localhost:2001/tms/auth/'
66+
Connected = True
67+
Left = 72
68+
Top = 16
69+
end
70+
object XDataWebClient1: TXDataWebClient
71+
Connection = XDataWebConnection1
72+
OnError = XDataWebClient1Error
73+
Left = 216
74+
Top = 16
75+
end
6476
end

ClientWeb/Src/Login.View.pas

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ interface
55
uses
66
System.SysUtils,
77
System.Classes,
8-
JS,
98
Web,
109
WEBLib.Graphics,
1110
WEBLib.Controls,
1211
WEBLib.Forms,
1312
WEBLib.Dialogs,
1413
Vcl.Controls,
1514
Vcl.StdCtrls,
16-
WEBLib.StdCtrls,
17-
Main.View;
15+
WEBLib.StdCtrls, Main.View, XData.Web.Client, XData.Web.Connection, JS, Configs;
1816

1917
type
2018
TLoginView = class(TWebForm)
@@ -23,11 +21,16 @@ TLoginView = class(TWebForm)
2321
edtSenha: TWebEdit;
2422
btnEntrar: TWebButton;
2523
ckLembrarMe: TWebCheckBox;
24+
[Async]
25+
XDataWebConnection1: TXDataWebConnection;
26+
XDataWebClient1: TXDataWebClient;
27+
[Async]
2628
procedure btnEntrarClick(Sender: TObject);
2729
procedure WebFormShow(Sender: TObject);
2830
procedure edtLoginKeyPress(Sender: TObject; var Key: Char);
2931
procedure edtSenhaKeyPress(Sender: TObject; var Key: Char);
3032
procedure WebFormCreate(Sender: TObject);
33+
procedure XDataWebClient1Error(Error: TXDataClientError);
3134
private
3235

3336
public
@@ -52,6 +55,16 @@ procedure TLoginView.WebFormShow(Sender: TObject);
5255
edtLogin.SetFocus;
5356
end;
5457

58+
procedure TLoginView.XDataWebClient1Error(Error: TXDataClientError);
59+
begin
60+
ShowMessage(
61+
'StatusCode: ' + Error.StatusCode.ToString + sLineBreak +
62+
'RequestUrl: ' + Error.RequestUrl + sLineBreak +
63+
'RequestId: ' + Error.RequestId + sLineBreak +
64+
'ErrorCode: ' + Error.ErrorCode + sLineBreak +
65+
'ErrorMessage: ' + Error.ErrorMessage);
66+
end;
67+
5568
procedure TLoginView.edtLoginKeyPress(Sender: TObject; var Key: Char);
5669
begin
5770
if Key = #13 then
@@ -65,21 +78,36 @@ procedure TLoginView.edtSenhaKeyPress(Sender: TObject; var Key: Char);
6578
end;
6679

6780
procedure TLoginView.btnEntrarClick(Sender: TObject);
81+
var
82+
LResponse: TXDataClientResponse;
6883
begin
69-
if edtLogin.Text <> 'admin' then
84+
if Trim(edtLogin.Text).IsEmpty or Trim(edtSenha.Text).IsEmpty then
7085
begin
71-
ShowMessage('Login inválido');
86+
ShowMessage('Login e senha devem ser informados');
7287
edtLogin.SetFocus;
7388
Exit;
7489
end;
7590

76-
if edtSenha.Text <> 'admin' then
91+
LResponse := TAwait.Exec<TXDataClientResponse>(
92+
XDataWebClient1.RawInvokeAsync('ILoginService.Login', [edtLogin.Text, edtSenha.Text]));
93+
94+
if LResponse.StatusCode <> 200 then
7795
begin
78-
ShowMessage('Senha inválido');
79-
edtSenha.SetFocus;< EF5E /div>
96+
ShowMessage('Login ou senha informados são inválidos');
97+
edtLogin.SetFocus;
8098
Exit;
8199
end;
82100

101+
Configs_Token := string(TJSObject(LResponse.Result)['value']);
102+
if Configs_Token.Trim.IsEmpty then
103+
begin
104+
ShowMessage('Token não pode ser recuperado');
105+
edtLogin.SetFocus;
106+
Exit;
107+
end;
108+
109+
ShowMessage('Token: ' + Configs_Token);
110+
83111
MainView := TMainView.CreateNew;
84112
MainView.ShowModal;
85113
end;

ClientWeb/Src/Main.View.dfm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ object MainView: TMainView
916916
end
917917
object XDataWebConnection1: TXDataWebConnection
918918
URL = 'http://localhost:8000/tms/xdata'
919+
OnError = XDataWebConnection1Error
920+
OnRequest = XDataWebConnection1Request
919921
Left = 96
920922
Top = 16
921923
end

ClientWeb/Src/Main.View.pas

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,22 @@ interface
2121
XData.Web.Connection,
2222
WEBLib.DB,
2323
JS,
24-
Clientes.Cadastrar.View, VCL.TMSFNCTypes, VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes, System.Rtti,
25-
VCL.TMSFNCDataGridCell, VCL.TMSFNCDataGridData, VCL.TMSFNCDataGridBase, VCL.TMSFNCDataGridCore,
26-
VCL.TMSFNCDataGridRenderer, VCL.TMSFNCCustomControl, VCL.TMSFNCDataGrid, VCL.TMSFNCCustomComponent,
27-
VCL.TMSFNCDataGridDatabaseAdapter;
24+
Clientes.Cadastrar.View,
25+
VCL.TMSFNCTypes,
26+
VCL.TMSFNCUtils,
27+
VCL.TMSFNCGraphics,
28+
VCL.TMSFNCGraphicsTypes,
29+
System.Rtti,
30+
VCL.TMSFNCDataGridCell,
31+
VCL.TMSFNCDataGridData,
32+
VCL.TMSFNCDataGridBase,
33+
VCL.TMSFNCDataGridCore,
34+
VCL.TMSFNCDataGridRenderer,
35+
VCL.TMSFNCCustomControl,
36+
VCL.TMSFNCDataGrid,
37+
VCL.TMSFNCCustomComponent,
38+
VCL.TMSFNCDataGridDatabaseAdapter,
39+
Configs;
2840

2941
type
3042
TMainView = class(TWebForm)
@@ -73,6 +85,8 @@ TMainView = class(TWebForm)
7385
[Async]
7486
procedure btnDeleteClick(Sender: TObject);
7587
procedure XDataWebClient1Error(Error: TXDataClientError);
88+
procedure XDataWebConnection1Error(Error: TXDataWebConnectionError);
89+
procedure XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
7690
private
7791
function GetClientePreenchido(const AView: TClientesCadastrarView): TJSObject;
7892
public
@@ -131,6 +145,16 @@ procedure TMainView.XDataWebClient1Error(Error: TXDataClientError);
131145
'ErrorMessage: ' + Error.ErrorMessage);
132146
end;
133147

148+
procedure TMainView.XDataWebConnection1Error(Error: TXDataWebConnectionError);
149+
begin
150+
ShowMessage('StatusCode: ' + Error.ErrorMessage);
151+
end;
152+
153+
procedure TMainView.XDataWebConnection1Request(Args: TXDataWebConnectionRequest);
154+
begin
155+
Args.Request.Headers.SetValue('Authorization', 'Bearer ' + Configs_Token);
156+
end;
157+
134158
procedure TMainView.btnGetNomeClick(Sender: TObject);
135159
var
136160
LResponse: TXDataClientResponse;
@@ -142,6 +166,9 @@ procedure TMainView.btnGetNomeClick(Sender: TObject);
142166
Exit;
143167
end;
144168

169+
if not XDataWebConnection1.Connected then
170+
XDataWebConnection1.Open;
171+
145172
LResponse := TAwait.Exec<TXDataClientResponse>(
146173
XDataWebClient1.RawInvokeAsync('IClientesService.GetNome', [StrToIntDef(edtCodigo.Text, 0)]));
147174

@@ -159,6 +186,9 @@ procedure TMainView.btnGetClick(Sender: TObject);
159186
Exit;
160187
end;
161188

189+
if not XDataWebConnection1.Connected then
190+
XDataWebConnection1.Open;
191+
162192
LResponse := TAwait.Exec<TXDataClientResponse>(
163193
XDataWebClient1.RawInvokeAsync('IClientesService.Get', [StrToIntDef(edtCodigo.Text, 0)]));
164194

@@ -180,6 +210,9 @@ procedure TMainView.btnListarClick(Sender: TObject);
180210
var
181211
LResponse: TXDataClientResponse;
182212
begin
213+
if not XDataWebConnection1.Connected then
214+
XDataWebConnection1.Open;
215+
183216
LResponse := TAwait.Exec<TXDataClientResponse>(
184217
XDataWebClient1.RawInvokeAsync('IClientesService.List', []));
185218

@@ -200,6 +233,9 @@ procedure TMainView.btnDeleteClick(Sender: TObject);
200233
var
201234
LResponse: TXDataClientResponse;
202235
begin
236+
if not XDataWebConnection1.Connected then
237+
XDataWebConnection1.Open;
238+
203239
if await(TModalResult, MessageDlgAsync('Confirma realmente deletar o registro?', mtConfirmation, [mbYes, mbNo])) <> mrYes then
204240
Exit;
205241

Server/Src/XData.DM.dfm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ object XDataDM: TXDataDM
1313
end
1414
object XDataServer1Compress: TSparkleCompressMiddleware
1515
end
16+
object XDataServer1JWT: TSparkleJwtMiddleware
17+
ForbidAnonymousAccess = True
18+
OnGetSecretEx = XDataServer1JWTGetSecretEx
19+
OnForbidRequest = XDataServer1JWTForbidRequest
20+
end
1621
end
1722
object SparkleHttpSysDispatcher1: TSparkleHttpSysDispatcher
1823
Left = 128

Server/Src/XData.DM.pas

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ interface
1010
XData.Server.Module,
1111
Sparkle.Comp.Server,
1212
Sparkle.Comp.HttpSysDispatcher,
13-
XData.Comp.Server, Sparkle.Comp.CompressMiddleware, Sparkle.Comp.CorsMiddleware;
13+
XData.Comp.Server,
14+
Sparkle.Comp.CompressMiddleware,
15+
Sparkle.Comp.CorsMiddleware, Sparkle.Comp.JwtMiddleware;
1416

1517
type
1618
TXDataDM = class(TDataModule)
1719
XDataServer1: TXDataServer;
1820
SparkleHttpSysDispatcher1: TSparkleHttpSysDispatcher;
1921
XDataServer1CORS: TSparkleCorsMiddleware;
2022
XDataServer1Compress: TSparkleCompressMiddleware;
23+
XDataServer1JWT: TSparkleJwtMiddleware;
24+
procedure XDataServer1JWTGetSecretEx(Sender: TObject; const JWT: TJWT; Context: THttpServerContext;
25+
var Secret: TBytes);
26+
procedure XDataServer1JWTForbidRequest(Sender: TObject; Context: THttpServerContext; var Forbid: Boolean);
2127
private
2228

2329
public
@@ -33,4 +39,16 @@ implementation
3339

3440
{$R *.dfm}
3541

42+
procedure TXDataDM.XDataServer1JWTForbidRequest(Sender: TObject; Context: THttpServerContext; var Forbid: Boolean);
43+
begin
44+
if Context.Request.Uri.AbsolutePath.Contains('swagger') then
45+
Forbid := False;
46+
end;
47+
48+
procedure TXDataDM.XDataServer1JWTGetSecretEx(Sender: TObject; const JWT: TJWT; Context: THttpServerContext;
49+
var Secret: TBytes);
50+
begin
51+
Secret := TEncoding.UTF8.GetBytes('sua-chave-secreta-1234567890-12345');
52+
end;
53+
3654
end.

ServerAuth/Src/LoginService/LoginServiceImplementation.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function TLoginService.Login(const User, Password: string): string;
4040
JWT.Claims.SetClaimOfType<Boolean>('admin', True);
4141
JWT.Claims.SetClaimOfType<string>('teste', 'asdf');
4242

43-
Result := TJOSE.SHA256CompactToken('sua-chave-secreta', JWT);
43+
Result := TJOSE.SHA256CompactToken('sua-chave-secreta-1234567890-12345', JWT);
4444
finally
4545
JWT.Free;
4646
end;

0 commit comments

Comments
 (0)
0