Skip to content

Commit 8e538b9

Browse files
committed
Oicclient cleanup
1 parent 8388661 commit 8e538b9

21 files changed

Lines changed: 345 additions & 213 deletions

lib/src/main/java/com/auth0/jwt/creators/Message.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
package com.auth0.jwt.creators;
2121

22+
import com.fasterxml.jackson.core.JsonProcessingException;
2223
import com.fasterxml.jackson.core.type.TypeReference;
2324
import com.fasterxml.jackson.databind.ObjectMapper;
24-
import com.google.gson.Gson;
25-
25+
import com.fasterxml.jackson.databind.ObjectWriter;
2626
import java.io.IOException;
2727
import java.io.UnsupportedEncodingException;
2828
import java.net.URLDecoder;
@@ -40,8 +40,9 @@ public String toUrlDecoded(String urlEncoded) throws UnsupportedEncodingExceptio
4040
return URLDecoder.decode(urlEncoded, "UTF-8");
4141
}
4242

43-
public String toJSON(HashMap<String,Object> hashMap) {
44-
return new Gson().toJson(hashMap);
43+
public String toJSON() throws JsonProcessingException {
44+
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
45+
return ow.writeValueAsString(this);
4546
}
4647

4748
public HashMap<String,Object> fromJSON(String json) throws IOException {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package oiccli;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class FakeResponse {
7+
8+
private Map<String,String> headers;
9+
private String text;
10+
11+
public FakeResponse(String header) {
12+
Map<String,String> headersTemp = new HashMap<>();
13+
headers.put("contentType", header);
14+
this.headers = headersTemp;
15+
this.text = "TEST_RESPONSE";
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package oiccli.HTTP;
2+
3+
public class CookieJar {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package oiccli.HTTP;
2+
3+
public class FileCookieJar {
4+
}

lib/src/main/java/oiccli/HTTP/Http.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import oiccli.exceptions.ValueError;
56

67
public class Http {
78

@@ -12,13 +13,13 @@ public class Http {
1213
private Object events;
1314
private Object reqCallback;
1415

15-
public Http(String caCerts, boolean shouldVerifySSL, KeyJar keyjar, String clientCert) {
16+
public Http(String caCerts, boolean shouldVerifySSL, KeyJar keyjar, String clientCert) throws ValueError {
1617
this.caCerts = caCerts;
1718
this.requestArgs = new HashMap() {{
1819
put("allowRedirects", false);
1920
}};
2021
this.keyJar = keyjar; //or KeyJar(verify_ssl=verify_ssl)
21-
//this.cookiejar = FileCookieJar()
22+
this.cookiejar = FileCookieJar();
2223
if (caCerts != null) {
2324
if (!shouldVerifySSL) {
2425
throw new ValueError("conflict: ca_certs defined, but verify_ssl is False");

lib/src/main/java/oiccli/Service.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.creators.Message;
44
import com.google.common.base.Strings;
55
import java.io.UnsupportedEncodingException;
6+
import java.lang.reflect.Field;
67
import java.net.URI;
78
import java.net.URISyntaxException;
89
import java.text.ParseException;
@@ -11,6 +12,7 @@
1112
import java.util.HashMap;
1213
import java.util.List;
1314
import java.util.Map;
15+
import java.util.Set;
1416
import javax.xml.ws.http.HTTPException;
1517
import oiccli.HTTP.Response;
1618
import oiccli.client_info.ClientInfo;
@@ -67,33 +69,60 @@ public class Service {
6769
private List<Object> preConstruct;
6870
private List<String> postConstruct;
6971
private List<String> postParseResponse;
72+
private Map<String,String> conf;
7073

7174

72-
public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod, Map<String, String> args) {
75+
public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod, Map<String,String> conf, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException {
7376
this.httpLib = httpLib;
7477
this.keyJar = keyJar;
7578
this.clientAuthenticationMethod = clientAuthenticationMethod;
7679
this.events = new ArrayList<>();
7780
this.endpoint = "";
7881
this.defaultRequestArgs = new HashMap<>();
82+
83+
if(conf != null) {
84+
this.conf = conf;
85+
List<String> params = Arrays.asList("msgType", "responseCls", "errorMsg", "defaultAuthenticationMethod",
86+
"httpMethod", "bodyType", "responseBodyType");
87+
for(String param : params) {
88+
if(conf.containsKey(param)) {
89+
this.getClass().getField(param).set(this, conf.get("param"));
90+
}
91+
}
92+
} else {
93+
this.conf = new HashMap<>();
94+
}
95+
7996
this.preConstruct = new ArrayList<>();
8097
this.postConstruct = new ArrayList<>();
8198
this.postParseResponse = new ArrayList<>();
82-
this.httpMethod = "GET";
83-
this.bodyType = "urlencoded";
84-
this.responseBodyType = "json";
85-
this.defaultAuthenticationMethod = "";
86-
this.request = "";
87-
this.endpoint = "";
88-
this.endpointName = "";
89-
this.setUp();
9099
}
91100

92-
public void gatherRequestArgs(ClientInfo clientInfo, Map<String, Object> args) {
101+
public Map<String, Object> gatherRequestArgs(ClientInfo clientInfo, Map<String, Object> args) throws NoSuchFieldException {
102+
Map<String,Object> arArgs = new HashMap<>(args);
103+
Set<String> properties = this.msgType.getCParam().keySet();
93104

105+
Field field;
106+
for(String property : properties) {
107+
if(!arArgs.containsKey(property)) {
108+
field = clientInfo.getClass().getField(property);
109+
if(field != null) {
110+
arArgs.put(property, field);
111+
} else {
112+
field = this.conf.getRequestArgs().getProp();
113+
if(field != null) {
114+
arArgs.put(property, field);
115+
} else {
116+
arArgs.put(property, this.defaultRequestArgs.get(property));
117+
}
118+
}
119+
}
120+
}
121+
122+
return arArgs;
94123
}
95124

96-
public static Map<String, Map<String, String>> updateHttpArgs(Map<String, String> httpArgs, Map<String, Map<String, String>> info) {
125+
public static Map<String, Object> updateHttpArgs(Map<String, String> httpArgs, Map<String, Map<String, String>> info) {
97126
Map<String, String> hArgs = info.get("hArgs");
98127
if (hArgs == null) {
99128
hArgs = new HashMap<>();
@@ -161,7 +190,7 @@ public Message constructMessage(ClientInfo clientInfo, Map<String, Object> reque
161190

162191
List<Map<String, Object>> returnedArgs = this.doPreConstruct(clientInfo, requestArgs, args);
163192

164-
if (!this.msgType.c_param.containsKey("state")) {
193+
if (!this.msgType.getCParam().containsKey("state")) {
165194
args.remove("state");
166195
}
167196

@@ -295,7 +324,8 @@ public Map<String, Object> doRequestInit(ClientInfo clientInfo, String bodyType,
295324
}
296325

297326
public String getUrlInfo(String info) throws URISyntaxException {
298-
String query = null, fragment = null;
327+
String query = null;
328+
String fragment = null;
299329
if (info.contains("?") || info.contains("#")) {
300330
URI uri = new URI(info);
301331
query = uri.getQuery();
@@ -395,7 +425,7 @@ public String getValueType(FakeResponse response, String bodyType) throws WrongC
395425
}
396426
}
397427

398-
public ErrorResponse parseRequestResponse(ErrorResponse response, ClientInfo clientInfo, String responseBodyType,
428+
public static ErrorResponse parseRequestResponse(ErrorResponse response, ClientInfo clientInfo, String responseBodyType,
399429
String state, Map<String, Object> args) {
400430
int statusCode = response.getStatusCode();
401431
if (successfulCodes.contains(statusCode)) {

lib/src/main/java/oiccli/State.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Map<String, Object> getTokenInfo(String state) throws ExpiredToken {
144144
return getTokenInfo(state, 0);
145145
}
146146

147-
public Map<String, Object> getResponseArgs(String state, ABCMeta request, int now) throws ExpiredToken {
147+
public Map<String, Object> getResponseArgs(String state, AccessTokenRequest request, int now) throws ExpiredToken {
148148
Map<String, String> info = state.getState(state);
149149
Map<String, Object> responseArgs = new HashMap<>();
150150
for (String claim : request.c_param) {

lib/src/main/java/oiccli/Util.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package oiccli;
22

33
import com.auth0.jwt.creators.Message;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
45
import com.google.common.base.Strings;
56
import java.io.UnsupportedEncodingException;
67
import java.text.DateFormat;
@@ -66,7 +67,7 @@ public static Map<String, Object> getOrPost(String uri, String method, Message c
6667
return getOrPost(uri, method, cis, DEFAULT_POST_CONTENT_TYPE, null, args);
6768
}
6869

69-
public static Map<String, Object> getOrPost(String uri, String method, Message request, String contentType, String accept, Map<String, String> args) throws UnsupportedEncodingException, UnsupportedType {
70+
public static Map<String, Object> getOrPost(String uri, String method, Message request, String contentType, String accept, Map<String, String> args) throws UnsupportedEncodingException, UnsupportedType, JsonProcessingException {
7071
Map<String, Object> response = new HashMap<>();
7172
String urlEncoded;
7273
if (method.equals("GET") || method.equals("DELETE")) {
@@ -81,7 +82,7 @@ public static Map<String, Object> getOrPost(String uri, String method, Message r
8182
if (contentType.equals(URL_ENCODED)) {
8283
response.put("body", request.toUrlEncoded(request.toString()));
8384
} else if (contentType.equals(JSON_ENCODED)) {
84-
response.put("body", request.toJSON(request.toHashMap()));
85+
response.put("body", request.toJSON());
8586
} else {
8687
throw new UnsupportedType("Unsupported content type " + contentType);
8788
}
@@ -212,7 +213,7 @@ public static String verifyHeader(FakeResponse response, String bodyType) throws
212213
} else if (bodyType.equals(JSON)) {
213214
if (matchTo(JWT_ENCODED, contentType)) {
214215
bodyType = JWT;
215-
} else {
216+
} else if(!matchTo(JSON_ENCODED, contentType) || !matchTo(JRD_JSON, contentType)){
216217
throw new WrongContentType(contentType);
217218
}
218219
} else if (bodyType.equals(JWT)) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package oiccli;
2+
3+
import com.auth0.jwt.creators.Message;
4+
import com.google.common.base.Strings;
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
import java.util.Map;
10+
import oiccli.client_info.ClientInfo;
11+
import oiccli.exceptions.MissingRequiredAttribute;
12+
13+
public class Utils {
14+
15+
public static Message requestObjectEncryption(Message message, ClientInfo clientInfo, Map<String,Object> args) throws MissingRequiredAttribute {
16+
String encryptionAlg = (String) args.get("requestObjectEncryptionAlg");
17+
18+
if(Strings.isNullOrEmpty(encryptionAlg)) {
19+
List<String> listOfAlgs = clientInfo.getBehavior().get("requestObjectEncryptionAlg");
20+
if(listOfAlgs != null || !listOfAlgs.isEmpty()) {
21+
encryptionAlg = listOfAlgs.get(0);
22+
}
23+
24+
if(encryptionAlg == null) {
25+
return message;
26+
}
27+
}
28+
29+
String encryptionEnc = (String) args.get("requestObjectEncryptionEnc");
30+
31+
if(Strings.isNullOrEmpty(encryptionEnc)) {
32+
List<String> listOfAlgs = clientInfo.getBehavior().get("requestObjectEncryptionEnc");
33+
if(listOfAlgs != null || !listOfAlgs.isEmpty()) {
34+
encryptionEnc = listOfAlgs.get(0);
35+
}
36+
37+
if(encryptionEnc == null) {
38+
throw new MissingRequiredAttribute("No requestObjectEncryptionEnc specified");
39+
}
40+
}
41+
42+
JWE jwe = new JWE(message, encryptionAlg, encryptionEnc);
43+
String keyType = StringUtil.alg2keytype(encryptionAlg);
44+
45+
String kid = (String) args.get("encKid");
46+
if(Strings.isNullOrEmpty(kid)) {
47+
kid = "";
48+
}
49+
50+
if(!args.containsKey("target")) {
51+
throw new MissingRequiredAttribute("No target specified");
52+
}
53+
54+
List<Key> keys;
55+
if(!Strings.isNullOrEmpty(kid)) {
56+
keys = clientInfo.getKeyJar().getEncryptKey(keyType, args.get("target"), kid);
57+
jwe.setKid(kid);
58+
} else {
59+
keys = clientInfo.getKeyJar().getEncryptKey(keyType, args.get("target"));
60+
}
61+
62+
return jwe.encrypt(keys);
63+
}
64+
65+
public static Tuple constructRequestUri(String localDir, String basePath, Map<String,String> args) {
66+
File file = new File(localDir);
67+
if(!file.isDirectory()) {
68+
file.mkdirs();
69+
}
70+
String name = StringUtil.generateRandomString(10) + ".jwt";
71+
File fileExists = Paths.get(localDir, name).toFile();
72+
while(fileExists.exists()) {
73+
name = StringUtil.generateRandomString(10);
74+
fileExists = Paths.get(localDir, name).toFile();
75+
}
76+
77+
String webName = basePath + name;
78+
return new Tuple(fileExists.toString(), webName);
79+
}
80+
}

lib/src/main/java/oiccli/client_auth/BearerHeader.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import java.util.HashMap;
44
import java.util.Map;
55
import oiccli.client_info.ClientInfo;
6+
import oiccli.exceptions.MissingRequiredAttribute;
67

78
public class BearerHeader {
89

910
public Map<String, Map<String, String>> construct(Map<String, String> cis, ClientInfo clientInfo, Map<String, String> requestArgs, Map<String, Map<String, String>> httpArgs,
10-
Map<String, String> args) {
11+
Map<String, String> args) throws MissingRequiredAttribute {
1112
String accessToken;
1213
if (cis != null) {
1314
if (cis.containsKey("accessToken")) {
@@ -16,19 +17,15 @@ public Map<String, Map<String, String>> construct(Map<String, String> cis, Clien
1617
//cis.c_param["access_token"] = SINGLE_OPTIONAL_STRING
1718
} else {
1819
accessToken = requestArgs.get("accessToken");
19-
requestArgs.remove(accessToken);
2020

21-
if (accessToken != null) {
22-
accessToken = args.get("accessToken");
23-
if (accessToken == null) {
24-
accessToken = clientInfo.getStateDb().getTokenInfo(args).get("accessToken");
25-
}
21+
if (accessToken == null) {
22+
accessToken = clientInfo.getStateDb().getTokenInfo(args).get("accessToken");
2623
}
2724
}
2825
} else {
2926
accessToken = args.get("accessToken");
3027
if (accessToken == null) {
31-
accessToken = requestArgs.get("accessToken");
28+
throw new MissingRequiredAttribute("accessToken");
3229
}
3330
}
3431

0 commit comments

Comments
 (0)