Search in sources :

Example 6 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class SelfSignupUtilTestCase method testIsUserNameWithAllowedDomainNameWhenDomainNotGiven.

@Test
public void testIsUserNameWithAllowedDomainNameWhenDomainNotGiven() throws Exception {
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    RealmConfiguration realmConfiguration = new RealmConfiguration();
    realmConfiguration.addRestrictedDomainForSelfSignUp("foo.com");
    Mockito.when(userRealm.getRealmConfiguration()).thenReturn(realmConfiguration);
    boolean result = SelfSignUpUtil.isUserNameWithAllowedDomainName("john", userRealm);
    Assert.assertTrue(result);
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) UserRealm(org.wso2.carbon.user.core.UserRealm) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class SelfSignupUtilTestCase method testIsUserNameWithAllowedDomainNameFalse.

@Test
public void testIsUserNameWithAllowedDomainNameFalse() throws Exception {
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    RealmConfiguration realmConfiguration = new RealmConfiguration();
    realmConfiguration.addRestrictedDomainForSelfSignUp("bar.com");
    Mockito.when(userRealm.getRealmConfiguration()).thenReturn(realmConfiguration);
    boolean result = SelfSignUpUtil.isUserNameWithAllowedDomainName("bar.com/john", userRealm);
    Assert.assertFalse(result);
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) UserRealm(org.wso2.carbon.user.core.UserRealm) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class APIManagerComponentPermissionsTest method testShouldAddApplicationPermissionsToRegistry.

@Test
public void testShouldAddApplicationPermissionsToRegistry() throws Exception {
    RealmConfiguration realmConfig = Mockito.mock(RealmConfiguration.class);
    Collection collection = Mockito.mock(Collection.class);
    Mockito.when(realm.getRealmConfiguration()).thenReturn(realmConfig);
    Mockito.when(realmConfig.getAdminUserName()).thenReturn(USER_NAME);
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(false).thenReturn(true);
    Mockito.when(registry.newCollection()).thenReturn(collection);
    Mockito.when(registry.put(Mockito.anyString(), Mockito.any(Collection.class))).thenReturn("");
    APIManagerComponent apiManagerComponent = new APIManagerComponent();
    try {
        apiManagerComponent.activate(componentContext);
    } catch (Exception ex) {
        Assert.fail("Unexpected exception was thrown");
    }
    Assert.assertTrue(true);
    // Resource doesn't exists
    try {
        apiManagerComponent.activate(componentContext);
    } catch (Exception ex) {
        Assert.fail("Unexpected exception was thrown");
    }
    Assert.assertTrue(true);
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) Collection(org.wso2.carbon.registry.core.Collection) UserStoreException(org.wso2.carbon.user.core.UserStoreException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class SettingsMappingUtil method fromSettingstoDTO.

public SettingsDTO fromSettingstoDTO(Boolean isUserAvailable, Boolean moneatizationEnabled, boolean recommendationEnabled, boolean anonymousEnabled, String organization) throws APIManagementException {
    SettingsDTO settingsDTO = new SettingsDTO();
    settingsDTO.setScopes(GetScopeList());
    settingsDTO.setApplicationSharingEnabled(APIUtil.isMultiGroupAppSharingEnabled());
    settingsDTO.setRecommendationEnabled(recommendationEnabled);
    settingsDTO.setMapExistingAuthApps(APIUtil.isMapExistingAuthAppsEnabled());
    settingsDTO.setMonetizationEnabled(moneatizationEnabled);
    SettingsIdentityProviderDTO identityProviderDTO = new SettingsIdentityProviderDTO();
    identityProviderDTO.setExternal(APIUtil.getIdentityProviderConfig() != null);
    settingsDTO.setIdentityProvider(identityProviderDTO);
    settingsDTO.setIsAnonymousModeEnabled(anonymousEnabled);
    APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    boolean enableChangePassword = Boolean.parseBoolean(config.getFirstProperty(APIConstants.ENABLE_CHANGE_PASSWORD));
    settingsDTO.setIsPasswordChangeEnabled(enableChangePassword);
    String username = RestApiCommonUtil.getLoggedInUsername();
    String tenantDomain = MultitenantUtils.getTenantDomain(username);
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    String userStorePasswordPattern = null;
    String passwordPolicyPattern = null;
    int passwordPolicyMinLength = -1;
    int passwordPolicyMaxLength = -1;
    try {
        // Get password pattern from the UserStoreManager configuration
        RealmConfiguration realmConfiguration = null;
        RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
        if (realmService != null && tenantId != MultitenantConstants.INVALID_TENANT_ID) {
            UserStoreManager userStoreManager = null;
            userStoreManager = (UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
            realmConfiguration = userStoreManager.getRealmConfiguration();
        }
        if (realmConfiguration != null) {
            String passwordJavaRegEx = realmConfiguration.getUserStoreProperty(APIConstants.PASSWORD_JAVA_REGEX_PROPERTY);
            if (passwordJavaRegEx != null && !passwordJavaRegEx.trim().isEmpty()) {
                userStorePasswordPattern = passwordJavaRegEx;
            }
        }
        // Get password pattern from the Password policy
        Property passwordPolicyEnabledProperty = FrameworkUtils.getResidentIdpConfiguration(APIConstants.IS_PASSWORD_POLICY_ENABLED_PROPERTY, tenantDomain);
        boolean isPasswordPolicyEnabled = Boolean.parseBoolean(passwordPolicyEnabledProperty.getValue());
        if (isPasswordPolicyEnabled) {
            passwordPolicyPattern = FrameworkUtils.getResidentIdpConfiguration(APIConstants.PASSWORD_POLICY_PATTERN_PROPERTY, tenantDomain).getValue();
            passwordPolicyMinLength = Integer.parseInt(FrameworkUtils.getResidentIdpConfiguration(APIConstants.PASSWORD_POLICY_MIN_LENGTH_PROPERTY, tenantDomain).getValue());
            passwordPolicyMaxLength = Integer.parseInt(FrameworkUtils.getResidentIdpConfiguration(APIConstants.PASSWORD_POLICY_MAX_LENGTH_PROPERTY, tenantDomain).getValue());
        }
    } catch (UserStoreException e) {
        String errorMessage = "Error occurred in getting userRealm for the tenant: " + tenantId;
        throw new APIManagementException(errorMessage, e);
    } catch (FrameworkException e) {
        String errorMessage = "Error occurred in getting Resident Idp Configurations for tenant: " + tenantId;
        throw new APIManagementException(errorMessage, e);
    }
    settingsDTO.setUserStorePasswordPattern(userStorePasswordPattern);
    settingsDTO.setPasswordPolicyPattern(passwordPolicyPattern);
    settingsDTO.setPasswordPolicyMinLength(passwordPolicyMinLength);
    settingsDTO.setPasswordPolicyMaxLength(passwordPolicyMaxLength);
    if (isUserAvailable) {
        settingsDTO.setGrantTypes(APIUtil.getGrantTypes());
        Map<String, Environment> environments = APIUtil.getEnvironments(organization);
        if (environments.isEmpty()) {
            settingsDTO.apiGatewayEndpoint("http://localhost:8280, https://localhost:8243");
        } else {
            for (Map.Entry<String, Environment> entry : environments.entrySet()) {
                Environment environment = environments.get(entry.getKey());
                if (environment.isDefault()) {
                    settingsDTO.apiGatewayEndpoint(environment.getApiGatewayEndpoint());
                    break;
                }
            }
            if (settingsDTO.getApiGatewayEndpoint() == null) {
                Map.Entry<String, Environment> entry = environments.entrySet().iterator().next();
                Environment environment = environments.get(entry.getKey());
                settingsDTO.apiGatewayEndpoint(environment.getApiGatewayEndpoint());
            }
        }
    }
    return settingsDTO;
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) SettingsIdentityProviderDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SettingsIdentityProviderDTO) RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) SettingsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SettingsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Environment(org.wso2.carbon.apimgt.api.model.Environment) Property(org.wso2.carbon.identity.application.common.model.Property) Map(java.util.Map)

Example 10 with RealmConfiguration

use of org.wso2.carbon.user.core.config.RealmConfiguration in project carbon-apimgt by wso2.

the class RegistrationServiceImpl method isUserSuperAdmin.

private boolean isUserSuperAdmin(String username) {
    try {
        RealmConfiguration realmConfig = new RealmConfigXMLProcessor().buildRealmConfigurationFromFile();
        String adminUserName = realmConfig.getAdminUserName();
        return adminUserName.equalsIgnoreCase(username);
    } catch (UserStoreException e) {
        log.error("Error while retrieving super admin username", e);
        return false;
    }
}
Also used : RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) RealmConfigXMLProcessor(org.wso2.carbon.user.core.config.RealmConfigXMLProcessor) UserStoreException(org.wso2.carbon.user.core.UserStoreException)

Aggregations

RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.junit.Test)5 UserRealm (org.wso2.carbon.user.core.UserRealm)5 RealmService (org.wso2.carbon.user.core.service.RealmService)5 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 UserStoreException (org.wso2.carbon.user.core.UserStoreException)3 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)3 File (java.io.File)2 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)2 RealmConfigXMLProcessor (org.wso2.carbon.user.core.config.RealmConfigXMLProcessor)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Environment (org.wso2.carbon.apimgt.api.model.Environment)1 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 APIMConfigService (org.wso2.carbon.apimgt.impl.config.APIMConfigService)1 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)1