@@ -27,20 +27,28 @@ interface
27
27
uses
28
28
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
29
29
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdGlobal, System.Generics.Defaults,
30
- System.Generics.Collections, Vcl.ExtCtrls, Vcl.ComCtrls,
30
+ System.Generics.Collections, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Mask,
31
31
32
32
JOSE.Core.JWT,
33
33
JOSE.Core.JWS,
34
34
JOSE.Core.JWK,
35
35
JOSE.Core.JWA,
36
- JOSE.Types.JSON;
36
+ JOSE.Types.JSON,
37
+ JOSE.Types.Bytes,
38
+ JOSE.Core.Builder,
39
+ JOSE.Context,
40
+ JOSE.Consumer,
41
+ JOSE.Consumer.Validators;
37
42
38
43
type
39
44
TMyClaims = class (TJWTClaims)
40
45
private
46
+ function GetNonce : string;
47
+ procedure SetNonce (const Value : string);
41
48
function GetAppIssuer : string;
42
49
procedure SetAppIssuer (const Value : string);
43
50
public
51
+ property Nonce: string read GetNonce write SetNonce;
44
52
property AppIssuer: string read GetAppIssuer write SetAppIssuer;
45
53
end ;
46
54
@@ -61,15 +69,17 @@ TfrmMain = class(TForm)
61
69
chkIssuedAt: TCheckBox;
62
70
chkExpires: TCheckBox;
63
71
chkNotBefore: TCheckBox;
64
- Button1: TButton;
65
72
edtIssuedAtDate: TDateTimePicker;
66
73
edtExpiresTime: TDateTimePicker;
67
74
edtNotBeforeTime: TDateTimePicker;
68
75
cbbAlgorithm: TComboBox;
69
76
Label6: TLabel;
77
+ btnCheckCustom: TButton;
78
+ procedure btnCheckCustomClick (Sender: TObject);
70
79
procedure btnCustomClaimsClick (Sender: TObject);
80
+ procedure Button1Click (Sender: TObject);
71
81
private
72
- { Private declarations }
82
+ FCompact: TJOSEBytes;
73
83
public
74
84
{ Public declarations }
75
85
end ;
@@ -81,8 +91,7 @@ implementation
81
91
82
92
uses
83
93
System.Rtti,
84
- JOSE.Types.Bytes,
85
- JOSE.Core.Builder;
94
+ System.DateUtils;
86
95
87
96
{ $R *.dfm}
88
97
@@ -97,10 +106,13 @@ procedure TfrmMain.btnCustomClaimsClick(Sender: TObject);
97
106
98
107
LClaims.IssuedAt := Now;
99
108
LClaims.Expiration := Now + 1 ;
109
+ LClaims.Subject := ' paolo-rossi' ;
100
110
LClaims.Issuer := ' WiRL REST Library' ;
101
111
LClaims.AppIssuer :=' CustomClaims' ;
112
+ LClaims.Nonce := ' 9876543' ;
102
113
103
- mmoCompact.Lines.Add(TJOSE.SHA256CompactToken(' secret' , LToken));
114
+ FCompact := TJOSE.SHA256CompactToken(' secret' , LToken);
115
+ mmoCompact.Lines.Add(FCompact);
104
116
105
117
mmoJSON.Lines.Add(TJSONUtils.ToJSON(LToken.Header.JSON));
106
118
mmoJSON.Lines.Add(TJSONUtils.ToJSON(LToken.Claims.JSON));
@@ -109,16 +121,75 @@ procedure TfrmMain.btnCustomClaimsClick(Sender: TObject);
109
121
end ;
110
122
end ;
111
123
124
+ procedure TfrmMain.btnCheckCustomClick (Sender: TObject);
125
+ var
126
+ LConsumer: IJOSEConsumer;
127
+ begin
128
+ LConsumer := TJOSEConsumerBuilder.NewConsumer
129
+ .SetClaimsClass(TMyClaims)
130
+
131
+ // JWS-related validation
132
+ .SetVerificationKey(' secret' )
133
+ .SetSkipVerificationKeyValidation
134
+ .SetDisableRequireSignature
135
+
136
+ // string-based claims validation
137
+ .SetExpectedSubject(' paolo-rossi' )
138
+
139
+ .RegisterValidator(
140
+ function (AJOSEContext: TJOSEContext): string
141
+ var
142
+ LNonce: string;
143
+ begin
144
+ Result := ' ' ;
145
+ LNonce := AJOSEContext.GetClaims<TMyClaims>.Nonce;
146
+
147
+ if not AJOSEContext.GetClaims.ClaimExists(' nonce' ) then
148
+ Exit(' Nonce was not present' );
149
+
150
+ if not (LNonce = ' 9876543' ) then
151
+ Exit(Format(' Nonce [nonce] claim value [%s] doesn'' t match expected value of [%s]' ,
152
+ [LNonce, ' 9876543' ]));
153
+ end
154
+ )
155
+ // Build the consumer object
156
+ .Build();
157
+
158
+ mmoCompact.Lines.Add(' ======================================' );
159
+ try
160
+ LConsumer.Process(FCompact);
161
+ mmoCompact.Lines.Add(' Validation process passed' );
162
+ except
163
+ on E: EInvalidJWTException do
164
+ mmoCompact.Lines.Add(E.Message);
165
+ end ;
166
+ end ;
167
+
168
+ procedure TfrmMain.Button1Click (Sender: TObject);
169
+ begin
170
+
171
+ end ;
172
+
112
173
{ TMyClaims }
113
174
114
175
function TMyClaims.GetAppIssuer : string;
115
176
begin
116
177
Result := TJSONUtils.GetJSONValue(' ais' , FJSON).AsString;
117
178
end ;
118
179
180
+ function TMyClaims.GetNonce : string;
181
+ begin
182
+ Result := TJSONUtils.GetJSONValue(' nonce' , FJSON).AsString;
183
+ end ;
184
+
119
185
procedure TMyClaims.SetAppIssuer (const Value : string);
120
186
begin
121
187
TJSONUtils.SetJSONValueFrom<string>(' ais' , Value , FJSON);
122
188
end ;
123
189
190
+ procedure TMyClaims.SetNonce (const Value : string);
191
+ begin
192
+ TJSONUtils.SetJSONValueFrom<string>(' nonce' , Value , FJSON);
193
+ end ;
194
+
124
195
end .
0 commit comments