Search in sources :

Example 36 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class JWTGenerator method populateCustomClaims.

@Override
public Map<String, String> populateCustomClaims(TokenValidationContext validationContext) throws APIManagementException {
    APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    JWTConfigurationDto jwtConfigurationDto = apiManagerConfiguration.getJwtConfigurationDto();
    Map<String, String> customClaims = new HashMap<>();
    Map<String, Object> properties = new HashMap<>();
    String username = validationContext.getValidationInfoDTO().getEndUserName();
    int tenantId = APIUtil.getTenantId(username);
    if (jwtConfigurationDto.isEnableUserClaims()) {
        String accessToken = validationContext.getAccessToken();
        if (accessToken != null) {
            properties.put(APIConstants.KeyManager.ACCESS_TOKEN, accessToken);
        }
        String dialectURI = jwtConfigurationDto.getConsumerDialectUri();
        if (!StringUtils.isEmpty(dialectURI)) {
            properties.put(APIConstants.KeyManager.CLAIM_DIALECT, dialectURI);
            String keymanagerName = validationContext.getValidationInfoDTO().getKeyManager();
            KeyManager keymanager = KeyManagerHolder.getKeyManagerInstance(APIUtil.getTenantDomainFromTenantId(tenantId), keymanagerName);
            if (keymanager != null) {
                customClaims = keymanager.getUserClaims(username, properties);
                if (log.isDebugEnabled()) {
                    log.debug("Retrieved claims :" + customClaims);
                }
            }
        }
    }
    ClaimsRetriever claimsRetriever = getClaimsRetriever();
    if (claimsRetriever != null) {
        customClaims.putAll(claimsRetriever.getClaims(username));
    }
    return customClaims;
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) JWTConfigurationDto(org.wso2.carbon.apimgt.common.gateway.dto.JWTConfigurationDto)

Example 37 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class APIAdminImpl method getPolicies.

@Override
public Policy[] getPolicies(int tenantId, String level) throws APIManagementException {
    Policy[] policies = null;
    if (PolicyConstants.POLICY_LEVEL_API.equals(level)) {
        policies = apiMgtDAO.getAPIPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equals(level)) {
        policies = apiMgtDAO.getApplicationPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_SUB.equals(level)) {
        policies = apiMgtDAO.getSubscriptionPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_GLOBAL.equals(level)) {
        policies = apiMgtDAO.getGlobalPolicies(tenantId);
    }
    // Get the API Manager configurations and check whether the unlimited tier is disabled. If disabled, remove
    // the tier from the array.
    APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    ThrottleProperties throttleProperties = apiManagerConfiguration.getThrottleProperties();
    List<Policy> policiesWithoutUnlimitedTier = new ArrayList<Policy>();
    if (policies != null) {
        for (Policy policy : policies) {
            if (APIConstants.UNLIMITED_TIER.equals(policy.getPolicyName())) {
                if (throttleProperties.isEnableUnlimitedTier()) {
                    policiesWithoutUnlimitedTier.add(policy);
                }
            } else {
                policiesWithoutUnlimitedTier.add(policy);
            }
        }
    }
    policies = policiesWithoutUnlimitedTier.toArray(new Policy[0]);
    return policies;
}
Also used : Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) ArrayList(java.util.ArrayList) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 38 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class APIConsumerImpl method getAppAttributesFromConfig.

/**
 * This method is used to get keys of custom attributes, configured by user
 *
 * @param userId user name of logged in user
 * @return Array of JSONObject, contains keys of attributes
 * @throws APIManagementException
 */
public JSONArray getAppAttributesFromConfig(String userId) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(userId);
    int tenantId = 0;
    try {
        tenantId = getTenantId(tenantDomain);
    } catch (UserStoreException e) {
        handleException("Error in getting tenantId of " + tenantDomain, e);
    }
    JSONArray applicationAttributes = null;
    JSONObject applicationConfig = APIUtil.getAppAttributeKeysFromRegistry(tenantDomain);
    if (applicationConfig != null) {
        applicationAttributes = (JSONArray) applicationConfig.get(APIConstants.ApplicationAttributes.ATTRIBUTES);
    } else {
        APIManagerConfiguration configuration = getAPIManagerConfiguration();
        applicationAttributes = configuration.getApplicationAttributes();
    }
    return applicationAttributes;
}
Also used : JSONObject(org.json.simple.JSONObject) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONArray(org.json.simple.JSONArray)

Example 39 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class APIConsumerImpl method changeUserPassword.

/**
 * Change user's password
 *
 * @param currentPassword Current password of the user
 * @param newPassword     New password of the user
 */
@Override
public void changeUserPassword(String currentPassword, String newPassword) throws APIManagementException {
    // check whether EnablePasswordChange configuration is set to 'true'
    APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    boolean enableChangePassword = Boolean.parseBoolean(config.getFirstProperty(APIConstants.ENABLE_CHANGE_PASSWORD));
    if (!enableChangePassword) {
        throw new APIManagementException("Password change operation is disabled in the system", ExceptionCodes.PASSWORD_CHANGE_DISABLED);
    }
    UserAdmin userAdmin = new UserAdmin();
    try {
        userAdmin.changePasswordByUser(userNameWithoutChange, currentPassword, newPassword);
    } catch (UserAdminException e) {
        String genericErrorMessage = "Error occurred while changing the user password";
        if (log.isDebugEnabled()) {
            log.debug(genericErrorMessage, e);
        }
        // filter the exception message
        String exceptionMessage = e.getMessage();
        if (exceptionMessage.matches("(?i:.*\\b(current)\\b.*\\b(password)\\b.*\\b(incorrect)\\b.*)")) {
            String errorMessage = "The current user password entered is incorrect";
            throw new APIManagementException(errorMessage, ExceptionCodes.CURRENT_PASSWORD_INCORRECT);
        } else if ((exceptionMessage.matches("(?i:.*\\b(password)\\b.*\\b(length)\\b.*)")) || (ExceptionUtils.getStackTrace(e).contains("PolicyViolationException"))) {
            String errorMessage = "The new password entered is invalid since it doesn't comply with the password " + "pattern/policy configured";
            throw new APIManagementException(errorMessage, ExceptionCodes.PASSWORD_PATTERN_INVALID);
        } else {
            throw new APIManagementException(genericErrorMessage);
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserAdmin(org.wso2.carbon.user.mgt.UserAdmin) UserAdminException(org.wso2.carbon.user.mgt.common.UserAdminException)

Example 40 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.

the class InboundWebsocketProcessorUtilTest method init.

@Before
public void init() {
    System.setProperty("carbon.home", "jhkjn");
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.mockStatic(ThrottleDataPublisher.class);
    dataPublisher = Mockito.mock(DataPublisher.class);
    ThrottleDataPublisher throttleDataPublisher = Mockito.mock(ThrottleDataPublisher.class);
    Mockito.when(serviceReferenceHolder.getThrottleDataPublisher()).thenReturn(throttleDataPublisher);
    PowerMockito.when(ThrottleDataPublisher.getDataPublisher()).thenReturn(dataPublisher);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(serviceReferenceHolder.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    PowerMockito.mockStatic(WebsocketUtil.class);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ThrottleDataPublisher(org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher) ThrottleDataPublisher(org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher) DataPublisher(org.wso2.carbon.databridge.agent.DataPublisher) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Before(org.junit.Before)

Aggregations

APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)122 Test (org.junit.Test)76 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)67 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)51 HashMap (java.util.HashMap)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)36 RealmService (org.wso2.carbon.user.core.service.RealmService)33 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)31 ArrayList (java.util.ArrayList)26 API (org.wso2.carbon.apimgt.api.model.API)25 Before (org.junit.Before)24 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)24 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)24 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)23 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)20 Cache (javax.cache.Cache)19 Map (java.util.Map)18