/
InformazioniToken.java
553 lines (475 loc) · 15.1 KB
/
InformazioniToken.java
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* GovWay - A customizable API Gateway
* https://govway.org
*
* Copyright (c) 2005-2024 Link.it srl (https://link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openspcoop2.pdd.core.token;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openspcoop2.pdd.core.token.attribute_authority.InformazioniAttributi;
import org.openspcoop2.pdd.core.token.parser.ITokenParser;
import org.openspcoop2.utils.UtilsException;
import org.openspcoop2.utils.json.JSONUtils;
import com.fasterxml.jackson.databind.JsonNode;
/**
* InformazioniToken
*
* @author Poli Andrea (poli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*/
public class InformazioniToken extends org.openspcoop2.utils.beans.BaseBean implements Serializable , Cloneable {
/**
*
*/
private static final long serialVersionUID = 1L;
public InformazioniToken() {} // per serializzatore
public InformazioniToken(SorgenteInformazioniToken sourceType, String rawResponse, ITokenParser tokenParser) throws UtilsException {
this(null,sourceType, rawResponse,tokenParser);
}
public InformazioniToken(Integer httpResponseCode, SorgenteInformazioniToken sourceType, String rawResponse, ITokenParser tokenParser) throws UtilsException {
this.rawResponse = rawResponse;
this.sourceType = sourceType;
JSONUtils jsonUtils = JSONUtils.getInstance();
if(jsonUtils.isJson(this.rawResponse)) {
JsonNode root = jsonUtils.getAsNode(this.rawResponse);
Map<String, Serializable> readClaims = jsonUtils.convertToSimpleMap(root);
if(readClaims!=null && readClaims.size()>0) {
this.claims.putAll(readClaims);
}
}
tokenParser.init(this.rawResponse, this.claims);
if(httpResponseCode!=null) {
tokenParser.checkHttpTransaction(httpResponseCode);
}
this.valid = tokenParser.isValid();
this.iss = tokenParser.getIssuer();
this.sub = tokenParser.getSubject();
this.username = tokenParser.getUsername();
List<String> a = tokenParser.getAudience();
if(a!=null && !a.isEmpty()) {
this.aud = new ArrayList<>();
this.aud.addAll(a);
}
this.exp = tokenParser.getExpired();
this.iat = tokenParser.getIssuedAt();
this.nbf = tokenParser.getNotToBeUsedBefore();
this.clientId = tokenParser.getClientId();
this.jti = tokenParser.getJWTIdentifier();
List<String> r = tokenParser.getRoles();
if(r!=null && !r.isEmpty()) {
this.roles = new ArrayList<>();
this.roles.addAll(r);
}
List<String> s = tokenParser.getScopes();
if(s!=null && !s.isEmpty()) {
this.scopes = new ArrayList<>();
this.scopes.addAll(s);
}
if(tokenParser.getUserInfoParser()!=null) {
this.userInfo = new InformazioniTokenUserInfo(this, tokenParser.getUserInfoParser());
}
}
public InformazioniToken(String errorDetails, SorgenteInformazioniToken sourceType, String token) {
this(errorDetails,null,null,sourceType, token);
}
public InformazioniToken(String errorDetails, Integer httpResponseCode, byte[] rawResponse, SorgenteInformazioniToken sourceType, String token) {
this.valid = false;
this.token = token;
this.claims = null;
this.sourceType = sourceType;
if(httpResponseCode!=null) {
this.httpResponseCode = httpResponseCode+"";
}
if(rawResponse!=null) {
this.rawResponse = new String(rawResponse);
}
this.errorDetails = errorDetails;
}
@SuppressWarnings("unchecked")
public InformazioniToken(boolean saveSourceTokenInfo, InformazioniToken ... informazioniTokens ) throws UtilsException {
if(informazioniTokens!=null && informazioniTokens.length>0) {
if(saveSourceTokenInfo) {
this.sourcesTokenInfo = new HashMap<>();
for (int i = 0; i < informazioniTokens.length; i++) {
this.sourcesTokenInfo.put(informazioniTokens[i].getSourceType(),
informazioniTokens[i].getRawResponse());
}
}
else {
this.sourceTypes = new ArrayList<>();
for (int i = 0; i < informazioniTokens.length; i++) {
this.sourceTypes.add(informazioniTokens[i].getSourceType());
}
}
for (int i = 0; i < informazioniTokens.length; i++) {
if(informazioniTokens[i].getClaims().size()>0) {
this.claims.putAll(informazioniTokens[i].getClaims());
}
}
Object issV = getValue("iss", informazioniTokens);
if(issV instanceof String) {
this.iss = (String) issV;
}
Object subV = getValue("sub", informazioniTokens);
if(subV instanceof String) {
this.sub = (String) subV;
}
Object usernameV = getValue("username", informazioniTokens);
if(usernameV instanceof String) {
this.username = (String) usernameV;
}
Object audV = getValue("aud", informazioniTokens);
if(audV instanceof List) {
List<String> l = (List<String>) audV;
if(!l.isEmpty()) {
if(this.aud == null) {
this.aud = new ArrayList<>();
}
this.aud.addAll(l);
}
}
Object expV = getValue("exp", informazioniTokens);
if(expV instanceof Date) {
this.exp = (Date) expV;
}
Object iatV = getValue("iat", informazioniTokens);
if(iatV instanceof Date) {
this.iat = (Date) iatV;
}
Object nbfV = getValue("nbf", informazioniTokens);
if(nbfV instanceof Date) {
this.nbf = (Date) nbfV;
}
Object clientIdV = getValue("clientId", informazioniTokens);
if(clientIdV instanceof String) {
this.clientId = (String) clientIdV;
}
Object jtiV = getValue("jti", informazioniTokens);
if(jtiV instanceof String) {
this.jti = (String) jtiV;
}
Object rolesV = getValue("roles", informazioniTokens);
if(rolesV instanceof List) {
List<String> l = (List<String>) rolesV;
if(!l.isEmpty()) {
if(this.roles == null) {
this.roles = new ArrayList<>();
}
this.roles.addAll(l);
}
}
Object scopesV = getValue("scopes", informazioniTokens);
if(scopesV instanceof List) {
List<String> l = (List<String>) scopesV;
if(!l.isEmpty()) {
if(this.scopes == null) {
this.scopes = new ArrayList<>();
}
this.scopes.addAll(l);
}
}
List<InformazioniTokenUserInfo> listUserInfo = new ArrayList<>();
for (int i = 0; i < informazioniTokens.length; i++) {
InformazioniTokenUserInfo userInfoV = informazioniTokens[i].getUserInfo();
if(userInfoV!=null) {
listUserInfo.add(userInfoV);
}
}
if(listUserInfo.size()==1) {
this.userInfo = listUserInfo.get(0);
}
else {
this.userInfo = new InformazioniTokenUserInfo(listUserInfo.toArray(new InformazioniTokenUserInfo[1]));
}
}
}
private static Object getValue(String field, InformazioniToken ... informazioniTokens) throws UtilsException {
Object tmp = null;
List<Object> listTmp = new ArrayList<>();
String getMethodName = "get"+((field.charAt(0)+"").toUpperCase())+field.substring(1);
Method getMethod = null;
try {
getMethod = InformazioniToken.class.getMethod(getMethodName);
}catch(Exception e) {
throw new UtilsException(e.getMessage(),e);
}
// La fusione avviene per precedenza dall'ultimo fino al primo (a meno che non sia una lista)
tmp = getValue(getMethod, listTmp, informazioniTokens);
if(!listTmp.isEmpty()) {
return listTmp;
}
else {
return tmp;
}
}
private static Object getValue(Method getMethod, List<Object> listTmp, InformazioniToken ... informazioniTokens) throws UtilsException {
Object tmp = null;
for (int i = 0; i < informazioniTokens.length; i++) {
InformazioniToken infoToken = informazioniTokens[i];
Object o = null;
try {
o = getMethod.invoke(infoToken);
}catch(Exception e) {
throw new UtilsException(e.getMessage(),e);
}
if(o!=null) {
tmp = getValue(o, listTmp);
}
}
return tmp;
}
private static Object getValue(Object o, List<Object> listTmp) {
Object tmp = null;
if(o instanceof List<?>) {
List<?> list = (List<?>) o;
if(!list.isEmpty()) {
for (Object object : list) {
if(!listTmp.contains(object)) {
listTmp.add(object);
}
}
}
}
else {
tmp = o;
}
return tmp;
}
// NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
private TipoInformazioni type = TipoInformazioni.validated_token;
// Indicazione se il token e' valido
private boolean valid;
// String representing the issuer of this token, as defined in JWT [RFC7519].
private String iss;
// Subject of the token, as defined in JWT [RFC7519].
// Usually a machine-readable identifier of the resource owner who authorized this token.
private String sub;
// Human-readable identifier for the resource owner who authorized this token.
private String username;
// Service-specific string identifier or list of string identifiers representing the intended audience for this token,
// as defined in JWT [RFC7519].
private List<String> aud;
// Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
// indicating when this token will expire, as defined in JWT [RFC7519].
private Date exp;
// Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
// indicating when this token was originally issued, as defined in JWT [RFC7519].
private Date iat;
// Integer timestamp, measured in the number of seconds since January 1 1970 UTC,
// indicating when this token is not to be used before, as defined in JWT [RFC7519].
private Date nbf;
// Client identifier for the OAuth 2.0 client that requested this token.
// Per un ID_TOKEN, If present, it MUST contain the OAuth 2.0 Client ID of this party.
private String clientId; // in oidc e' azp
// The "jti" (JWT ID) claim provides a unique identifier for the JWT.
private String jti;
// Ruoli di un utente
private List<String> roles;
// Scopes
private List<String> scopes;
// UserInfo
private InformazioniTokenUserInfo userInfo;
// Claims
private Map<String,Serializable> claims = new HashMap<>();
// NOTA: l'ordine stabilisce come viene serializzato nell'oggetto json
// RawResponse
private String rawResponse;
// Token (nel caso di errori)
private String token;
// HttpCode (nel caso di errori)
private String httpResponseCode;
// Failed
private String errorDetails;
// SorgenteInformazioniToken
private SorgenteInformazioniToken sourceType;
// Multiple Source
private List<SorgenteInformazioniToken> sourceTypes = null;
private Map<SorgenteInformazioniToken,String> sourcesTokenInfo = null;
// DynamicDiscovery (ulteriore informazione valorizzata con la funzionalità specifica)
private DynamicDiscovery dynamicDiscovery;
// Attributes Authority (ulteriore informazione valorizzata con la funzionalità specifica)
private InformazioniAttributi aa;
// InformazioniNegoziazioneToken (ulteriore informazione valorizzata con la funzionalità specifica)
private InformazioniNegoziazioneToken retrievedToken;
// InformazioniPDND (ulteriore informazione valorizzata con la funzionalità specifica)
private Map<String,Serializable> pdnd = null;
public TipoInformazioni getType() {
return this.type;
}
public void setType(TipoInformazioni type) {
this.type = type;
}
public boolean isValid() {
return this.valid;
}
public boolean getValid() { // clone
return this.valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public String getIss() {
return this.iss;
}
public void setIss(String iss) {
this.iss = iss;
}
public String getSub() {
return this.sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public List<String> getAud() {
return this.aud;
}
public void setAud(List<String> aud) {
this.aud = aud;
}
public Date getExp() {
return this.exp;
}
public void setExp(Date exp) {
this.exp = exp;
}
public Date getIat() {
return this.iat;
}
public void setIat(Date iat) {
this.iat = iat;
}
public Date getNbf() {
return this.nbf;
}
public void setNbf(Date nbf) {
this.nbf = nbf;
}
public String getClientId() {
return this.clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getJti() {
return this.jti;
}
public void setJti(String jti) {
this.jti = jti;
}
public List<String> getRoles() {
return this.roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
public List<String> getScopes() {
return this.scopes;
}
public void setScopes(List<String> scopes) {
this.scopes = scopes;
}
public InformazioniTokenUserInfo getUserInfo() {
return this.userInfo;
}
public void setUserInfo(InformazioniTokenUserInfo userInfo) {
this.userInfo = userInfo;
}
public Map<String, Serializable> getClaims() {
return this.claims;
}
public void setClaims(Map<String, Serializable> claims) {
this.claims = claims;
}
public String getRawResponse() {
return this.rawResponse;
}
public void setRawResponse(String rawResponse) {
this.rawResponse = rawResponse;
}
public String getToken() {
return this.token;
}
public void setToken(String token) {
this.token = token;
}
public String getHttpResponseCode() {
return this.httpResponseCode;
}
public void setHttpResponseCode(String httpResponseCode) {
this.httpResponseCode = httpResponseCode;
}
public String getErrorDetails() {
return this.errorDetails;
}
public void setErrorDetails(String errorDetails) {
this.errorDetails = errorDetails;
}
public SorgenteInformazioniToken getSourceType() {
return this.sourceType;
}
public void setSourceType(SorgenteInformazioniToken sourceType) {
this.sourceType = sourceType;
}
public Map<SorgenteInformazioniToken, String> getSourcesTokenInfo() {
return this.sourcesTokenInfo;
}
public void setSourcesTokenInfo(Map<SorgenteInformazioniToken, String> sourcesTokenInfo) {
this.sourcesTokenInfo = sourcesTokenInfo;
}
public List<SorgenteInformazioniToken> getSourceTypes() {
return this.sourceTypes;
}
public void setSourceTypes(List<SorgenteInformazioniToken> sourceTypes) {
this.sourceTypes = sourceTypes;
}
public DynamicDiscovery getDynamicDiscovery() {
return this.dynamicDiscovery;
}
public void setDynamicDiscovery(DynamicDiscovery dynamicDiscovery) {
this.dynamicDiscovery = dynamicDiscovery;
}
public InformazioniAttributi getAa() {
return this.aa;
}
public void setAa(InformazioniAttributi aa) {
this.aa = aa;
}
public InformazioniNegoziazioneToken getRetrievedToken() {
return this.retrievedToken;
}
public void setRetrievedToken(InformazioniNegoziazioneToken retrievedToken) {
this.retrievedToken = retrievedToken;
}
public Map<String, Serializable> getPdnd() {
return this.pdnd;
}
public void setPdnd(Map<String, Serializable> pdnd) {
this.pdnd = pdnd;
}
}