Search in sources :

Example 6 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class AbstractKeyManagerTestCase method testCanHandleTokenWithConfigurationJWTAndOpaueNegative.

@Test
public void testCanHandleTokenWithConfigurationJWTAndOpaueNegative() throws APIManagementException {
    KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
    keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[{\"enable\": true,\"type\": \"JWT\",\"value\": {\"body\": {\"iss\": \"https://localhost:9443\"}}}," + "{\"enable\": true,\"type\": \"REFERENCE\",\"value\": \"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0" + "-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\"}]");
    KeyManager keyManager = new ModelKeyManagerForTest();
    keyManager.loadConfiguration(keyManagerConfiguration);
    assertFalse(keyManager.canHandleToken("avffr.erwrwrwr.ergrtyttwre"));
}
Also used : ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Test(org.junit.Test) ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testRenewConsumerSecret.

@Test
public void testRenewConsumerSecret() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
    String clientId = UUID.randomUUID().toString();
    apiConsumer.apiMgtDAO = apiMgtDAO;
    KeyManagerConfigurationDTO keyManagerConfiguration = new KeyManagerConfigurationDTO();
    keyManagerConfiguration.setEnabled(true);
    Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName(Mockito.anyString(), Mockito.anyString())).thenReturn(keyManagerConfiguration);
    Mockito.when(keyManager.getNewApplicationConsumerSecret(Mockito.anyObject())).thenReturn("updatedClientSecret");
    assertNotNull(apiConsumer.renewConsumerSecret(clientId, APIConstants.KeyManager.DEFAULT_KEY_MANAGER));
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class APIConsumerImpl method updateAuthClient.

/**
 * @param userId Subscriber name.
 * @param application The Application.
 * @param tokenType Token type (PRODUCTION | SANDBOX)
 * @param callbackUrl callback URL
 * @param allowedDomains allowedDomains for token.
 * @param validityTime validity time period.
 * @param tokenScope Scopes for the requested tokens.
 * @param groupingId APIM application id.
 * @param jsonString Callback URL for the Application.
 * @param keyManagerID Key Manager ID of the relevant Key Manager
 * @return
 * @throws APIManagementException
 */
@Override
public OAuthApplicationInfo updateAuthClient(String userId, Application application, String tokenType, String callbackUrl, String[] allowedDomains, String validityTime, String tokenScope, String groupingId, String jsonString, String keyManagerID) throws APIManagementException {
    boolean tenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
            tenantFlowStarted = true;
        }
        final String subscriberName = application.getSubscriber().getName();
        boolean isCaseInsensitiveComparisons = Boolean.parseBoolean(getAPIManagerConfiguration().getFirstProperty(APIConstants.API_STORE_FORCE_CI_COMPARISIONS));
        boolean isUserAppOwner;
        if (isCaseInsensitiveComparisons) {
            isUserAppOwner = subscriberName.equalsIgnoreCase(userId);
        } else {
            isUserAppOwner = subscriberName.equals(userId);
        }
        if (!isUserAppOwner) {
            throw new APIManagementException("user: " + userId + ", attempted to update OAuth application " + "owned by: " + subscriberName);
        }
        String keyManagerName;
        KeyManagerConfigurationDTO keyManagerConfiguration = apiMgtDAO.getKeyManagerConfigurationByUUID(keyManagerID);
        String keyManagerTenant;
        if (keyManagerConfiguration != null) {
            keyManagerName = keyManagerConfiguration.getName();
            keyManagerTenant = keyManagerConfiguration.getOrganization();
        } else {
            // keeping this just in case the name is sent by mistake.
            keyManagerConfiguration = apiMgtDAO.getKeyManagerConfigurationByName(tenantDomain, keyManagerID);
            if (keyManagerConfiguration == null) {
                throw new APIManagementException("Key Manager " + keyManagerID + " couldn't found.", ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
            } else {
                keyManagerName = keyManagerID;
                keyManagerID = keyManagerConfiguration.getUuid();
                keyManagerTenant = keyManagerConfiguration.getOrganization();
            }
        }
        if (!keyManagerConfiguration.isEnabled()) {
            throw new APIManagementException("Key Manager " + keyManagerName + " not activated in the requested " + "Tenant", ExceptionCodes.KEY_MANAGER_NOT_ENABLED);
        }
        if (KeyManagerConfiguration.TokenType.EXCHANGED.toString().equals(keyManagerConfiguration.getTokenType())) {
            throw new APIManagementException("Key Manager " + keyManagerName + " doesn't support to generate" + " Client Application", ExceptionCodes.KEY_MANAGER_NOT_SUPPORTED_TOKEN_GENERATION);
        }
        // Create OauthAppRequest object by passing json String.
        OAuthAppRequest oauthAppRequest = ApplicationUtils.createOauthAppRequest(application.getName(), null, callbackUrl, tokenScope, jsonString, application.getTokenType(), keyManagerTenant, keyManagerName);
        oauthAppRequest.getOAuthApplicationInfo().addParameter(ApplicationConstants.APP_KEY_TYPE, tokenType);
        String consumerKey = apiMgtDAO.getConsumerKeyByApplicationIdKeyTypeKeyManager(application.getId(), tokenType, keyManagerID);
        oauthAppRequest.getOAuthApplicationInfo().setClientId(consumerKey);
        // get key manager instance.
        KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(keyManagerTenant, keyManagerName);
        if (keyManager == null) {
            throw new APIManagementException("Key Manager " + keyManagerName + " not initialized in the requested" + "Tenant", ExceptionCodes.KEY_MANAGER_INITIALIZATION_FAILED);
        }
        // set application attributes
        oauthAppRequest.getOAuthApplicationInfo().putAllAppAttributes(application.getApplicationAttributes());
        oauthAppRequest.getOAuthApplicationInfo().setApplicationUUID(application.getUUID());
        // call update method.
        OAuthApplicationInfo updatedAppInfo = keyManager.updateApplication(oauthAppRequest);
        apiMgtDAO.updateApplicationKeyTypeMetaData(application.getId(), tokenType, keyManagerID, updatedAppInfo);
        JSONObject appLogObject = new JSONObject();
        appLogObject.put(APIConstants.AuditLogConstants.APPLICATION_NAME, updatedAppInfo.getClientName());
        appLogObject.put("Updated Oauth app with Call back URL", callbackUrl);
        appLogObject.put("Updated Oauth app with grant types", jsonString);
        APIUtil.logAuditMessage(APIConstants.AuditLogConstants.APPLICATION, appLogObject.toString(), APIConstants.AuditLogConstants.UPDATED, this.username);
        return updatedAppInfo;
    } finally {
        if (tenantFlowStarted) {
            endTenantFlow();
        }
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 9 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class APIAdminImpl method getKeyManagerConfigurationByName.

@Override
public KeyManagerConfigurationDTO getKeyManagerConfigurationByName(String organization, String name) throws APIManagementException {
    KeyManagerConfigurationDTO keyManagerConfiguration = apiMgtDAO.getKeyManagerConfigurationByName(organization, name);
    if (keyManagerConfiguration != null && APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(keyManagerConfiguration.getName())) {
        APIUtil.getAndSetDefaultKeyManagerConfiguration(keyManagerConfiguration);
    }
    maskValues(keyManagerConfiguration);
    if (!StringUtils.equals(KeyManagerConfiguration.TokenType.EXCHANGED.toString(), keyManagerConfiguration.getTokenType())) {
        getKeyManagerEndpoints(keyManagerConfiguration);
    }
    return keyManagerConfiguration;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)

Example 10 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class APIAdminImpl method getAllKeyManagerConfigurations.

@Override
public Map<String, List<KeyManagerConfigurationDTO>> getAllKeyManagerConfigurations() throws APIManagementException {
    List<KeyManagerConfigurationDTO> keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurations();
    Map<String, List<KeyManagerConfigurationDTO>> keyManagerConfigurationsByTenant = new HashMap<>();
    for (KeyManagerConfigurationDTO keyManagerConfiguration : keyManagerConfigurations) {
        List<KeyManagerConfigurationDTO> keyManagerConfigurationDTOS;
        if (keyManagerConfigurationsByTenant.containsKey(keyManagerConfiguration.getOrganization())) {
            keyManagerConfigurationDTOS = keyManagerConfigurationsByTenant.get(keyManagerConfiguration.getOrganization());
        } else {
            keyManagerConfigurationDTOS = new ArrayList<>();
        }
        if (APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(keyManagerConfiguration.getName())) {
            APIUtil.getAndSetDefaultKeyManagerConfiguration(keyManagerConfiguration);
        }
        keyManagerConfigurationDTOS.add(keyManagerConfiguration);
        keyManagerConfigurationsByTenant.put(keyManagerConfiguration.getOrganization(), keyManagerConfigurationDTOS);
    }
    return keyManagerConfigurationsByTenant;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)14 KeyManagerConfiguration (org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration)14 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)12 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 ModelKeyManagerForTest (org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest)8 HashMap (java.util.HashMap)7 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)7 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)7 JSONObject (org.json.simple.JSONObject)4 KeyMgtConfigurations (org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations)4 Gson (com.google.gson.Gson)3 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)3 AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)3 Application (org.wso2.carbon.apimgt.api.model.Application)3 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2