Skip to content

Commit 2065b0f

Browse files
authored
Merge pull request #269 from HydrologicEngineeringCenter/CWMS-2000_Adding_support_for_discoverable_auth_url
CWMS-2000 - Updates to throw IOException if failed to parse open api spec
2 parents 9a7506c + c34e721 commit 2065b0f

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

.github/coveragereport/badge_branchcoverage.svg

Lines changed: 1 addition & 1 deletion
Loading

cwms-radar-client/src/main/java/mil/army/usace/hec/cwms/radar/client/controllers/CdaOpenIdTokenController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public final class CdaOpenIdTokenController extends OpenIdTokenController {
3737
protected String retrieveWellKnownEndpoint(ApiConnectionInfo apiConnectionInfo) throws IOException {
3838
String url = apiConnectionInfo.getApiRoot() + "/" + SWAGGER_DOC_ENDPOINT;
3939
OpenAPI openAPI = new OpenAPIV3Parser().read(url);
40+
if(openAPI == null) {
41+
throw new IOException("Failed to parse OpenAPI spec from " + url);
42+
}
4043
SecurityScheme openIdScheme = openAPI.getComponents()
4144
.getSecuritySchemes()
4245
.get("OpenIDConnect");

cwms-radar-client/src/test/java/mil/army/usace/hec/cwms/radar/client/controllers/TestCdaOpenIdTokenController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import com.fasterxml.jackson.databind.ObjectMapper;
2727
import com.fasterxml.jackson.databind.node.ObjectNode;
2828
import hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
29+
import java.io.IOException;
2930
import javax.net.ssl.SSLSocketFactory;
3031
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
3132
import mil.army.usace.hec.cwms.http.client.SslSocketData;
3233
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
3335
import org.junit.jupiter.api.Test;
3436
import org.mockito.Mockito;
3537

@@ -53,4 +55,24 @@ void testRetrieveTokenUrl() throws Exception {
5355
ApiConnectionInfo tokenUrl = new CdaOpenIdTokenController().retrieveTokenUrl(buildConnectionInfo(), sslSocketData);
5456
assertEquals("https://api.example.com/auth/realms/cwbi/protocol/openid-connect/token", tokenUrl.getApiRoot());
5557
}
58+
59+
@Test
60+
void testRetrieveTokenUrlNoSpec() throws Exception {
61+
SSLSocketFactory mockSslSocketFactory = Mockito.mock(SSLSocketFactory.class);
62+
String resource = "{\"spec\": \"none\"}";
63+
String openIdConfig = "radar/v1/json/openIdConfig.json";
64+
ObjectMapper mapper = new ObjectMapper();
65+
ObjectNode node = (ObjectNode) mapper.readTree(resource);
66+
ApiConnectionInfo webServiceUrl = buildConnectionInfo();
67+
ObjectNode components = node.with("components");
68+
ObjectNode securitySchemes = components.with("securitySchemes");
69+
ObjectNode openIdConnect = securitySchemes.with("OpenIDConnect");
70+
openIdConnect.remove("openIdConnectUrl");
71+
openIdConnect.put("openIdConnectUrl", webServiceUrl.getApiRoot() + "/.well-known/openid-configuration");
72+
String updatedIdpConfig = mapper.writeValueAsString(node);
73+
mockHttpServer.enqueue(updatedIdpConfig);
74+
mockHttpServer.enqueue(readJsonFile(openIdConfig));
75+
SslSocketData sslSocketData = new SslSocketData(mockSslSocketFactory, CwbiAuthTrustManager.getTrustManager());
76+
assertThrows(IOException.class, () -> new CdaOpenIdTokenController().retrieveTokenUrl(buildConnectionInfo(), sslSocketData));
77+
}
5678
}

0 commit comments

Comments
 (0)