Search in sources :

Example 1 with APIKeyDTO

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdApiKeysKeyTypeGeneratePost.

@Override
public Response applicationsApplicationIdApiKeysKeyTypeGeneratePost(String applicationId, String keyType, String ifMatch, APIKeyGenerateRequestDTO body, MessageContext messageContext) {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    Application application;
    int validityPeriod;
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(userName);
        if ((application = apiConsumer.getApplicationByUUID(applicationId)) == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        } else {
            if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            } else {
                if (APIConstants.API_KEY_TYPE_PRODUCTION.equalsIgnoreCase(keyType)) {
                    application.setKeyType(APIConstants.API_KEY_TYPE_PRODUCTION);
                } else if (APIConstants.API_KEY_TYPE_SANDBOX.equalsIgnoreCase(keyType)) {
                    application.setKeyType(APIConstants.API_KEY_TYPE_SANDBOX);
                } else {
                    RestApiUtil.handleBadRequest("Invalid keyType. KeyType should be either PRODUCTION or SANDBOX", log);
                }
                if (body != null && body.getValidityPeriod() != null && body.getValidityPeriod() > 0) {
                    validityPeriod = body.getValidityPeriod();
                } else {
                    validityPeriod = -1;
                }
                String restrictedIP = null;
                String restrictedReferer = null;
                if (body.getAdditionalProperties() != null) {
                    Map additionalProperties = (HashMap) body.getAdditionalProperties();
                    if (additionalProperties.get(APIConstants.JwtTokenConstants.PERMITTED_IP) != null) {
                        restrictedIP = (String) additionalProperties.get(APIConstants.JwtTokenConstants.PERMITTED_IP);
                    }
                    if (additionalProperties.get(APIConstants.JwtTokenConstants.PERMITTED_REFERER) != null) {
                        restrictedReferer = (String) additionalProperties.get(APIConstants.JwtTokenConstants.PERMITTED_REFERER);
                    }
                }
                String apiKey = apiConsumer.generateApiKey(application, userName, validityPeriod, restrictedIP, restrictedReferer);
                APIKeyDTO apiKeyDto = ApplicationKeyMappingUtil.formApiKeyToDTO(apiKey, validityPeriod);
                return Response.ok().entity(apiKeyDto).build();
            }
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while generatig API Keys for application " + applicationId, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HashMap(java.util.HashMap) 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) APIKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIKeyDTO)

Example 2 with APIKeyDTO

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

the class ApisApiServiceImpl method generateInternalAPIKey.

@Override
public Response generateInternalAPIKey(String apiId, MessageContext messageContext) throws APIManagementException {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(userName);
    String token = apiProvider.generateApiKey(apiId);
    APIKeyDTO apiKeyDTO = new APIKeyDTO();
    apiKeyDTO.setApikey(token);
    apiKeyDTO.setValidityTime(60 * 1000);
    return Response.ok().entity(apiKeyDTO).build();
}
Also used : APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIKeyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIKeyDTO)

Example 3 with APIKeyDTO

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

the class ApplicationKeyMappingUtil method formApiKeyToDTO.

public static APIKeyDTO formApiKeyToDTO(String apiKey, int validityTime) {
    APIKeyDTO apiKeyDto = new APIKeyDTO();
    apiKeyDto.setApikey(apiKey);
    apiKeyDto.setValidityTime(validityTime);
    return apiKeyDto;
}
Also used : APIKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIKeyDTO)

Aggregations

APIKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIKeyDTO)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 APIKeyDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIKeyDTO)1 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)1