use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method readSecuritySchemesObject.
@Test(dataProvider = "data")
public void readSecuritySchemesObject(JsonNode rootNode) throws Exception {
final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
final SwaggerParseResult result = deserializer.deserialize(rootNode);
Assert.assertNotNull(result);
List<String> messages = result.getMessages();
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.name is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.in is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.scheme is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.openIdConnectUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.tokenUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_password'.authorizationUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_clientCredentials'.authorizationUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.scheme is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.flows is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.openIdConnectUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.http'.name is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.http'.in is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.http'.flows is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.http'.openIdConnectUrl is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.name is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.in is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.scheme is missing"));
assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.flows is missing"));
final OpenAPI openAPI = result.getOpenAPI();
Assert.assertNotNull(openAPI);
final Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
Assert.assertNotNull(securitySchemes);
Assert.assertEquals(securitySchemes.size(), 10);
SecurityScheme securityScheme = securitySchemes.get("reference");
assertTrue(securityScheme.get$ref().equals("#/components/securitySchemes/api_key"));
securityScheme = securitySchemes.get("remote_reference");
assertTrue(securityScheme.get$ref().equals("http://localhost:${dynamicPort}/remote/security#/petstore_remote"));
securityScheme = securitySchemes.get("petstore_auth");
assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
securityScheme = securitySchemes.get("petstore_auth_password");
assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
securityScheme = securitySchemes.get("petstore_auth_clientCredentials");
assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
securityScheme = securitySchemes.get("petstore_auth_authorizationCode");
assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
securityScheme = securitySchemes.get("api_key");
assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
assertTrue(securityScheme.getIn() == SecurityScheme.In.HEADER);
securityScheme = securitySchemes.get("api_key_cookie");
assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
assertTrue(securityScheme.getIn() == SecurityScheme.In.COOKIE);
securityScheme = securitySchemes.get("http");
assertTrue(securityScheme.getType() == SecurityScheme.Type.HTTP);
securityScheme = securitySchemes.get("openID");
assertTrue(securityScheme.getType() == SecurityScheme.Type.OPENIDCONNECT);
}
use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.
the class SwaggerConverter method convertOauth2SecurityScheme.
private SecurityScheme convertOauth2SecurityScheme(SecuritySchemeDefinition definition) {
SecurityScheme securityScheme = new SecurityScheme();
OAuth2Definition oAuth2Definition = (OAuth2Definition) definition;
OAuthFlows oAuthFlows = new OAuthFlows();
OAuthFlow oAuthFlow = new OAuthFlow();
securityScheme.setType(SecurityScheme.Type.OAUTH2);
String flow = oAuth2Definition.getFlow();
if (flow != null) {
switch(flow) {
case "implicit":
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
oAuthFlows.setImplicit(oAuthFlow);
break;
case "password":
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setPassword(oAuthFlow);
break;
case "application":
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setClientCredentials(oAuthFlow);
break;
case "accessCode":
oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
oAuthFlows.setAuthorizationCode(oAuthFlow);
break;
}
}
Scopes scopes = new Scopes();
Map<String, String> oAuth2Scopes = oAuth2Definition.getScopes();
if (oAuth2Scopes != null) {
oAuth2Scopes.forEach((k, v) -> scopes.addString(k, v));
}
oAuthFlow.setScopes(scopes);
securityScheme.setFlows(oAuthFlows);
return securityScheme;
}
use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.
the class SwaggerConverter method createBasicSecurityScheme.
private SecurityScheme createBasicSecurityScheme() {
SecurityScheme securityScheme = new SecurityScheme();
securityScheme.setType(SecurityScheme.Type.HTTP);
securityScheme.setScheme("basic");
return securityScheme;
}
use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.
the class V2ConverterTest method testIssue15.
@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
assertNotNull(oas);
Info info = oas.getInfo();
assertNotNull(info.getExtensions().get("x-apis-json"));
assertNotNull(info.getLicense().getExtensions().get("x-notes"));
assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
PathItem pathItem = oas.getPaths().get("/something");
assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
Operation get = pathItem.getGet();
assertNotNull(get.getExtensions().get("x-version"));
assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
assertNotNull(securitySchemes);
assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.
the class ComponentsProcessor method processSecuritySchemes.
private void processSecuritySchemes(Set<String> securitySchemeKey, Map<String, SecurityScheme> securitySchemes) {
securitySchemeKey.addAll(securitySchemes.keySet());
for (String securitySchemeName : securitySchemeKey) {
final SecurityScheme securityScheme = securitySchemes.get(securitySchemeName);
SecurityScheme resolvedSecurityScheme = securitySchemeProcessor.processSecurityScheme(securityScheme);
securitySchemes.replace(securitySchemeName, securityScheme, resolvedSecurityScheme);
}
}
Aggregations