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;
}
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;
}
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;
}
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);
}
}
}
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);
}
Aggregations