-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGHttpsRestJWTClient_1.dpr
More file actions
140 lines (128 loc) · 4.54 KB
/
GHttpsRestJWTClient_1.dpr
File metadata and controls
140 lines (128 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
program GHttpsRestJWTClient_1;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
System.JSON,
System.Net.HttpClient,
System.Net.URLClient,
System.NetEncoding;
type
TValidate = class
public
class procedure ValidateServerCertificate(const Sender: TObject;
const ARequest: TURLRequest; const Certificate: TCertificate;
var Accepted: Boolean);
end;
class procedure TValidate.ValidateServerCertificate(const Sender: TObject;
const ARequest: TURLRequest; const Certificate: TCertificate;
var Accepted: Boolean);
begin
Accepted := True;
end;
const
HOST_URL = 'https://localhost:8443';
USERNAME = 'admin';
PASSWORD = 'admin';
var
HttpClient: THTTPClient;
Response: IHTTPResponse;
LoginJson, ResponseJson: TJSONObject;
Token: string;
begin
try
HttpClient := THTTPClient.Create;
try
HTTPClient.SecureProtocols := [THTTPSecureProtocol.TLS1, THTTPSecureProtocol.TLS11, THTTPSecureProtocol.TLS12];
HTTPClient.OnValidateServerCertificate := TValidate.ValidateServerCertificate;
HttpClient.ConnectionTimeout := 4000;
HttpClient.ResponseTimeout := 4000;
WriteLn('=== Test GHttpsRestSvrJWTPrj ===');
WriteLn;
WriteLn('1. Getting JWT token...');
LoginJson := TJSONObject.Create;
try
LoginJson.AddPair('username', USERNAME);
LoginJson.AddPair('password', PASSWORD);
try
Response := HttpClient.Post(
HOST_URL + '/api/token',
TStringStream.Create(LoginJson.ToJSON),
nil,
TNetHeaders.Create(TNameValuePair.Create('Content-Type', 'application/json'))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
if Response.StatusCode = 200 then
begin
ResponseJson := TJSONObject.ParseJSONValue(Response.ContentAsString) as TJSONObject;
try
if Assigned(ResponseJson) then
begin
Token := ResponseJson.GetValue<string>('token', '');
if Token <> '' then
begin
WriteLn('JWT Token obtained successfully!');
WriteLn('2. Calling protected endpoint with JWT token...');
try
Response := HttpClient.Get(
HOST_URL + '/api/autotest',
nil,
TNetHeaders.Create(TNameValuePair.Create('Authorization', 'Bearer ' + Token))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
WriteLn('3. Calling protected endpoint with invalid token...');
Response := HttpClient.Get(
HOST_URL + '/api/autotest',
nil,
TNetHeaders.Create(TNameValuePair.Create('Authorization', 'Bearer invalid.token.value'))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
WriteLn('4. Calling protected endpoint without token...');
Response := HttpClient.Get(HOST_URL + '/api/autotest');
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
except
on E: Exception do
WriteLn('Error calling protected endpoint: ', E.Message);
end;
end
else
WriteLn('Error: Token not found in response.');
end
else
WriteLn('Error: Invalid JSON response.');
finally
ResponseJson.Free;
end;
end
else
WriteLn('Error: Could not obtain token. Check if server is running.');
except
on E: Exception do
WriteLn('Error obtaining token: ', E.Message);
end;
finally
LoginJson.Free;
end;
finally
HttpClient.Free;
end;
WriteLn;
WriteLn('=== End of test ===');
WriteLn('Press Enter to exit...');
ReadLn;
except
on E: Exception do
begin
WriteLn('Unexpected error: ', E.Message);
ReadLn;
end;
end;
end.