Search in sources :

Example 16 with ApplicationKeyDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method getApplicationKeyByAppIDAndKeyMapping.

/**
 * Returns Keys of an application by key type
 *
 * @param applicationId Application Id
 * @param keyMappingId       Key Mapping ID
 * @return Application Key Information
 */
private ApplicationKeyDTO getApplicationKeyByAppIDAndKeyMapping(String applicationId, String keyMappingId) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getLightweightApplicationByUUID(applicationId);
        if (application != null) {
            APIKey apiKey = apiConsumer.getApplicationKeyByAppIDAndKeyMapping(application.getId(), keyMappingId);
            if (apiKey != null) {
                return ApplicationKeyMappingUtil.fromApplicationKeyToDTO(apiKey);
            }
        } else {
            log.error("Application not found with ID: " + applicationId);
        }
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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)

Example 17 with ApplicationKeyDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdOauthKeysKeyMappingIdPut.

@Override
public Response applicationsApplicationIdOauthKeysKeyMappingIdPut(String applicationId, String keyMappingId, ApplicationKeyDTO body, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
    Application application = apiConsumer.getApplicationByUUID(applicationId);
    if (application != null) {
        ApplicationKeyDTO appKey = getApplicationKeyByAppIDAndKeyMapping(applicationId, keyMappingId);
        if (RestAPIStoreUtils.isUserOwnerOfApplication(application) && appKey != null) {
            String grantTypes = StringUtils.join(body.getSupportedGrantTypes(), ',');
            JsonObject jsonParams = new JsonObject();
            jsonParams.addProperty(APIConstants.JSON_GRANT_TYPES, grantTypes);
            jsonParams.addProperty(APIConstants.JSON_USERNAME, username);
            if (body.getAdditionalProperties() != null) {
                if (body.getAdditionalProperties() instanceof String && StringUtils.isNotEmpty((String) body.getAdditionalProperties())) {
                    jsonParams.addProperty(APIConstants.JSON_ADDITIONAL_PROPERTIES, (String) body.getAdditionalProperties());
                } else if (body.getAdditionalProperties() instanceof Map) {
                    String jsonContent = new Gson().toJson(body.getAdditionalProperties());
                    jsonParams.addProperty(APIConstants.JSON_ADDITIONAL_PROPERTIES, jsonContent);
                }
            }
            OAuthApplicationInfo updatedData = apiConsumer.updateAuthClient(username, application, appKey.getKeyType().value(), body.getCallbackUrl(), null, null, null, body.getGroupId(), new Gson().toJson(jsonParams), appKey.getKeyManager());
            ApplicationKeyDTO applicationKeyDTO = new ApplicationKeyDTO();
            applicationKeyDTO.setCallbackUrl(updatedData.getCallBackURL());
            JsonObject json = new Gson().fromJson(updatedData.getJsonString(), JsonObject.class);
            if (json.get(APIConstants.JSON_GRANT_TYPES) != null) {
                String[] updatedGrantTypes = json.get(APIConstants.JSON_GRANT_TYPES).getAsString().split(" ");
                applicationKeyDTO.setSupportedGrantTypes(Arrays.asList(updatedGrantTypes));
            }
            applicationKeyDTO.setConsumerKey(updatedData.getClientId());
            applicationKeyDTO.setConsumerSecret(updatedData.getClientSecret());
            applicationKeyDTO.setKeyType(appKey.getKeyType());
            Object additionalProperties = updatedData.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES);
            if (additionalProperties != null) {
                applicationKeyDTO.setAdditionalProperties(additionalProperties);
            }
            applicationKeyDTO.setKeyMappingId(body.getKeyMappingId());
            applicationKeyDTO.setKeyManager(body.getKeyManager());
            return Response.ok().entity(applicationKeyDTO).build();
        } else {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } else {
        RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
    }
    return null;
}
Also used : ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) JsonObject(com.google.gson.JsonObject) 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)

Example 18 with ApplicationKeyDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdKeysGet.

/**
 * Retrieve all keys of an application
 *
 * @param applicationId Application Id
 * @return Application Key Information list
 */
@Override
public Response applicationsApplicationIdKeysGet(String applicationId, MessageContext messageContext) {
    Set<APIKey> applicationKeys = getApplicationKeys(applicationId);
    List<ApplicationKeyDTO> keyDTOList = new ArrayList<>();
    ApplicationKeyListDTO applicationKeyListDTO = new ApplicationKeyListDTO();
    applicationKeyListDTO.setCount(0);
    if (applicationKeys != null) {
        for (APIKey apiKey : applicationKeys) {
            ApplicationKeyDTO appKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(apiKey);
            keyDTOList.add(appKeyDTO);
        }
        applicationKeyListDTO.setList(keyDTOList);
        applicationKeyListDTO.setCount(keyDTOList.size());
    }
    return Response.ok().entity(applicationKeyListDTO).build();
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) ArrayList(java.util.ArrayList) ApplicationKeyListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyListDTO)

Aggregations

ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)14 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)9 Application (org.wso2.carbon.apimgt.api.model.Application)8 APIKey (org.wso2.carbon.apimgt.api.model.APIKey)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 JSONObject (org.json.simple.JSONObject)5 Gson (com.google.gson.Gson)4 JsonObject (com.google.gson.JsonObject)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ApplicationTokenDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO)4 ParseException (org.json.simple.parser.ParseException)3 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)2 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)2 ApplicationKeyListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyListDTO)2 JsonElement (com.google.gson.JsonElement)1