forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
255 lines (231 loc) · 10.2 KB
/
Util.java
File metadata and controls
255 lines (231 loc) · 10.2 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
package oiccli;
import com.auth0.jwt.creators.Message;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oiccli.exceptions.UnsupportedType;
import oiccli.exceptions.ValueError;
import oiccli.exceptions.WrongContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Util {
private static final String URL_ENCODED = "application/x-www-form-urlencoded";
private static final String JSON_ENCODED = "application/json";
private static final String JRD_JSON = "application/jrd+json";
private static final String JWT_ENCODED = "application/jwt";
private static final String PLAIN_TEXT = "text/plain";
private static final String HTML_TEXT = "text/html";
private static final String JWT = "jwt";
private static final String JSON = "json";
private static final String URLENCODED = "urlencoded";
private static final String TEXT = "txt";
private static final String DEFAULT_POST_CONTENT_TYPE = URL_ENCODED;
private static final Map<String, String> pairs = new HashMap<String, String>() {{
put("port", "portSpecified");
put("domain", "domainSpecified");
put("path", "pathSpecified");
}};
private static final Map<String, Object> attributes = new HashMap<String, Object>() {{
put("version", null);
put("name", "");
put("value", null);
put("port", null);
put("portSpecified", false);
put("domain", "");
put("domainSpecified", false);
put("domainInitialDot", false);
put("path", "");
put("pathSpecified", false);
put("secure", false);
put("expires", null);
put("discard", true);
put("comment", null);
put("commentUrl", null);
put("rest", "");
put("rfc2109", true);
}};
private final static Logger logger = LoggerFactory.getLogger(Util.class);
private final static Map<String, Integer> sortOrder = new HashMap<String, Integer>() {{
put("RS", 0);
put("ES", 1);
put("HS", 2);
put("PS", 3);
put("no", 4);
}};
public static Map<String, Object> getOrPost(String uri, String method, Message cis, Map<String, String> args) throws UnsupportedEncodingException, UnsupportedType {
return getOrPost(uri, method, cis, DEFAULT_POST_CONTENT_TYPE, null, args);
}
public static Map<String, Object> getOrPost(String uri, String method, Message request, String contentType, String accept, Map<String, String> args) throws UnsupportedEncodingException, UnsupportedType, JsonProcessingException {
Map<String, Object> response = new HashMap<>();
String urlEncoded;
if (method.equals("GET") || method.equals("DELETE")) {
/*urlEncoded = request.toUrlEncoded(request.toString());
if (urlEncoded.contains("?")) {
response.put("uri", uri + '&' + urlEncoded);
} else {
response.put("uri", uri + '?' + urlEncoded);
}*/
} else if (method.equals("POST") || method.equals("PUT")) {
response.put("uri", uri);
if (contentType.equals(URL_ENCODED)) {
response.put("body", request.toUrlEncoded(request.toString()));
} else if (contentType.equals(JSON_ENCODED)) {
response.put("body", request.toJSON());
} else {
throw new UnsupportedType("Unsupported content type " + contentType);
}
Map<String, Object> headers = new HashMap<>();
headers.put("Content-Type", contentType);
if (accept != null) {
headers = new HashMap<>();
headers.put("Accept", accept);
}
if (args.containsKey("headers")) {
//kwargs["headers"].update(header_ext)
} else {
args.put("headers", headers);
}
response.put("args", args);
} else {
throw new UnsupportedType("Unsupported HTTP method " + method);
}
return response;
}
public static void setCookie(CookieJar cookieJar, Map<String, Morsel> cookieMap) {
Map<String, Object> attributesCopy;
String codedValue;
Morsel morsel;
String morselValue;
for (String cookieName : cookieMap.keySet()) {
attributesCopy = new HashMap<>(attributes);
attributesCopy.put("name", cookieName);
morsel = cookieMap.get(cookieName);
codedValue = morsel.getCodedValue();
if (codedValue.startsWith("\"") && codedValue.endsWith("\"")) {
attributesCopy.put("value", codedValue.substring(1, codedValue.length() - 1));
} else {
attributesCopy.put("value", codedValue);
}
attributesCopy.put("version", 0);
String failedAttribute = null;
try {
for (String attribute : morsel.keySet()) {
failedAttribute = attribute;
if (attributes.containsKey(attribute)) {
morselValue = morsel.get(attribute);
if (!Strings.isNullOrEmpty(morselValue)) {
if (attribute.equals("expires")) {
attributesCopy.put(attribute, dateToTime(morselValue));
} else {
attributesCopy.put(attribute, morselValue);
}
} else if (attribute.equals("maxAge")) {
if (!Strings.isNullOrEmpty(morselValue)) {
attributesCopy.put("expires", dateToTime(morselValue));
}
}
}
}
} catch (ParseException e) {
logger.info("Time format error on " + failedAttribute + " parameter in received cookie");
continue;
}
for (String attribute : pairs.keySet()) {
if (attributesCopy.get(attribute) != null) {
attributesCopy.put(pairs.get(attribute), true);
}
}
if (attributesCopy.get("domain") instanceof String && !Strings.isNullOrEmpty((String) attributesCopy.get("domain")) && ((String) attributesCopy.get("domain")).startsWith(".")) {
attributesCopy.put("domainInitialDot", true);
}
if (morsel.getMaxAge() == 0) {
cookieJar.clear(attributesCopy.getDomain(), attributesCopy.getPath(), attributesCopy.getName());
} else {
if (attributesCopy.containsKey("version")) {
attributesCopy.put("version", ((String) attributesCopy.get("version")).split(",")[0]);
}
Cookie newCookie = new Cookie();
cookieJar.setCookie(newCookie);
}
}
}
public static boolean matchTo(String value, List<String> valuesList) {
for (String index : valuesList) {
if (index.startsWith(value)) {
return true;
}
}
return false;
}
public static boolean matchTo(String value, String valueExpected) {
return valueExpected.startsWith(value);
}
public static String verifyHeader(FakeResponse response, String bodyType) throws WrongContentType, ValueError {
logger.debug("Response headers: " + response.getHeaders().toString());
logger.debug("Response txt: " + response.getText().toString());
String contentType = response.getHeaders().getContentType();
if (Strings.isNullOrEmpty(contentType)) {
if (!Strings.isNullOrEmpty(bodyType)) {
return bodyType;
} else {
return "txt";
}
}
logger.debug("Expected body type: " + bodyType);
if (bodyType.equals("")) {
if (matchTo(JSON_ENCODED, contentType) || matchTo(JRD_JSON, contentType)) {
bodyType = JSON;
} else if (matchTo(JWT_ENCODED, contentType)) {
bodyType = JWT;
} else if (matchTo(URL_ENCODED, contentType)) {
bodyType = URLENCODED;
} else {
bodyType = TEXT;
}
} else if (bodyType.equals(JSON)) {
if (matchTo(JWT_ENCODED, contentType)) {
bodyType = JWT;
} else if(!matchTo(JSON_ENCODED, contentType) || !matchTo(JRD_JSON, contentType)){
throw new WrongContentType(contentType);
}
} else if (bodyType.equals(JWT)) {
if (!matchTo(JWT_ENCODED, contentType)) {
throw new WrongContentType(contentType);
}
} else if (bodyType.equals(URLENCODED)) {
if (!matchTo(DEFAULT_POST_CONTENT_TYPE, contentType) &&
!matchTo(PLAIN_TEXT, contentType)) {
throw new WrongContentType(contentType);
}
} else if (bodyType.equals(TEXT)) {
if (!matchTo(PLAIN_TEXT, contentType) && !matchTo(HTML_TEXT, contentType)) {
throw new WrongContentType("Content type: " + contentType);
}
} else {
throw new ValueError("Unknown return format: " + bodyType);
}
logger.debug("Got body type: " + bodyType);
return bodyType;
}
public Integer sortSignAlgorithm(String algorithm1, String algorithm2) {
if (sortOrder.get(algorithm1.substring(0, 2)) < sortOrder.get(algorithm2.substring(0, 2))) {
return -1;
} else if (sortOrder.get(algorithm1.substring(0, 2)) > sortOrder.get(algorithm2.substring(0, 2))) {
return 1;
} else {
return algorithm1.compareTo(algorithm2);
}
}
public static long dateToTime(String date) throws ParseException {
DateFormat inputFormat = new SimpleDateFormat("dd MMM yyy HH:mm:ss zz");
Date d = inputFormat.parse(date);
return d.getTime();
}
}