-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathWorker.java
More file actions
202 lines (163 loc) · 7.61 KB
/
Worker.java
File metadata and controls
202 lines (163 loc) · 7.61 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
package swurg.workers;
import burp.api.montoya.http.HttpService;
import burp.api.montoya.http.message.HttpHeader;
import burp.api.montoya.http.message.params.HttpParameter;
import burp.api.montoya.http.message.requests.HttpRequest;
import burp.http.MyHttpRequest;
import burp.api.montoya.core.ByteArray;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.apache.http.client.utils.URIBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Worker {
public OpenAPI processOpenAPI(String resource) {
try {
return readOpenAPI(resource).getOpenAPI();
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
}
private SwaggerParseResult readOpenAPI(String resource) {
try {
SwaggerParseResult result = new OpenAPIParser().readLocation(resource, null, null);
if (result.getOpenAPI() == null)
throw new IllegalArgumentException(result.getMessages().toString());
return result;
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
}
public List<String> parseMetadata(OpenAPI openAPI) {
List<String> metadataList = new ArrayList<>();
Optional.ofNullable(openAPI.getSpecVersion())
.map(specVersion -> "Spec Version: " + specVersion.toString())
.ifPresent(metadataList::add);
Optional.ofNullable(openAPI.getJsonSchemaDialect())
.map(jsonSchemaDialect -> "Json Schema Dialect: " + jsonSchemaDialect.toString())
.ifPresent(metadataList::add);
Optional.ofNullable(openAPI.getExternalDocs())
.map(externalDoc -> "External Docs: " + externalDoc.toString())
.ifPresent(metadataList::add);
Optional.ofNullable(openAPI.getInfo())
.map(info -> "Info: " + info.toString())
.ifPresent(metadataList::add);
return metadataList;
}
public static String fixUriPath(String rawPath) {
return rawPath.replaceFirst("^/+", "");
}
public List<MyHttpRequest> parseOpenAPI(OpenAPI openAPI) {
List<MyHttpRequest> logEntries = new ArrayList<>();
openAPI.getServers().forEach(server -> {
String serverUrl = Optional.ofNullable(server.getUrl())
.filter(url -> url.startsWith("http://") || url.startsWith("https://"))
.orElse("https://example.com");
openAPI.getPaths().forEach(
(path, pathItem) -> getOperationMap(pathItem).forEach((method, operation) -> Optional.ofNullable(operation)
.ifPresent(op -> {
try {
URI serverUri = new URI(serverUrl);
URI baseUrl = new URIBuilder(serverUri).setPath(serverUri.getPath() + fixUriPath(path))
.build();
HttpService httpService = HttpService.httpService(baseUrl.toString());
List<HttpHeader> httpHeaders = buildHttp2RequestHeaders(
method, baseUrl, op.getRequestBody(), op.getResponses());
List<HttpParameter> httpParameters = buildHttpRequestParameters(
op.getParameters(), op.getRequestBody(),
openAPI.getComponents().getSchemas());
HttpRequest httpRequest = HttpRequest.http2Request(
httpService, httpHeaders, ByteArray.byteArray(new byte[0]))
.withAddedParameters(httpParameters);
int contentLength = httpRequest.body().length();
if (contentLength > 0)
httpRequest = httpRequest.withAddedHeader(HttpHeader
.httpHeader("content-length", String.valueOf(contentLength)));
logEntries.add(new MyHttpRequest(httpRequest, op.getDescription()));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
})));
});
return logEntries;
}
private Map<String, Operation> getOperationMap(PathItem pathItem) {
Map<String, Operation> operationMap = new HashMap<>();
if (pathItem != null) {
operationMap.put("DELETE", pathItem.getDelete());
operationMap.put("GET", pathItem.getGet());
operationMap.put("HEAD", pathItem.getHead());
operationMap.put("PATCH", pathItem.getPatch());
operationMap.put("POST", pathItem.getPost());
operationMap.put("PUT", pathItem.getPut());
operationMap.put("TRACE", pathItem.getTrace());
}
return operationMap;
}
private List<HttpHeader> buildHttp2RequestHeaders(String method, URI uri, RequestBody requestBody,
ApiResponses apiResponses) {
List<HttpHeader> httpHeaders = new ArrayList<>();
httpHeaders.add(HttpHeader.httpHeader(":scheme", uri.getScheme()));
httpHeaders.add(HttpHeader.httpHeader(":method", method));
httpHeaders.add(HttpHeader.httpHeader(":path", uri.getPath()));
httpHeaders.add(HttpHeader.httpHeader(":authority", uri.getAuthority()));
Optional.ofNullable(apiResponses)
.map(responses -> responses.get("200"))
.map(ApiResponse::getContent)
.map(contentMap -> String.join(",", contentMap.keySet()))
.ifPresent(acceptHeaderValue -> httpHeaders.add(HttpHeader.httpHeader("accept", acceptHeaderValue)));
Optional.ofNullable(requestBody)
.map(RequestBody::getContent)
.flatMap(contentMap -> contentMap.keySet().stream().findFirst())
.ifPresent(contentType -> httpHeaders.add(HttpHeader.httpHeader("content-type", contentType)));
return httpHeaders;
}
private List<HttpParameter> buildHttpRequestParameters(List<Parameter> parameters, RequestBody requestBody,
Map<String, Schema> schemas) {
List<HttpParameter> httpParameters = new ArrayList<>();
Optional.ofNullable(parameters)
.ifPresent(parameterList -> parameterList.forEach(parameter -> {
String in = parameter.getIn();
String name = parameter.getName();
Schema schema = parameter.getSchema();
String value = Optional.ofNullable(schema)
.map(Schema::getType)
.orElse(null);
if ("header".equals(in))
httpParameters.add(HttpParameter.cookieParameter(name, value));
else if ("query".equals(in))
httpParameters.add(HttpParameter.urlParameter(name, value));
}));
Optional.ofNullable(requestBody)
.map(RequestBody::getContent)
.flatMap(content -> content.entrySet().stream().findFirst())
.map(Map.Entry::getValue)
.map(MediaType::getSchema)
.map(Schema::get$ref)
.ifPresent(ref -> {
Schema schema = schemas.get(ref.substring(ref.lastIndexOf("/") + 1));
Map<String, Schema> properties = schema.getProperties();
Optional.ofNullable(properties)
.ifPresent(props -> props.forEach((name, propertySchema) -> {
Object example = propertySchema.getExample();
String value = Optional.ofNullable(example).map(Object::toString).orElse(propertySchema.getType());
httpParameters.add(HttpParameter.bodyParameter(name, value));
}));
});
return httpParameters;
}
}