Search in sources :

Example 61 with APIKey

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;
}
Also used : APIKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIKeyDTO)

Example 62 with APIKey

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;
}
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 63 with APIKey

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();
}
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 64 with APIKey

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);
}
Also used : RevocationRequestPublisher(org.wso2.carbon.apimgt.impl.publishers.RevocationRequestPublisher) Properties(java.util.Properties)

Example 65 with APIKey

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;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) RealmService(org.wso2.carbon.user.core.service.RealmService) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Aggregations

APIKey (org.wso2.carbon.apimgt.api.model.APIKey)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)14 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)14 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 Test (org.testng.annotations.Test)11 Application (org.wso2.carbon.apimgt.api.model.Application)10 Cache (javax.cache.Cache)9 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)9 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)9 HttpResponse (org.wso2.micro.gateway.tests.util.HttpResponse)9 HashSet (java.util.HashSet)7 APIKeyDataStore (org.wso2.carbon.apimgt.gateway.handlers.security.keys.APIKeyDataStore)7 WSAPIKeyDataStore (org.wso2.carbon.apimgt.gateway.handlers.security.keys.WSAPIKeyDataStore)7 ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)6 IOException (java.io.IOException)5 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)5 LinkedHashSet (java.util.LinkedHashSet)4