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;
}
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();
}
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;
}
Aggregations