Search in sources :

Example 6 with ApplicationKeyDTO

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdMapKeysPost.

/**
 * Generate keys using existing consumer key and consumer secret
 *
 * @param applicationId Application id
 * @param body          Contains consumer key, secret and key type information
 * @return A response object containing application keys
 */
@Override
public Response applicationsApplicationIdMapKeysPost(String applicationId, ApplicationKeyMappingRequestDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    JSONObject jsonParamObj = new JSONObject();
    APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
    Application application = apiConsumer.getApplicationByUUID(applicationId);
    String keyManagerName = APIConstants.KeyManager.DEFAULT_KEY_MANAGER;
    if (StringUtils.isNotEmpty(body.getKeyManager())) {
        keyManagerName = body.getKeyManager();
    }
    if (application != null) {
        if (RestAPIStoreUtils.isUserOwnerOfApplication(application)) {
            String clientId = body.getConsumerKey();
            String keyType = body.getKeyType().toString();
            String tokenType = APIConstants.DEFAULT_TOKEN_TYPE;
            jsonParamObj.put(APIConstants.SUBSCRIPTION_KEY_TYPE, body.getKeyType().toString());
            jsonParamObj.put(APIConstants.JSON_CLIENT_SECRET, body.getConsumerSecret());
            String organization = RestApiUtil.getValidatedOrganization(messageContext);
            Map<String, Object> keyDetails = apiConsumer.mapExistingOAuthClient(jsonParamObj.toJSONString(), username, clientId, application.getName(), keyType, tokenType, keyManagerName, organization);
            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);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) 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)

Example 7 with ApplicationKeyDTO

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdOauthKeysGet.

@Override
public Response applicationsApplicationIdOauthKeysGet(String applicationId, String xWso2Tenant, MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    Set<APIKey> applicationKeys = getApplicationKeys(applicationId, organization);
    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)

Example 8 with ApplicationKeyDTO

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

the class ApplicationMappingUtil method fromApplicationtoDTO.

public static ApplicationDTO fromApplicationtoDTO(Application application) throws APIManagementException {
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(application.getUUID());
    applicationDTO.setThrottlingPolicy(application.getTier());
    applicationDTO.setDescription(application.getDescription());
    Map<String, String> applicationAttributes = application.getApplicationAttributes();
    applicationDTO.setAttributes(applicationAttributes);
    applicationDTO.setName(application.getName());
    applicationDTO.setStatus(application.getStatus());
    applicationDTO.setOwner(application.getOwner());
    if (StringUtils.isNotEmpty(application.getGroupId())) {
        applicationDTO.setGroups(Arrays.asList(application.getGroupId().split(",")));
    }
    applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.OAUTH);
    applicationDTO.setSubscriptionCount(application.getSubscriptionCount());
    if (StringUtils.isNotEmpty(application.getTokenType()) && !APIConstants.DEFAULT_TOKEN_TYPE.equals(application.getTokenType())) {
        applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.valueOf(application.getTokenType()));
    }
    /*List<ApplicationKeyDTO> applicationKeyDTOs = new ArrayList<>();
        for(APIKey apiKey : application.getKeys()) {
            ApplicationKeyDTO applicationKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(apiKey);
            applicationKeyDTOs.add(applicationKeyDTO);
        }
        applicationDTO.setKeys(applicationKeyDTOs);*/
    return applicationDTO;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)

Example 9 with ApplicationKeyDTO

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

the class ImportUtils method getAPIKeyFromApplicationKeyDTO.

/**
 * This extracts information for creating an APIKey from an OAuthApplication
 *
 * @param applicationKeyDto Application Key DTO
 * @return An APIKey containing keys from OAuthApplication
 */
public static APIKey getAPIKeyFromApplicationKeyDTO(ApplicationKeyDTO applicationKeyDto) {
    APIKey apiKey = new APIKey();
    apiKey.setType(String.valueOf(applicationKeyDto.getKeyType()));
    apiKey.setConsumerKey(applicationKeyDto.getConsumerKey());
    apiKey.setConsumerSecret(new String(Base64.decodeBase64(applicationKeyDto.getConsumerSecret())));
    apiKey.setKeyManager(applicationKeyDto.getKeyManager());
    apiKey.setGrantTypes(StringUtils.join(applicationKeyDto.getSupportedGrantTypes(), ", "));
    if (apiKey.getGrantTypes() != null && (apiKey.getGrantTypes().contains(GRANT_TYPE_IMPLICIT) || apiKey.getGrantTypes().contains(GRANT_TYPE_CODE))) {
        apiKey.setCallbackUrl(applicationKeyDto.getCallbackUrl());
    }
    apiKey.setValidityPeriod(applicationKeyDto.getToken().getValidityTime());
    apiKey.setTokenScope(DEFAULT_TOKEN_SCOPE);
    return apiKey;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey)

Example 10 with ApplicationKeyDTO

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

the class ApplicationKeyMappingUtil method fromApplicationKeyToDTO.

/**
 * Insert the application related details to a DTO Object using api key
 *
 * @param apiKey Object that contains details needed during token request
 * @return DTO object with application related details
 */
public static ApplicationKeyDTO fromApplicationKeyToDTO(APIKey apiKey) {
    ApplicationKeyDTO applicationKeyDTO = new ApplicationKeyDTO();
    applicationKeyDTO.setKeyType(ApplicationKeyDTO.KeyTypeEnum.valueOf(apiKey.getType()));
    applicationKeyDTO.setConsumerKey(apiKey.getConsumerKey());
    applicationKeyDTO.setConsumerSecret(apiKey.getConsumerSecret());
    applicationKeyDTO.setKeyState(apiKey.getState());
    applicationKeyDTO.setMode(ApplicationKeyDTO.ModeEnum.valueOf(apiKey.getCreateMode()));
    applicationKeyDTO.setKeyManager(apiKey.getKeyManager());
    applicationKeyDTO.setKeyMappingId(apiKey.getMappingId());
    if (apiKey.getGrantTypes() != null) {
        applicationKeyDTO.setSupportedGrantTypes(Arrays.asList(apiKey.getGrantTypes().split(" ")));
    } else {
        applicationKeyDTO.setSupportedGrantTypes(null);
    }
    applicationKeyDTO.setCallbackUrl(apiKey.getCallbackUrl());
    ApplicationTokenDTO tokenDTO = new ApplicationTokenDTO();
    if (apiKey.getTokenScope() != null) {
        tokenDTO.setTokenScopes(Arrays.asList(apiKey.getTokenScope().split(" ")));
    }
    tokenDTO.setAccessToken(apiKey.getAccessToken());
    tokenDTO.setValidityTime(apiKey.getValidityPeriod());
    applicationKeyDTO.setToken(tokenDTO);
    applicationKeyDTO.setAdditionalProperties(apiKey.getAdditionalProperties());
    return applicationKeyDTO;
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)

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