Skip to content

Commit a33a2c2

Browse files
authored
Merge pull request #1194 from zeitlinger/auth-per-host
allow to filter authorization by URL
2 parents 7a077bf + 5450ff0 commit a33a2c2

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/AuthorizationValue.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package io.swagger.v3.parser.core.models;
22

3+
import java.net.URL;
4+
import java.util.Objects;
5+
import java.util.function.Predicate;
6+
7+
38
public class AuthorizationValue {
49
private String value, type, keyName;
10+
private Predicate<URL> urlMatcher;
511

612
public AuthorizationValue() {
713
}
814

9-
public AuthorizationValue(String keyName, String value, String type) {
15+
public AuthorizationValue(String keyName, String value, String type, Predicate<URL> urlMatcher) {
1016
this.setKeyName(keyName);
1117
this.setValue(value);
1218
this.setType(type);
19+
this.setUrlMatcher(urlMatcher);
20+
}
21+
22+
public AuthorizationValue(String keyName, String value, String type) {
23+
this(keyName, value, type, url -> true);
1324
}
1425

1526
public AuthorizationValue value(String value) {
@@ -27,6 +38,11 @@ public AuthorizationValue keyName(String keyName) {
2738
return this;
2839
}
2940

41+
public AuthorizationValue urlMatcher(Predicate<URL> urlMatcher) {
42+
setUrlMatcher(urlMatcher);
43+
return this;
44+
}
45+
3046
public String getValue() {
3147
return value;
3248
}
@@ -51,13 +67,22 @@ public void setKeyName(String keyName) {
5167
this.keyName = keyName;
5268
}
5369

70+
public Predicate<URL> getUrlMatcher() {
71+
return urlMatcher;
72+
}
73+
74+
public void setUrlMatcher(Predicate<URL> urlMatcher) {
75+
this.urlMatcher = Objects.requireNonNull(urlMatcher);
76+
}
77+
5478
@Override
5579
public int hashCode() {
5680
final int prime = 31;
5781
int result = 1;
5882
result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
5983
result = prime * result + ((type == null) ? 0 : type.hashCode());
6084
result = prime * result + ((value == null) ? 0 : value.hashCode());
85+
result = prime * result + urlMatcher.hashCode();
6186
return result;
6287
}
6388

@@ -94,6 +119,9 @@ public boolean equals(Object obj) {
94119
} else if (!value.equals(other.value)) {
95120
return false;
96121
}
122+
if (!urlMatcher.equals(other.urlMatcher)) {
123+
return false;
124+
}
97125
return true;
98126
}
99127
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import io.swagger.v3.parser.util.RemoteUrl;
1414
import io.swagger.v3.parser.util.ResolverFully;
1515
import org.apache.commons.io.FileUtils;
16-
import javax.net.ssl.SSLHandshakeException;
1716
import org.slf4j.Logger;
1817
import org.slf4j.LoggerFactory;
1918

19+
import javax.net.ssl.SSLHandshakeException;
2020
import java.net.URI;
2121
import java.nio.charset.Charset;
2222
import java.nio.file.Files;
@@ -127,7 +127,7 @@ private ObjectMapper getRightMapper(String data) {
127127
}
128128
return mapper;
129129
}
130-
130+
131131
public SwaggerParseResult readWithInfo(String location, List<AuthorizationValue> auths) {
132132
String data;
133133

@@ -175,7 +175,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
175175
SwaggerParseResult result = new SwaggerParseResult();
176176
if(swaggerAsString != null && !"".equals(swaggerAsString.trim())) {
177177
ObjectMapper mapper = getRightMapper(swaggerAsString);
178-
178+
179179
if(auth == null) {
180180
auth = new ArrayList<>();
181181
}
@@ -217,7 +217,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
217217
* Locates extensions on the current thread class loader and then, if it differs
218218
* from this class classloader (as in OSGi), locates extensions from this
219219
* class classloader as well.
220-
*
220+
*
221221
* @return a list of extensions
222222
*/
223223
public static List<SwaggerParserExtension> getExtensions() {
@@ -262,6 +262,7 @@ protected List<AuthorizationValue> transform(List<AuthorizationValue> input) {
262262
v.setKeyName(value.getKeyName());
263263
v.setValue(value.getValue());
264264
v.setType(value.getType());
265+
v.setUrlMatcher(value.getUrlMatcher());
265266

266267
output.add(v);
267268
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RemoteUrl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
115115
final List<AuthorizationValue> header = new ArrayList<>();
116116
if (auths != null && auths.size() > 0) {
117117
for (AuthorizationValue auth : auths) {
118-
if ("query".equals(auth.getType())) {
119-
appendValue(inUrl, auth, query);
120-
} else if ("header".equals(auth.getType())) {
121-
appendValue(inUrl, auth, header);
118+
if (auth.getUrlMatcher().test(inUrl)) {
119+
if ("query".equals(auth.getType())) {
120+
appendValue(inUrl, auth, query);
121+
} else if ("header".equals(auth.getType())) {
122+
appendValue(inUrl, auth, header);
123+
}
122124
}
123125
}
124126
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/RemoteUrlTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
import com.github.tomakehurst.wiremock.WireMockServer;
44
import com.github.tomakehurst.wiremock.client.WireMock;
5+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
56
import io.swagger.v3.parser.core.models.AuthorizationValue;
67
import org.testng.annotations.AfterMethod;
78
import org.testng.annotations.BeforeMethod;
89
import org.testng.annotations.Test;
910

1011
import java.util.Arrays;
12+
import java.util.List;
1113

1214
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
1315
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
1416
import static com.github.tomakehurst.wiremock.client.WireMock.get;
1517
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
1618
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
1719
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
18-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
1920
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
2021
import static org.testng.Assert.assertEquals;
22+
import static org.testng.Assert.assertFalse;
2123

2224
public class RemoteUrlTest {
2325

@@ -76,6 +78,43 @@ public void testAuthorizationHeader() throws Exception {
7678
);
7779
}
7880

81+
@Test
82+
public void testAuthorizationHeaderWithMatchingUrl() throws Exception {
83+
84+
final String expectedBody = setupStub();
85+
86+
final String headerName = "Authorization";
87+
final String headerValue = "foobar";
88+
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header",
89+
url -> url.toString().startsWith("http://localhost"));
90+
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
91+
92+
assertEquals(actualBody, expectedBody);
93+
94+
verify(getRequestedFor(urlEqualTo("/v2/pet/1"))
95+
.withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER))
96+
.withHeader(headerName, equalTo(headerValue))
97+
);
98+
}
99+
100+
@Test
101+
public void testAuthorizationHeaderWithNonMatchingUrl() throws Exception {
102+
103+
final String expectedBody = setupStub();
104+
105+
final String headerValue = "foobar";
106+
String authorization = "Authorization";
107+
final AuthorizationValue authorizationValue = new AuthorizationValue(authorization,
108+
headerValue, "header", u -> false);
109+
final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
110+
111+
assertEquals(actualBody, expectedBody);
112+
113+
List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1")));
114+
assertEquals(1, requests.size());
115+
assertFalse(requests.get(0).containsHeader(authorization));
116+
}
117+
79118
private String getUrl() {
80119
return String.format("http://%s:%d/v2/pet/1", LOCALHOST, WIRE_MOCK_PORT);
81120
}

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ public void testIssue768() {
124124

125125
@Test
126126
public void test30Url() {
127-
String location = "http://petstore.swagger.io/v2/swagger.json";
127+
String location = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml";
128128

129129
SwaggerParseResult result = new OpenAPIParser().readLocation(location, null, null);
130130

131131
assertNotNull(result);
132132
assertNotNull(result.getOpenAPI());
133-
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
133+
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.0");
134134
}
135135

136136
@Test

0 commit comments

Comments
 (0)