Skip to content

Commit 0bd3d7d

Browse files
committed
Service
1 parent 451ecaf commit 0bd3d7d

3 files changed

Lines changed: 164 additions & 10 deletions

File tree

lib/src/main/java/com/auth0/jwt/oiccli/Service.java

Lines changed: 156 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import com.auth0.jwt.oiccli.Utils.ClientInfo;
55
import com.auth0.jwt.oiccli.exceptions.HTTPError;
66
import com.auth0.jwt.oiccli.exceptions.MissingEndpoint;
7+
import com.auth0.jwt.oiccli.exceptions.OiccliError;
78
import com.auth0.jwt.oiccli.exceptions.UnsupportedType;
89
import com.auth0.jwt.oiccli.exceptions.ValueError;
910
import com.auth0.jwt.oiccli.exceptions.WrongContentType;
1011
import com.auth0.jwt.oiccli.responses.ErrorResponse;
1112
import com.auth0.jwt.oiccli.responses.Response;
13+
import com.auth0.jwt.oiccli.tuples.Tuple;
1214
import com.auth0.jwt.oiccli.util.FakeResponse;
1315
import com.auth0.jwt.oiccli.util.Util;
1416
import com.google.common.base.Strings;
@@ -28,7 +30,7 @@ public class Service {
2830
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(Service.class);
2931
private static final List<Integer> successfulCodes =
3032
Arrays.asList(200, 201, 202, 203, 204, 205, 206);
31-
private static final List<String> specialArgs = Arrays.asList("authenticationEndpoint", "algs");
33+
private static final List<String> SPECIAL_ARGS = Arrays.asList("authenticationEndpoint", "algs");
3234
public Message msgType;
3335
public Message responseCls;
3436
public ErrorResponse errorMessage;
@@ -76,8 +78,29 @@ public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod,
7678
this.postParseResponse = new ArrayList<>();
7779
}
7880

79-
public void gatherRequestArgs() {
80-
throw new UnsupportedOperationException();
81+
public Map<String, String> gatherRequestArgs(ClientInfo clientInfo, Map<String,String> args) throws NoSuchFieldException, IllegalAccessException {
82+
83+
Map<String,String> arArgs = new HashMap<>(args);
84+
85+
String value;
86+
String requestArgsValue;
87+
for(String property : this.msgType.getCParam().keySet()) {
88+
if(!arArgs.containsKey(property)) {
89+
value = (String) clientInfo.getClass().getField(property).get(this);
90+
if(!Strings.isNullOrEmpty(value)) {
91+
arArgs.put(property, value);
92+
} else {
93+
requestArgsValue = this.conf.get("requestArgs").get(property);
94+
if(!Strings.isNullOrEmpty(requestArgsValue)) {
95+
arArgs.put(property, requestArgsValue);
96+
} else {
97+
arArgs.put(property, this.defaultRequestArgs.get(property));
98+
}
99+
}
100+
}
101+
}
102+
103+
return arArgs;
81104
}
82105

83106
public void doPreConstruct() {
@@ -179,8 +202,48 @@ public Map<String, Map<String,String>> doRequestInit(ClientInfo clientInfo, Stri
179202
return this.updateHttpArgs(httpArgs, info);
180203
}
181204

182-
private Map<String,Map<String,String>> requestInfo(ClientInfo clientInfo, String method, Map<String, Object> requestArgs, String bodyType, String authenticationMethod, boolean b, Map<String, String> args) {
183-
throw new UnsupportedOperationException();
205+
private Map<String,Map<String,String>> requestInfo(ClientInfo clientInfo, String method, Map<String,String> requestArgs, String bodyType, String authenticationMethod, boolean lax, Map<String, String> args) {
206+
if(Strings.isNullOrEmpty(method)) {
207+
method = this.httpMethod;
208+
}
209+
210+
if(requestArgs == null) {
211+
requestArgs = new HashMap<>();
212+
}
213+
214+
Map<String,String> newArgs = new HashMap<>();
215+
for(String key : args.keySet()) {
216+
if(!(SPECIAL_ARGS.contains(key) && SPECIAL_ARGS.contains(args.get(key)))) {
217+
newArgs.put(key, args.get(key));
218+
}
219+
}
220+
221+
DummyMessage request = this.construct(clientInfo, requestArgs, newArgs);
222+
223+
if(this.events != null && !this.events.isEmpty()) {
224+
this.events.add("Protocol request", request);
225+
}
226+
227+
request.setLax(lax);
228+
Map<String,String> hArgs = new HashMap<>();
229+
230+
if(!Strings.isNullOrEmpty(authenticationMethod)) {
231+
hArgs = this.initAuthenticationMethod(request, clientInfo, authenticationMethod, args);
232+
}
233+
234+
if(hArgs != null) {
235+
if(hArgs.keySet().contains("headers")) {
236+
args.get("headers").update(hArgs.get("headers"));
237+
} else {
238+
args.put("headers", hArgs.get("headers"));
239+
}
240+
}
241+
242+
if(bodyType.equals("json")) {
243+
args.put("contentType", Util.JSON_ENCODED);
244+
}
245+
246+
return this.uriAndBody(request, method, args);
184247
}
185248

186249
public String getUrlInfo(String info) throws URISyntaxException {
@@ -286,8 +349,17 @@ public String getConfigurationAttribute(String attribute) {
286349
return getConfigurationAttribute(attribute, null);
287350
}
288351

289-
public void buildServices() {
290-
throw new UnsupportedOperationException();
352+
public Service buildServices(List<Tuple> services, Function serviceFactor, String httpLib, KeyJar keyJar, String clientAuthenticationMethod) throws NoSuchFieldException, IllegalAccessException {
353+
Map<String,Service> hMap = new HashMap<>();
354+
Service service = null;
355+
for(Tuple tuple : services) {
356+
service = serviceFactory(tuple.getA(), httpLib, keyJar, clientAuthenticationMethod, tuple.getB());
357+
hMap.put(service.request, service);
358+
}
359+
360+
hMap.put("any", new Service(httpLib, keyJar, clientAuthenticationMethod, null));
361+
362+
return service;
291363
}
292364

293365
public Response serviceRequest(String url, Map<String, Object> args) throws ParseException, ValueError, WrongContentType {
@@ -302,8 +374,83 @@ public Response serviceRequest(String url, String method, ClientInfo clientInfo)
302374
return serviceRequest(url, "GET", null, "", null, null, null);
303375
}
304376

305-
private static FakeResponse parseResponse(Object text, ClientInfo clientInfo, String valueType, String state, Map<String, Object> args) {
306-
throw new UnsupportedOperationException();
377+
private Response parseResponse(String info, ClientInfo clientInfo, String sFormat, String state, Map<String, Object> args) throws URISyntaxException, ValueError, OiccliError {
378+
if(Strings.isNullOrEmpty(sFormat)) {
379+
sFormat = this.responseBodyType;
380+
}
381+
382+
logger.debug("response format: " + sFormat);
383+
384+
if(sFormat.equals("urlencoded")) {
385+
info = this.getUrlInfo(info);
386+
}
387+
388+
if(this.events != null && !this.events.isEmpty()) {
389+
this.events.add("Response", info);
390+
}
391+
392+
logger.debug("response cls: " + this.responseCls.toString());
393+
394+
Response response = this.responseCls.deserialize(info, sFormat, args);
395+
396+
if(this.events != null && !this.events.isEmpty()) {
397+
this.events.add("Protocol Response", response);
398+
}
399+
400+
List<ErrorResponse> errorMessages = null;
401+
if(response.getError() != null && !(response instanceof ErrorResponse)) {
402+
response = null;
403+
errorMessages = Arrays.asList(this.errorMessage);
404+
405+
/*
406+
if ErrorResponse not in errmsgs:
407+
# Allow unspecified error response
408+
errmsgs.append(ErrorResponse)
409+
*/
410+
411+
for(ErrorResponse errorResponse : errorMessages) {
412+
response = errorResponse.deserialize(info, sFormat);
413+
response.verify();
414+
break;
415+
}
416+
417+
if(response == null) {
418+
logger.debug("Could not map into an error message");
419+
throw new ValueError("No error message: " + info);
420+
}
421+
422+
logger.debug("Error response: " + response);
423+
} else {
424+
args.put("clientId", clientInfo.getClientId());
425+
args.put("issuer", clientInfo.getIssuer());
426+
427+
if(!args.containsKey("key") && !args.containsKey("keyjar")) {
428+
args.put("keyjar", this.keyJar);
429+
}
430+
431+
args.update(this.conf.get("verify"));
432+
433+
logger.debug("Verify response with " + args);
434+
435+
boolean shouldVerify = response.verify(args);
436+
437+
if(!shouldVerify) {
438+
logger.error("Verification of the response failed");
439+
throw new OiccliError("Verification of the response failed");
440+
}
441+
442+
if(response != null && response.getType().equals("AuthorizationResponse") && response.getScope() == null) {
443+
response.setScope(args.get("scope"));
444+
} else {
445+
throw new ResponseError("Missing or faulty response");
446+
}
447+
448+
if(!(response instanceof ErrorResponse)) {
449+
this.doPostParseResponse(response, clientInfo, state);
450+
}
451+
}
452+
453+
return response;
307454
}
308455

309456
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.auth0.jwt.oiccli.exceptions;
2+
3+
public class OiccliError extends Exception {
4+
public OiccliError(String message) {
5+
super(message);
6+
}
7+
}

lib/src/main/java/com/auth0/jwt/oiccli/util/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class Util {
2828

2929
private static final String URL_ENCODED = "application/x-www-form-urlencoded";
30-
private static final String JSON_ENCODED = "application/json";
30+
public static final String JSON_ENCODED = "application/json";
3131
private static final String JRD_JSON = "application/jrd+json";
3232
private static final String JWT_ENCODED = "application/jwt";
3333
private static final String PLAIN_TEXT = "text/plain";

0 commit comments

Comments
 (0)