Search in sources :

Example 1 with EmptyCallbackURLForCodeGrantsException

use of org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException in project carbon-apimgt by wso2.

the class AbstractKeyManager method buildFromJSON.

/**
 * This method will accept json String and will do the json parse will set oAuth application properties to OAuthApplicationInfo object.
 *
 * @param jsonInput this jsonInput will contain set of oAuth application properties.
 * @return OAuthApplicationInfo object will be return.
 * @throws APIManagementException
 */
public OAuthApplicationInfo buildFromJSON(OAuthApplicationInfo oAuthApplicationInfo, String jsonInput) throws APIManagementException {
    // initiate json parser.
    JSONParser parser = new JSONParser();
    JSONObject jsonObject;
    try {
        // parse json String
        jsonObject = (JSONObject) parser.parse(jsonInput);
        if (jsonObject != null) {
            // create a map to hold json parsed objects.
            Map<String, Object> params = (Map) jsonObject;
            if (params.get(APIConstants.JSON_CALLBACK_URL) != null) {
                oAuthApplicationInfo.setCallBackURL((String) params.get(APIConstants.JSON_CALLBACK_URL));
            }
            if (params.get(APIConstants.JSON_GRANT_TYPES) != null) {
                String grantTypeString = params.get(APIConstants.JSON_GRANT_TYPES).toString();
                if (StringUtils.isEmpty(oAuthApplicationInfo.getCallBackURL()) && (grantTypeString.contains("implicit") || grantTypeString.contains("authorization_code"))) {
                    throw new EmptyCallbackURLForCodeGrantsException("The callback url must have at least one URI " + "value when using Authorization code or implicit grant types.");
                }
            }
            // set client Id
            if (params.get(APIConstants.JSON_CLIENT_ID) != null) {
                oAuthApplicationInfo.setClientId((String) params.get(APIConstants.JSON_CLIENT_ID));
            }
            // set client secret
            if (params.get(APIConstants.JSON_CLIENT_SECRET) != null) {
                oAuthApplicationInfo.setClientSecret((String) params.get(APIConstants.JSON_CLIENT_SECRET));
            }
            // copy all params map in to OAuthApplicationInfo's Map object.
            oAuthApplicationInfo.putAll(params);
            validateOAuthAppCreationProperties(oAuthApplicationInfo);
            return oAuthApplicationInfo;
        }
    } catch (ParseException e) {
        handleException("Error occurred while parsing JSON String", e);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) EmptyCallbackURLForCodeGrantsException(org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException)

Example 2 with EmptyCallbackURLForCodeGrantsException

use of org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdGenerateKeysPost.

/**
 * Generate keys for a application
 *
 * @param applicationId     application identifier
 * @param body              request body
 * @return A response object containing application keys
 */
@Override
public Response applicationsApplicationIdGenerateKeysPost(String applicationId, ApplicationKeyGenerateRequestDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getApplicationByUUID(applicationId);
        if (application != null) {
            if (RestAPIStoreUtils.isUserOwnerOfApplication(application)) {
                String[] accessAllowDomainsArray = { "ALL" };
                JSONObject jsonParamObj = new JSONObject();
                jsonParamObj.put(ApplicationConstants.OAUTH_CLIENT_USERNAME, username);
                String grantTypes = StringUtils.join(body.getGrantTypesToBeSupported(), ',');
                if (!StringUtils.isEmpty(grantTypes)) {
                    jsonParamObj.put(APIConstants.JSON_GRANT_TYPES, grantTypes);
                }
                /* Read clientId & clientSecret from ApplicationKeyGenerateRequestDTO object.
                       User can provide clientId only or both clientId and clientSecret
                       User cannot provide clientSecret only */
                if (!StringUtils.isEmpty(body.getClientId())) {
                    jsonParamObj.put(APIConstants.JSON_CLIENT_ID, body.getClientId());
                    if (!StringUtils.isEmpty(body.getClientSecret())) {
                        jsonParamObj.put(APIConstants.JSON_CLIENT_SECRET, body.getClientSecret());
                    }
                }
                if (body.getAdditionalProperties() != null) {
                    if (body.getAdditionalProperties() instanceof String && StringUtils.isNotEmpty((String) body.getAdditionalProperties())) {
                        jsonParamObj.put(APIConstants.JSON_ADDITIONAL_PROPERTIES, body.getAdditionalProperties());
                    } else if (body.getAdditionalProperties() instanceof Map) {
                        String jsonContent = new Gson().toJson(body.getAdditionalProperties());
                        jsonParamObj.put(APIConstants.JSON_ADDITIONAL_PROPERTIES, jsonContent);
                    }
                }
                String jsonParams = jsonParamObj.toString();
                String tokenScopes = StringUtils.join(body.getScopes(), " ");
                String keyManagerName = APIConstants.KeyManager.DEFAULT_KEY_MANAGER;
                if (StringUtils.isNotEmpty(body.getKeyManager())) {
                    keyManagerName = body.getKeyManager();
                }
                String organization = RestApiUtil.getValidatedOrganization(messageContext);
                Map<String, Object> keyDetails = apiConsumer.requestApprovalForApplicationRegistration(username, application, body.getKeyType().toString(), body.getCallbackUrl(), accessAllowDomainsArray, body.getValidityTime(), tokenScopes, jsonParams, keyManagerName, organization, false);
                ApplicationKeyDTO applicationKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(keyDetails, body.getKeyType().toString());
                applicationKeyDTO.setKeyManager(keyManagerName);
                return Response.ok().entity(applicationKeyDTO).build();
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } catch (EmptyCallbackURLForCodeGrantsException e) {
        RestApiUtil.handleBadRequest(e.getMessage(), log);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) Map(java.util.Map) HashMap(java.util.HashMap) EmptyCallbackURLForCodeGrantsException(org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException)

Aggregations

JsonObject (com.google.gson.JsonObject)2 Map (java.util.Map)2 JSONObject (org.json.simple.JSONObject)2 EmptyCallbackURLForCodeGrantsException (org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException)2 Gson (com.google.gson.Gson)1 HashMap (java.util.HashMap)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)1 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)1