-
Notifications
You must be signed in to change notification settings - Fork 5
/
servletJWT.rpgle
519 lines (419 loc) · 13.4 KB
/
servletJWT.rpgle
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<%@ language="RPGLE" pgmtype="SRVPGM" pgmopt="export(*ALL)" %><%
**FREE
ctl-opt nomain;
ctl-opt copyright('System & Method A/S (C) sitemule, 2021');
ctl-opt decEdit('0,') datEdit(*YMD.);
ctl-opt bndDir('ICEBREAK');
ctl-opt debug(*yes);
ctl-opt thread(*CONCURRENT);
// --------------------------------------------------------------------------------------------------------------
// Copyright [2021] [System & Method A/S]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// --------------------------------------------------------------------------------------------------------------
//
// Project . . . : IceBreak
// Design . . . : Niels Liisberg
// Function . . : Sample service program to show prerequest servlet in action
//
//
// Prerequest is a special kind of a servlet - a plugin into the normal IceBreak flow
//
// to activate you prerequisite - your own service program - you have to append these tags into the webconfig.xml :
//
//
//
// First you need to setup the <plugin> tag in the webconfig to point to this
// service program and procedure :
//
// <!-- to handle the session by API, please look at the SVCSESMAN examples -->
// <plugin>
// <map function="preRequest" pgm="servletJWT" lib="*LIBL" proc="validateJWT"/>
// </plugin>
//
// In this case your service program called servletJWT will be loaded once at startup and for each request
// you procedure "preRequest" will be called it will hat will be a service
// So you have to implement the "preRequest" procedure like in this example.
//
// Also note: Your procedures need to be written re-entrant. It need to be thread safe
// so you need the ctl-opt thread(*CONCURRENT)
//
// An please note in threadfae applications:
// that is:
//
// DONT USE:
// ANY GLOBAL VARIABLES
// ANY GLOBAL FILES
// CALL TO CL/RPG/COBOL PROGRAMS
// .. unless You know what you are doing (I.e. using mutexes or semaphors)
//
// Read more here:
//
// https://www.ibm.com/docs/en/i/7.4?topic=applications-multithreaded-programming-techniques
//
//
// By Date Task Description
// -------- ---------- ------- ----------------------------------------------------------------------------------
// NLI 16.04.2021 0000000 New program
// --------------------------------------------------------------------------------------------------------------
// Note!! You need to use the servlet headers - NO IceBreak call back are allowed here
// Look in qAspHdr,servlet fro all API's available
/include qAspHdr,servlet
/include qAspHdr,jsonParser
/include qsysinc/qrpglesrc,qusec
/include 'headers/jwt_h.rpgle'
/include 'headers/servletJWT_h.rpgle'
// --------------------------------------------------------------------------------------------------------------
// Note: This is the name defined in "webconfig.xml"
// Note: servlets have always the same signature (request : response) and returns *ON if no further action
//
// You have 100% access to both the request and response and you can disable
// everything in the normal IceBreak flow:
//
// Here we validate a JWT token, and only if OK it will continu the call to applicatinserver ( pool)
//
// You can place this JWT used here is directly into i.e. Postman:
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.dKe7hfqSDavMTEN7WghKk3vlhEieEufgQzXRZzBtTFw
//
// --------------------------------------------------------------------------------------------------------------
dcl-proc validateJWT export;
dcl-pi *n ind;
req pointer value;
res pointer value;
end-pi;
dcl-s jwt varchar(8000);
dcl-s p int(5);
dcl-s ok ind;
dcl-s secret varchar(256);
secret = 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd';
jwt = req_header ( req : 'Authorization');
if jwt = '';
res_setStatus ( res : '401 no jwt token provided');
return *ON; // break flow
endif;
// skip the Bearer ( don't care if it being with B or b)
jwt = %triml(jwt);
p = %scan ('earer' : jwt);
if p <= 0;
res_setStatus ( res : '401 no Bearer in jwt token provided');
return *ON; // break flow
endif;
jwt = %triml(%subst (jwt : p +6 ));
ok = jwt_verify (jwt:secret);
if not ok;
res_setStatus ( res : '401 jwt token is invalid or expired');
return *ON; // break flow
endif;
// *OFF: Continue the normal server flow
// *ON : Dont continue, break the flow - we have served what is need
return *OFF;
end-proc;
// --------------------------------------------------------------------------------------------------------------
///
// ILEastic : JWT Service Program
//
// This service program offers procedures for signing and verifying JWT tokens.
//
// @author Mihael Schmidt
// @date 04.05.2019
// @project ILEastic
///
dcl-proc jwt_verify;
dcl-pi *n ind;
token like(jwt_token_t) const ccsid(*utf8);
signKey like(jwt_signKey_t) const ccsid(*utf8);
end-pi;
dcl-s valid ind inz(*off);
dcl-s serverSignedToken like(jwt_token_t) ccsid(*utf8);
dcl-s header like(jwt_token_t) ccsid(*utf8);
dcl-s payload like(jwt_token_t);
dcl-s json pointer;
header = jwt_decodeHeader(token);
payload = jwt_decodePayload(token);
serverSignedToken = jwt_sign(jwt_HS256 : payload : signKey);
if (token = serverSignedToken);
json = json_parseString(payload);
valid = (not isExpired(json)) and isActive(json);
json_close(json);
endif;
return valid;
end-proc;
dcl-proc jwt_decodeHeader ;
dcl-pi *n like(jwt_token_t) ccsid(*utf8);
token like(jwt_token_t) const ccsid(*utf8);
end-pi;
dcl-s x int(10);
dcl-s decoded like(jwt_token_t) ccsid(*utf8);
dcl-s header like(jwt_token_t) ccsid(*utf8);
// JWT header
x = %scan(UTF8_PERIOD : token);
if (x = 0);
return *blank;
endif;
header = %subst(token : 1 : x - 1);
decoded = decodeBase64Url(header);
return decoded;
end-proc;
dcl-proc jwt_decodePayload ;
dcl-pi *n like(jwt_token_t) ccsid(*utf8);
token like(jwt_token_t) const ccsid(*utf8);
end-pi;
dcl-s x int(10);
dcl-s x2 int(10);
dcl-s decoded like(jwt_token_t) ccsid(*utf8);
dcl-s payload like(jwt_token_t) ccsid(*utf8);
// JWT header
x = %scan(UTF8_PERIOD : token);
if (x = 0);
return *blank;
endif;
// JWT payload
x2 = %scan(UTF8_PERIOD : token : x+1);
if (x2 = 0);
return *blank;
endif;
payload = %subst(token : x+1 : x2 - x);
decoded = decodeBase64Url(payload);
return decoded;
end-proc;
dcl-proc jwt_sign ;
dcl-pi *n like(jwt_token_t) ccsid(*utf8);
algorithm char(100) const;
pPayload like(jwt_token_t) const ccsid(*utf8);
signKey like(jwt_signKey_t) const ccsid(*utf8);
claims likeds(jwt_claims_t) const options(*nopass);
end-pi;
dcl-pr memcpy pointer extproc('memcpy');
dest pointer value;
source pointer value;
count uns(10) value;
end-pr;
dcl-pr sys_calculateHmac extproc('Qc3CalculateHMAC');
input pointer value;
inputLength int(10) const;
inputDataFormat char(8) const;
algorithm char(65535) const;
algorithmFormat char(8) const;
key char(1000) const;
keyFormat char(8) const;
cryptoServiceProvier char(1) const;
cryptoDeviceName char(10) const;
hash char(32);
errorCode likeds(QUSEC);
end-pr;
dcl-ds algd0500_t qualified template;
algorithm int(10);
end-ds;
dcl-c ALGORITHM_SHA256 3;
dcl-ds keyd0200_t qualified template;
type int(10);
length int(10);
format char(1);
reserved char(3);
key char(100) ccsid(*utf8);
end-ds;
dcl-s headerPayload char(65535) ccsid(*utf8);
dcl-s header like(jwt_token_t) ccsid(*utf8);
dcl-s encoded like(jwt_token_t) ccsid(*utf8);
dcl-s hash char(32);
dcl-s tmpHash char(32) ccsid(*utf8);
dcl-ds algd0500 likeds(algd0500_t);
dcl-ds keyparam likeds(keyd0200_t) inz;
dcl-s base64Encoded like(jwt_token_t) ccsid(*utf8);
dcl-s paddingChar char(1) inz('=') ccsid(*utf8);
dcl-s payload like(jwt_token_t) ccsid(*utf8);
if (algorithm <> jwt_HS256);
joblog('Unsupported algorithm %s' : algorithm);
return *blank;
endif;
header = '{"alg":"' + jwt_HS256 + '","typ":"JWT"}';
payload = pPayload;
if (%parms() >= 4);
payload = addClaims(payload : claims);
endif;
base64Encoded = encodeBase64Url(payload);
base64Encoded = %trimr(base64Encoded : paddingChar);
headerPayload = encodeBase64Url(header) + '.' + base64Encoded;
algd0500.algorithm = ALGORITHM_SHA256;
keyparam.type = 3;
keyparam.length = %len(%trimr(signKey));
keyparam.key = signKey;
keyparam.format = '0';
// minimum 32 bytes for SHA-256
if (keyparam.length < 32);
keyparam.length = 32;
endif;
clear QUSEC;
sys_calculateHmac(
%addr(headerPayload) :
%len(%trimr(headerPayload)) :
'DATA0100' :
algd0500 :
'ALGD0500' :
keyparam :
'KEYD0200' :
'0' : // crypto
*blank : // crypto dev
hash :
QUSEC
);
memcpy(%addr(tmpHash) : %addr(hash) : 32);
encoded = encodeBase64Url(tmpHash);
encoded = %trimr(encoded : paddingChar);
return %trimr(headerPayload) + UTF8_PERIOD + encoded;
end-proc;
dcl-proc addClaims;
dcl-pi *n like(jwt_token_t) ccsid(*utf8);
pPayload like(jwt_token_t) const ccsid(*utf8);
claims likeds(jwt_claims_t) const;
end-pi;
dcl-s payload like(jwt_token_t) ccsid(*utf8);
dcl-s json pointer;
dcl-s value like(jwt_token_t);
dcl-s uxts int(10);
dcl-s changed ind inz(*off);
payload = %trimr(pPayload) + x'00';
json = json_parseString(%addr(payload : *DATA));
if (%len(claims.issuer) > 0);
value = claims.issuer + x'00';
json_setStr(json : 'iss' : %addr(value : *DATA));
changed = *on;
endif;
if (%len(claims.subject) > 0);
value = claims.subject + x'00';
json_setStr(json : 'sub' : %addr(value : *DATA));
changed = *on;
endif;
if (%len(claims.audience) > 0);
value = claims.audience + x'00';
json_setStr(json : 'aud' : %addr(value : *DATA));
changed = *on;
endif;
if (%len(claims.jwtId) > 0);
value = claims.jwtId + x'00';
json_setStr(json : 'jti' : %addr(value : *DATA));
changed = *on;
endif;
if (claims.expirationTime <> *loval);
uxts = toUnixTimestamp(claims.expirationTime);
json_setInt(json : 'exp' : uxts);
changed = *on;
endif;
if (claims.notBefore <> *loval);
uxts = toUnixTimestamp(claims.notBefore);
json_setInt(json : 'nbf' : uxts);
changed = *on;
endif;
if (claims.issuedAt <> *loval);
uxts = toUnixTimestamp(claims.issuedAt);
json_setInt(json : 'iat' : uxts);
changed = *on;
endif;
if (changed);
payload = json_asJsonText(json);
json_close(json);
return payload;
else;
json_close(json);
return pPayload;
endif;
end-proc;
dcl-proc jwt_isExpired;
dcl-pi *n ind;
pPayload like(jwt_token_t) const ccsid(*utf8);
end-pi;
dcl-s payload like(jwt_token_t);
dcl-s expired ind inz(*off);
dcl-s json pointer;
payload = pPayload;
json = json_parseString(payload);
expired = isExpired(json);
json_close(json);
return expired;
end-proc;
dcl-proc isExpired;
dcl-pi *n ind;
json pointer const;
end-pi;
dcl-s expired ind inz(*off);
dcl-s exp int(20);
dcl-s expTimestamp timestamp;
dcl-s now timestamp;
dcl-s offsetHours int(10);
dcl-s offsetMinutes int(10);
dcl-s offsetSeconds float(8);
now = %timestamp();
sys_getUtcOffset(offsetHours : offsetMinutes : offsetSeconds : *omit);
exp = json_getInt(json : 'exp' : -1);
if (exp >= 0);
expTimestamp = UNIX_EPOCH_START + %seconds(exp + %int(offsetSeconds));
expired = (now >= expTimestamp);
endif;
return expired;
end-proc;
dcl-proc isActive;
dcl-pi *n ind;
json pointer const;
end-pi;
dcl-s active ind inz(*on);
dcl-s nbf int(20);
dcl-s nbfTimestamp timestamp;
dcl-s now timestamp;
dcl-s offsetHours int(10);
dcl-s offsetMinutes int(10);
dcl-s offsetSeconds float(8);
now = %timestamp();
sys_getUtcOffset(offsetHours : offsetMinutes : offsetSeconds : *omit);
nbf = json_getInt(json : 'nbf' : -1);
if (nbf >= 0);
nbfTimestamp = UNIX_EPOCH_START
+ %seconds(nbf + %int(offsetSeconds));
active = (now >= nbfTimestamp);
endif;
return active;
end-proc;
dcl-proc encodeBase64Url;
dcl-pi *n varchar(65530) ccsid(*utf8);
string varchar(65530) const ccsid(*utf8);
end-pi;
dcl-s FROM char(2) inz('+/') ccsid(*utf8);
dcl-s TO char(2) inz('-_') ccsid(*utf8);
dcl-s encoded varchar(65530) ccsid(*utf8);
encoded = base64EncodeStr(string);
encoded = %xlate(FROM : TO : encoded);
return encoded;
end-proc;
dcl-proc decodeBase64Url;
dcl-pi *n varchar(65530) ccsid(*utf8);
string varchar(65530) const ccsid(*utf8);
end-pi;
dcl-s TO char(2) inz('+/') ccsid(*utf8);
dcl-s FROM char(2) inz('-_') ccsid(*utf8);
dcl-s decoded varchar(65530) ccsid(*utf8);
dcl-s value varchar(65530) ccsid(*utf8);
value = %xlate(FROM : TO : string);
decoded = base64DecodeStr(value);
return decoded;
end-proc;
dcl-proc toUnixTimestamp;
dcl-pi *n int(10);
ts timestamp const;
end-pi;
dcl-s offsetHours int(10);
dcl-s offsetMinutes int(10);
dcl-s offsetSeconds float(8);
dcl-s uxts int(10);
sys_getUtcOffset(offsetHours : offsetMinutes : offsetSeconds : *omit);
uxts = %diff(ts : UNIX_EPOCH_START : *SECONDS) - %int(offsetSeconds);
return uxts;
end-proc;