io.swagger.v3.oas.models.security.OAuthFlows Java Examples

The following examples show how to use io.swagger.v3.oas.models.security.OAuthFlows. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuthFlowsDiff.java    From openapi-diff with Apache License 2.0 6 votes vote down vote up
public Optional<ChangedOAuthFlows> diff(OAuthFlows left, OAuthFlows right) {
  ChangedOAuthFlows changedOAuthFlows = new ChangedOAuthFlows(left, right);
  if (left != null && right != null) {
    openApiDiff
        .getOAuthFlowDiff()
        .diff(left.getImplicit(), right.getImplicit())
        .ifPresent(changedOAuthFlows::setImplicitOAuthFlow);
    openApiDiff
        .getOAuthFlowDiff()
        .diff(left.getPassword(), right.getPassword())
        .ifPresent(changedOAuthFlows::setPasswordOAuthFlow);
    openApiDiff
        .getOAuthFlowDiff()
        .diff(left.getClientCredentials(), right.getClientCredentials())
        .ifPresent(changedOAuthFlows::setClientCredentialOAuthFlow);
    openApiDiff
        .getOAuthFlowDiff()
        .diff(left.getAuthorizationCode(), right.getAuthorizationCode())
        .ifPresent(changedOAuthFlows::setAuthorizationCodeOAuthFlow);
  }
  openApiDiff
      .getExtensionsDiff()
      .diff(getExtensions(left), getExtensions(right))
      .ifPresent(changedOAuthFlows::setExtensions);
  return isChanged(changedOAuthFlows);
}
 
Example #2
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 6 votes vote down vote up
private OpenAPI createBasicModel() {
    OpenAPI openAPI = new OpenAPI();

    Info info = new Info();
    info.setTitle(configuration.getApplicationTitle());
    info.setVersion(configuration.getApplicationApiVersion());
    openAPI.setInfo(info);

    Paths paths = new Paths();
    openAPI.setPaths(paths);

    Server server = new Server();
    server.setUrl(configuration.getServerUrl());
    server.setDescription(configuration.getServerDescription());
    openAPI.setServers(Collections.singletonList(server));
    Components components = new Components();
    SecurityScheme vaadinConnectOAuth2Scheme = new SecurityScheme()
            .type(SecurityScheme.Type.OAUTH2)
            .flows(new OAuthFlows().password(new OAuthFlow()
                    .tokenUrl(VAADIN_CONNECT_OAUTH2_TOKEN_URL)
                    .scopes(new Scopes())));
    components.addSecuritySchemes(VAADIN_CONNECT_OAUTH2_SECURITY_SCHEME,
            vaadinConnectOAuth2Scheme);
    openAPI.components(components);
    return openAPI;
}
 
Example #3
Source File: JaxRsActivatorNew.java    From pnc with Apache License 2.0 6 votes vote down vote up
private SecurityScheme getAuthScheme() {
    try {
        final KeycloakClientConfig keycloakConfig = systemConfig.getKeycloakServiceAccountConfig();
        if (keycloakConfig == null || StringUtils.isEmpty(keycloakConfig.getAuthServerUrl())) {
            return null;
        }
        URI keycloakURL = new URI(keycloakConfig.getAuthServerUrl() + "/")
                .resolve("realms/" + keycloakConfig.getRealm() + "/protocol/openid-connect/auth");

        final OAuthFlow implicitFlow = new OAuthFlow().authorizationUrl(keycloakURL.toString());

        SecurityScheme scheme = new SecurityScheme();
        scheme.type(SecurityScheme.Type.OAUTH2)
                .description("This application uses Keycloak oauth authentication")
                .flows(new OAuthFlows().implicit(implicitFlow));
        return scheme;
    } catch (URISyntaxException ex) {
        logger.warn("Failed to parse Keycloak setting", ex);
        return null;
    }
}
 
Example #4
Source File: SecurityParser.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Is empty boolean.
 *
 * @param oAuthFlows the o auth flows
 * @return the boolean
 */
private static boolean isEmpty(io.swagger.v3.oas.annotations.security.OAuthFlows oAuthFlows) {
	boolean result;
	if (oAuthFlows == null)
		result = true;
	else if (!isEmpty(oAuthFlows.implicit()) || !isEmpty(oAuthFlows.authorizationCode()) || !isEmpty(oAuthFlows.clientCredentials()) || !isEmpty(oAuthFlows.password()))
		result = false;
	else result = oAuthFlows.extensions().length <= 0;
	return result;
}
 
Example #5
Source File: SecuritySchemeSerializerTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private SecurityScheme getDummyScheme() {
    SecurityScheme scheme = new SecurityScheme();
    scheme.type(SecurityScheme.Type.HTTP);
    scheme.description("desc");
    scheme.name("name");
    scheme.$ref("ref");
    scheme.in(SecurityScheme.In.COOKIE);
    scheme.scheme("scheme");
    scheme.bearerFormat("format");
    scheme.flows(new OAuthFlows());
    scheme.openIdConnectUrl("url");
    scheme.extensions(Collections.emptyMap());
    return scheme;
}
 
Example #6
Source File: OpenAPIResourceBeanConfigurationComponentsSecuritySchemesTest.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@Bean
public OpenAPI openApi() {
	return new OpenAPI()
			.components(new Components()

					//HTTP Basic, see: https://swagger.io/docs/specification/authentication/basic-authentication/
					.addSecuritySchemes("basicScheme", new SecurityScheme()
							.type(SecurityScheme.Type.HTTP)
							.scheme("basic")
					)

					//API Key, see: https://swagger.io/docs/specification/authentication/api-keys/
					.addSecuritySchemes("apiKeyScheme", new SecurityScheme()
							.type(SecurityScheme.Type.APIKEY)
							.in(SecurityScheme.In.HEADER)
							.name("X-API-KEY")
					)

					//OAuth 2.0, see: https://swagger.io/docs/specification/authentication/oauth2/
					.addSecuritySchemes("oAuthScheme", new SecurityScheme()
							.type(SecurityScheme.Type.OAUTH2)
							.description("This API uses OAuth 2 with the implicit grant flow. [More info](https://api.example.com/docs/auth)")
							.flows(new OAuthFlows()
									.implicit(new OAuthFlow()
											.authorizationUrl("https://api.example.com/oauth2/authorize")
											.scopes(new Scopes()
													.addString("read_pets", "read your pets")
													.addString("write_pets", "modify pets in your account")
											)
									)
							)
					)
			)
			.addSecurityItem(new SecurityRequirement()
					.addList("basicScheme")
			)
			.addSecurityItem(new SecurityRequirement()
					.addList("apiKeyScheme")
			)
			.addSecurityItem(new SecurityRequirement()
					.addList("oAuthScheme")
			)
			;
}
 
Example #7
Source File: ChangedOAuthFlows.java    From openapi-diff with Apache License 2.0 4 votes vote down vote up
public ChangedOAuthFlows(OAuthFlows oldOAuthFlows, OAuthFlows newOAuthFlows) {
  this.oldOAuthFlows = oldOAuthFlows;
  this.newOAuthFlows = newOAuthFlows;
}
 
Example #8
Source File: OAuthFlowsDiff.java    From openapi-diff with Apache License 2.0 4 votes vote down vote up
private static Map<String, Object> getExtensions(OAuthFlows oAuthFlow) {
  return ofNullable(oAuthFlow).map(OAuthFlows::getExtensions).orElse(null);
}
 
Example #9
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #10
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public OAuthFlows getOAuthFlows(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }

    OAuthFlows oAuthFlows = new OAuthFlows();

    ObjectNode objectNode = getObject("implicit", node, false, location, result);
    if(objectNode!= null) {
        oAuthFlows.setImplicit(getOAuthFlow("implicit", objectNode, location, result));
    }

    objectNode = getObject("password", node, false, location, result);
    if(objectNode!= null) {
        oAuthFlows.setPassword(getOAuthFlow("password", objectNode, location, result));
    }

    objectNode = getObject("clientCredentials", node, false, location, result);
    if(objectNode!= null) {
        oAuthFlows.setClientCredentials(getOAuthFlow("clientCredentials", objectNode, location, result));
    }

    objectNode = getObject("authorizationCode", node, false, location, result);
    if(objectNode!= null) {
        oAuthFlows.setAuthorizationCode(getOAuthFlow("authorizationCode", objectNode, location, result));
    }

    Map <String,Object> extensions = getExtensions(node);
    if(extensions != null && extensions.size() > 0) {
        oAuthFlows.setExtensions(extensions);
    }

    Set<String> oAuthFlowKeys = getKeys(node);
    for(String key : oAuthFlowKeys) {
        if(!OAUTHFLOWS_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }


    return oAuthFlows;
}
 
Example #11
Source File: OAS3Parser.java    From carbon-apimgt with Apache License 2.0 3 votes vote down vote up
/**
 * This is to avoid removing the `scopes` field of default security scheme when there are no scopes present. This
 * will set an empty scope object there.
 *
 *   securitySchemes:
 *     default:
 *       type: oauth2
 *       flows:
 *         implicit:
 *           authorizationUrl: 'https://test.com'
 *           scopes: {}
 *           x-scopes-bindings: {}
 *
 * @param swagger OpenAPI object
 */
private void checkAndSetEmptyScope(OpenAPI swagger) {
    Components comp = swagger.getComponents();
    Map<String, SecurityScheme> securitySchemeMap;
    SecurityScheme securityScheme;
    OAuthFlows oAuthFlows;
    OAuthFlow implicitFlow;
    if (comp != null && (securitySchemeMap = comp.getSecuritySchemes()) != null &&
            (securityScheme = securitySchemeMap.get(OPENAPI_SECURITY_SCHEMA_KEY)) != null &&
            (oAuthFlows = securityScheme.getFlows()) != null &&
            (implicitFlow = oAuthFlows.getImplicit()) != null && implicitFlow.getScopes() == null) {
        implicitFlow.setScopes(new Scopes());
    }
}