forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.java
More file actions
481 lines (399 loc) · 18 KB
/
Service.java
File metadata and controls
481 lines (399 loc) · 18 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
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
package oiccli;
import com.auth0.jwt.creators.Message;
import com.google.common.base.Strings;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.ws.http.HTTPException;
import oiccli.HTTP.Response;
import oiccli.client_info.ClientInfo;
import oiccli.exceptions.MissingEndpoint;
import oiccli.exceptions.OicCliError;
import oiccli.exceptions.UnsupportedType;
import oiccli.exceptions.ValueError;
import oiccli.exceptions.WrongContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Service {
private final static Logger logger = LoggerFactory.getLogger(Service.class);
private static final List<Integer> successfulCodes =
Arrays.asList(200, 201, 202, 203, 204, 205, 206);
private static final List<String> specialArgs = Arrays.asList("authenticationEndpoint", "algs");
private static final Map<String, Object> attributes = new HashMap<String, Object>() {{
put("version", null);
put("name", "");
put("value", null);
put("port", null);
put("isPortSpecified", false);
put("domain", "");
put("isDomainSpecified", false);
put("domainInitialDot", false);
put("path", "");
put("isPathSpecified", false);
put("isSecure", false);
put("expires", null);
put("shouldDiscard", true);
put("comment", null);
put("commentUrl", null);
put("rest", "");
put("rfc2109", true);
}};
private Message msgType;
private Message responseCls;
private ErrorResponse errorResponse;
private String endpointName;
private boolean isSynchronous = true;
private String request;
private String defaultAuthenticationMethod;
private String httpMethod;
private String bodyType;
private String responseBodyType;
private String endpoint;
private String httpLib;
private KeyJar keyJar;
private String clientAuthenticationMethod;
private List<String> events;
private Map<String, String> defaultRequestArgs;
private List<Object> preConstruct;
private List<String> postConstruct;
private List<String> postParseResponse;
private Map<String,String> conf;
public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod, Map<String,String> conf, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException {
this.httpLib = httpLib;
this.keyJar = keyJar;
this.clientAuthenticationMethod = clientAuthenticationMethod;
this.events = new ArrayList<>();
this.endpoint = "";
this.defaultRequestArgs = new HashMap<>();
if(conf != null) {
this.conf = conf;
List<String> params = Arrays.asList("msgType", "responseCls", "errorMsg", "defaultAuthenticationMethod",
"httpMethod", "bodyType", "responseBodyType");
for(String param : params) {
if(conf.containsKey(param)) {
this.getClass().getField(param).set(this, conf.get("param"));
}
}
} else {
this.conf = new HashMap<>();
}
this.preConstruct = new ArrayList<>();
this.postConstruct = new ArrayList<>();
this.postParseResponse = new ArrayList<>();
}
public Map<String, Object> gatherRequestArgs(ClientInfo clientInfo, Map<String, Object> args) throws NoSuchFieldException {
Map<String,Object> arArgs = new HashMap<>(args);
Set<String> properties = this.msgType.getCParam().keySet();
Field field;
for(String property : properties) {
if(!arArgs.containsKey(property)) {
field = clientInfo.getClass().getField(property);
if(field != null) {
arArgs.put(property, field);
} else {
field = this.conf.getRequestArgs().getProp();
if(field != null) {
arArgs.put(property, field);
} else {
arArgs.put(property, this.defaultRequestArgs.get(property));
}
}
}
}
return arArgs;
}
public static Map<String, Object> updateHttpArgs(Map<String, String> httpArgs, Map<String, Map<String, String>> info) {
Map<String, String> hArgs = info.get("hArgs");
if (hArgs == null) {
hArgs = new HashMap<>();
}
if (httpArgs == null) {
httpArgs = hArgs;
} else {
httpArgs.update(info.get("hArgs"));
}
final String headers = info.get("kwargs").get("headers");
Map<String, String> hMap = new HashMap<String, String>() {{
put("headers", headers);
}};
httpArgs.update(hMap);
info.put("httpArgs", httpArgs);
return info;
}
public Map<String, Object> parseArgs(ClientInfo clientInfo, Map<String, Object> args) throws NoSuchFieldException, IllegalAccessException {
Map<String, Object> arArgs = new HashMap<>(args);
Object value;
for (String property : this.msgType.cParam.keySet()) {
if (!arArgs.containsKey(property)) {
value = clientInfo.getClass().getField(property).get(property);
if (value != null) {
arArgs.put(property, value);
} else {
arArgs.put(property, this.defaultRequestArgs.get(property));
}
}
}
return arArgs;
}
public List<Map<String, Object>> doPreConstruct(ClientInfo clientInfo, Map<String, Object> requestArgs, Map<String, String> args) {
Map<String, Object> postArgs = new HashMap<>();
for (Object method : this.preConstruct) {
/*request_args, _post_args = meth(cli_info, request_args, **kwargs)
post_args.update(_post_args)*/
}
return Arrays.asList(requestArgs, postArgs);
}
public Message doPostConstruct(ClientInfo clientInfo, Map<String, Object> requestArgs, Map<String, Object> postArgs) {
}
public void doPostParseResponse(Message response, ClientInfo clientInfo, String state, Map<String, KeyJar> args) {
/*
for meth in self.post_parse_response:
meth(resp, cli_info, state=state, **kwargs)
*/
}
public Message constructMessage(ClientInfo clientInfo, Map<String, Object> requestArgs, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException {
if (requestArgs == null) {
requestArgs = new HashMap<>();
}
List<Map<String, Object>> returnedArgs = this.doPreConstruct(clientInfo, requestArgs, args);
if (!this.msgType.getCParam().containsKey("state")) {
args.remove("state");
}
Map<String, Object> argsParam = this.gatherRequestArgs(clientInfo, requestArgs);
this.msgType(argsParam);
return this.doPostConstruct(clientInfo, requestArgs, returnedArgs.get(1));
}
public String getEndpoint(Map<String, String> args) throws MissingEndpoint {
String uri = args.get("endpoint");
if (uri != null) {
args.remove("endpoint");
} else {
if (!Strings.isNullOrEmpty(endpoint)) {
uri = this.endpoint;
} else {
throw new MissingEndpoint("No endpoint specified");
}
}
return uri;
}
public Map<String, Object> uriAndBody(Message cis, String method, Map<String, String> args) throws UnsupportedEncodingException, UnsupportedType, MissingEndpoint {
String uri = this.getEndpoint(args);
Map<String, Object> response = Util.getOrPost(uri, method, cis, args);
response.put("cis", cis);
Map<String, Object> hMap = new HashMap<>();
hMap.put("headers", response.get("headers"));
response.put("hArgs", hMap);
return response;
}
public Map<String, String> initAuthenticationMethod(Message cis, ClientInfo clientInfo, String authenticationMethod,
Map<String, Object> httpArgs, Map<String, String> args) {
return initAuthenticationMethod(cis, clientInfo, authenticationMethod, httpArgs, null, args);
}
public Map<String, String> initAuthenticationMethod(Message cis, ClientInfo clientInfo, String authenticationMethod,
Map<String, Object> requestArgs, Map<String, String> httpArgs, Map<String, String> args) {
if (httpArgs == null) {
httpArgs = new HashMap<>();
}
if (!Strings.isNullOrEmpty(authenticationMethod)) {
//return this.client_authn_method[authn_method]().construct(
// cis, cli_info, request_args, http_args, **kwargs);
} else {
return httpArgs;
}
}
public Map<String, Object> requestInfo(ClientInfo clientInfo, String method, Map<String, Object> requestArgs,
String bodyType, String authenticationMethod, boolean lax, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException, MissingEndpoint, UnsupportedEncodingException, UnsupportedType {
if (method == null) {
method = this.httpMethod;
}
if (requestArgs == null) {
requestArgs = new HashMap<>();
}
Map<String, String> hMap = new HashMap<>();
for (String key : args.keySet()) {
if (args.get(key) != null && !specialArgs.contains(key)) {
hMap.put(key, args.get(key));
}
}
Message cis = this.constructMessage(clientInfo, requestArgs, args);
if (this.events != null) {
this.events.store("Protocol request", cis);
}
cis.setLax(lax);
Map<String, String> hArg = new HashMap<>();
if (!Strings.isNullOrEmpty(authenticationMethod)) {
hArg = this.initAuthenticationMethod(cis, clientInfo, authenticationMethod, requestArgs, args);
}
if (hArg != null) {
if (args.containsKey("headers")) {
args.get("headers").update(hArg.get("headers"));
} else {
args.put("headers", hArg.get("headers"));
}
}
if (bodyType.equals("json")) {
args.put("contentType", "application/json");
}
return this.uriAndBody(cis, method, args);
}
public Map<String, Object> updateHttpArgs(Map<String, String> httpArgs, Map<String, Object> info) {
Map<String, String> hArgs = info.get("hArgs");
if (hArgs == null) {
hArgs = new HashMap<>();
}
if (httpArgs == null) {
httpArgs = hArgs;
} else {
httpArgs.update(info.get("h_args"));
}
info.put("httpArgs", httpArgs);
return info;
}
public Map<String, Object> doRequestInit(ClientInfo clientInfo, String bodyType, String method, String authenticationMethod,
Map<String, Object> requestArgs, Map<String, String> httpArgs, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException, MissingEndpoint, UnsupportedEncodingException, UnsupportedType {
if (Strings.isNullOrEmpty(method)) {
method = this.httpMethod;
}
if (Strings.isNullOrEmpty(authenticationMethod)) {
authenticationMethod = this.defaultAuthenticationMethod;
}
if (Strings.isNullOrEmpty(bodyType)) {
bodyType = this.bodyType;
}
Map<String, Object> info = this.requestInfo(clientInfo, method, requestArgs, bodyType, authenticationMethod, false, args);
return this.updateHttpArgs(httpArgs, info);
}
public String getUrlInfo(String info) throws URISyntaxException {
String query = null;
String fragment = null;
if (info.contains("?") || info.contains("#")) {
URI uri = new URI(info);
query = uri.getQuery();
fragment = uri.getFragment();
}
if (!Strings.isNullOrEmpty(query)) {
return query;
} else {
return fragment;
}
}
public Message parseResponse(String info, ClientInfo clientInfo, String sFormat, String state,
Map<String, Object> args) throws URISyntaxException, OicCliError {
logger.debug("Response format: " + sFormat);
if (sFormat.equals("urlencoded")) {
info = this.getUrlInfo(info);
}
if (this.events != null) {
this.events.store("Response", info);
}
logger.debug("Response cls: " + this.responseCls.getClass());
Message response = this.responseCls.deserialize(info, sFormat, args);
String message = "Initial response parsing => \"{}\"";
logger.debug(message.format(response.toDict()));
if (this.events != null) {
this.events.store("Protocol Response", response);
}
List<ErrorResponse> errorMsgs;
if (response.containsKey("error") && !(response instanceof ErrorResponse)) {
response = null;
errorMsgs = Arrays.asList(this.errorResponse);
for (ErrorResponse errorMsg : errorMsgs) {
response = errorMsg.deserialize(info, sFormat);
response.verify();
break;
}
logger.debug("Error response: " + response);
} else {
args.put("clientId", clientInfo.getClientId());
args.put("issuer", clientInfo.getIssuer());
if (!args.containsKey("key") && !args.containsKey("keyJar")) {
args.put("keyJar", keyJar);
}
logger.debug("Verify response with " + args);
boolean isVerificationSuccessful = response.verify(args);
if (!isVerificationSuccessful) {
logger.error("Verification of the response failed");
throw new OicCliError("Verification of the response failed");
}
if (response.getType().equals("AuthorizationResponse") && response.getScope() == null) {
response.setScope(args.get("scope"));
}
}
if (response == null) {
this.doPostParseResponse(response, clientInfo, state, args);
}
return response;
}
public ErrorResponse parseErrorMessage(Response response, String bodyType) {
String bodyTypeResult;
if (bodyType.equals("txt")) {
bodyTypeResult = "urlencoded";
} else {
bodyTypeResult = bodyType;
}
ErrorResponse errorResponse = this.errorResponse.deserialize(response.getText(), bodyTypeResult);
errorResponse.verify();
return errorResponse;
}
public String getValueType(FakeResponse response, String bodyType) throws WrongContentType, ValueError {
if (!Strings.isNullOrEmpty(bodyType)) {
return Util.verifyHeader(response, bodyType);
} else {
return "urlencoded";
}
}
public static ErrorResponse parseRequestResponse(ErrorResponse response, ClientInfo clientInfo, String responseBodyType,
String state, Map<String, Object> args) {
int statusCode = response.getStatusCode();
if (successfulCodes.contains(statusCode)) {
logger.debug("Response body type " + responseBodyType);
String valueType = this.getValueType(response, responseBodyType);
logger.debug("Successful response " + response.getText());
return this.parseResponse(response.getText(), clientInfo, valueType, state, args);
} else if (statusCode == 302 || statusCode == 303) {
return response;
} else if (statusCode == 500) {
logger.error("(" + statusCode + ")" + response.getText());
throw new ParseException("ERROR: Something went wrong " + response.getText());
} else if (statusCode >= 400 && statusCode < 500) {
logger.error("Error response (" + statusCode + "): " + response.getText());
String valueType = this.getValueType(response, responseBodyType);
return this.parseErrorMessage(response, valueType);
} else {
logger.error("Error response (" + statusCode + "):" + response.getText());
throw new HTTPException("HTTP ERROR: " + response.getText() + "[" + statusCode + "]" + " on " + response.getUrl());
}
}
public static ErrorResponse serviceRequest(String url, String method, String body, String responseBodyType, Map<String, String> httpArgs,
ClientInfo clientInfo, Map<String, Object> args) {
if (httpArgs == null) {
httpArgs = new HashMap<>();
}
logger.debug("Doing request with: URL: " + url + ", method: " + method + ", data: " + body + ", https_args: " + httpArgs);
Response response = this.httpLib(url, method, body, httpArgs);
if (!args.containsKey("keyjar")) {
args.put("keyjar", this.keyJar);
}
if (responseBodyType == null) {
responseBodyType = this.responseBodyType;
}
return parseRequestResponse(response, clientInfo, responseBodyType, "", args);
}
public static ErrorResponse serviceRequest(String url, Map<String, Object> args) {
return serviceRequest(url, "GET", null, "", null, null, args);
}
public static ErrorResponse serviceRequest(String url, String method, ClientInfo clientInfo, Map<String, Object> args) {
return serviceRequest(url, "GET", null, "", null, null, args);
}
public static ErrorResponse serviceRequest(String url, String method, ClientInfo clientInfo) {
return serviceRequest(url, "GET", null, "", null, null, null);
}
}