Java Code Examples for io.swagger.models.auth.OAuth2Definition#getVendorExtensions()

The following examples show how to use io.swagger.models.auth.OAuth2Definition#getVendorExtensions() . 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: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the oauth scopes according to the given swagger
 *
 * @param resourceConfigsJSON resource json
 * @return scope set
 * @throws APIManagementException
 */
@Override
public Set<Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    Swagger swagger = getSwagger(resourceConfigsJSON);
    String oauth2SchemeKey = getOAuth2SecuritySchemeKey(swagger);

    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    OAuth2Definition oAuth2Definition;
    if (securityDefinitions != null
            && (oAuth2Definition = (OAuth2Definition) securityDefinitions.get(oauth2SchemeKey)) != null
            && oAuth2Definition.getScopes() != null) {
        Set<Scope> scopeSet = new LinkedHashSet<>();
        for (Map.Entry<String, String> entry : oAuth2Definition.getScopes().entrySet()) {
            Scope scope = new Scope();
            scope.setKey(entry.getKey());
            scope.setName(entry.getKey());
            scope.setDescription(entry.getValue());
            Map<String, String> scopeBindings;
            if (oAuth2Definition.getVendorExtensions() != null && (scopeBindings =
                    (Map<String, String>) oAuth2Definition.getVendorExtensions()
                            .get(APIConstants.SWAGGER_X_SCOPES_BINDINGS)) != null) {
                if (scopeBindings.get(scope.getKey()) != null) {
                    scope.setRoles(scopeBindings.get(scope.getKey()));
                }
            }
            scopeSet.add(scope);
        }
        return OASParserUtil.sortScopes(scopeSet);
    } else {
        return OASParserUtil.sortScopes(getScopesFromExtensions(swagger));
    }
}
 
Example 2
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns the oauth scopes according to the given swagger(version 2)
 *
 * @param swagger resource json
 * @return Swagger
 * @throws APIManagementException
 */
private Swagger injectOtherScopesToDefaultScheme(Swagger swagger) throws APIManagementException {
    //Get security definitions from swagger
    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    List<String> otherSetOfSchemes = new ArrayList<>();
    Map<String, String> defaultScopeBindings = null;
    if (securityDefinitions != null) {
        //If there is no default type schemes set a one
        OAuth2Definition newDefault = new OAuth2Definition();
        securityDefinitions.put(SWAGGER_SECURITY_SCHEMA_KEY, newDefault);
        //Check all the security definitions
        for (Map.Entry<String, SecuritySchemeDefinition> definition : securityDefinitions.entrySet()) {
            String checkType = definition.getValue().getType();
            //Inject other scheme scopes into default scope
            if (!SWAGGER_SECURITY_SCHEMA_KEY.equals(definition.getKey()) && "oauth2".equals(checkType)) {
                //Add non default scopes to other scopes list
                otherSetOfSchemes.add(definition.getKey());
                //Check for default one
                OAuth2Definition noneDefaultFlowType = (OAuth2Definition) definition.getValue();
                OAuth2Definition defaultTypeFlow = (OAuth2Definition) securityDefinitions.get(SWAGGER_SECURITY_SCHEMA_KEY);
                Map<String, String> noneDefaultFlowScopes = noneDefaultFlowType.getScopes();
                Map<String, String> defaultTypeScopes = defaultTypeFlow.getScopes();
                if (defaultTypeScopes == null) {
                    defaultTypeScopes = new HashMap<>();
                }
                for (Map.Entry<String, String> input : noneDefaultFlowScopes.entrySet()) {
                    defaultTypeScopes.put(input.getKey(), input.getValue());
                }
                defaultTypeFlow.setScopes(defaultTypeScopes);
                //Check X-Scope Bindings
                Map<String, String> noneDefaultScopeBindings = null;
                Map<String, Object> defaultTypeExtension = defaultTypeFlow.getVendorExtensions();
                if (noneDefaultFlowType.getVendorExtensions() != null && (noneDefaultScopeBindings =
                        (Map<String, String>) noneDefaultFlowType.getVendorExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS))
                        != null) {
                    if (defaultScopeBindings == null) {
                        defaultScopeBindings = new HashMap<>();
                    }
                    //Inject non default scope bindings into default scheme
                    for (Map.Entry<String, String> roleInUse : noneDefaultScopeBindings.entrySet()) {
                        defaultScopeBindings.put(roleInUse.getKey(), roleInUse.getValue());
                    }
                }
                defaultTypeExtension.put(APIConstants.SWAGGER_X_SCOPES_BINDINGS, defaultScopeBindings);
                defaultTypeFlow.setVendorExtensions(defaultTypeExtension);
                securityDefinitions.put(SWAGGER_SECURITY_SCHEMA_KEY, defaultTypeFlow);
            }
        }
        //update list of security schemes in the swagger object
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    setOtherSchemes(otherSetOfSchemes);
    return swagger;
}