use of org.wso2.carbon.apimgt.api.model.APIKey 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;
}
use of org.wso2.carbon.apimgt.api.model.APIKey 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;
}
use of org.wso2.carbon.apimgt.api.model.APIKey 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();
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class APIConsumerImpl method revokeAPIKey.
public void revokeAPIKey(String apiKey, long expiryTime, String tenantDomain) throws APIManagementException {
RevocationRequestPublisher revocationRequestPublisher = RevocationRequestPublisher.getInstance();
Properties properties = new Properties();
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
String eventID = UUID.randomUUID().toString();
properties.put(APIConstants.NotificationEvent.EVENT_ID, eventID);
properties.put(APIConstants.NotificationEvent.TOKEN_TYPE, APIConstants.API_KEY_AUTH_TYPE);
properties.put(APIConstants.NotificationEvent.TENANT_ID, tenantId);
properties.put(APIConstants.NotificationEvent.TENANT_DOMAIN, tenantDomain);
ApiMgtDAO.getInstance().addRevokedJWTSignature(eventID, apiKey, APIConstants.API_KEY_AUTH_TYPE, expiryTime, tenantId);
revocationRequestPublisher.publishRevocationEvents(apiKey, expiryTime, properties);
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class APIConsumerImpl method updateApplicationOwner.
public boolean updateApplicationOwner(String userId, String organization, Application application) throws APIManagementException {
boolean isAppUpdated;
String consumerKey;
String oldUserName = application.getSubscriber().getName();
String oldTenantDomain = MultitenantUtils.getTenantDomain(oldUserName);
String newTenantDomain = MultitenantUtils.getTenantDomain(userId);
if (oldTenantDomain.equals(newTenantDomain)) {
if (!isSubscriberValid(userId)) {
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(newTenantDomain);
UserStoreManager userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
if (userStoreManager.isExistingUser(userId)) {
if (apiMgtDAO.getSubscriber(userId) == null) {
addSubscriber(userId, "");
}
} else {
throw new APIManagementException("User " + userId + " doesn't exist in user store");
}
} catch (UserStoreException e) {
throw new APIManagementException("Error while adding user " + userId + " as a subscriber");
}
}
String applicationName = application.getName();
if (!APIUtil.isApplicationOwnedBySubscriber(userId, applicationName, organization)) {
for (APIKey apiKey : application.getKeys()) {
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, apiKey.getKeyManager());
/* retrieving OAuth application information for specific consumer key */
consumerKey = apiKey.getConsumerKey();
OAuthApplicationInfo oAuthApplicationInfo = keyManager.retrieveApplication(consumerKey);
if (oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_NAME) != null) {
OAuthAppRequest oauthAppRequest = ApplicationUtils.createOauthAppRequest(oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_NAME).toString(), null, oAuthApplicationInfo.getCallBackURL(), null, null, application.getTokenType(), this.tenantDomain, apiKey.getKeyManager());
oauthAppRequest.getOAuthApplicationInfo().setAppOwner(userId);
oauthAppRequest.getOAuthApplicationInfo().setClientId(consumerKey);
/* updating the owner of the OAuth application with userId */
OAuthApplicationInfo updatedAppInfo = keyManager.updateApplicationOwner(oauthAppRequest, userId);
isAppUpdated = true;
audit.info("Successfully updated the owner of application " + application.getName() + " from " + oldUserName + " to " + userId + ".");
} else {
throw new APIManagementException("Unable to retrieve OAuth application information.");
}
}
} else {
throw new APIManagementException("Unable to update application owner to " + userId + " as this user has an application with the same name. Update owner to another user.");
}
} else {
throw new APIManagementException("Unable to update application owner to " + userId + " as this user does not belong to " + oldTenantDomain + " domain.");
}
isAppUpdated = apiMgtDAO.updateApplicationOwner(userId, application);
return isAppUpdated;
}
Aggregations