use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project core-util by WSO2Telco.
the class UserClaimProsser method getUserClaimsByUserName.
public Map<ClaimName, String> getUserClaimsByUserName(String userName) {
try {
APIManagerConfiguration config = HostObjectComponent.getAPIManagerConfiguration();
String remoteUserStoreManagerServiceEndpoint = config.getFirstProperty(APIConstants.AUTH_MANAGER_URL) + AdminServicePath.REMOTE_USER_STORE_MANAGER_SERVICE.getTObject();
String adminUsername = config.getFirstProperty(APIConstants.AUTH_MANAGER_USERNAME);
String adminPassword = config.getFirstProperty(APIConstants.AUTH_MANAGER_PASSWORD);
RemoteUserStoreManagerServiceStub userStoreManagerStub = new RemoteUserStoreManagerServiceStub(remoteUserStoreManagerServiceEndpoint);
CarbonUtils.setBasicAccessSecurityHeaders(adminUsername, adminPassword, userStoreManagerStub._getServiceClient());
ClaimUtil claimUtil = new ClaimUtil();
Claim[] claims = claimUtil.convertToClaims(userStoreManagerStub.getUserClaimValues(userName, UserProfileType.DEFAULT.getTObject()));
List<ClaimName> somethingList = Arrays.asList(ClaimName.values());
for (Iterator<ClaimName> iterator = somethingList.iterator(); iterator.hasNext(); ) {
ClaimName claimName = iterator.next();
getClaimValue(claims, claimName);
}
} catch (RemoteException | RemoteUserStoreManagerServiceUserStoreExceptionException e) {
log.error("unable to retrieve claims for user " + userName + " : ", e);
return Collections.emptyMap();
}
return userClaimDetails;
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class SAMLGroupIDExtractorImpl method getGroupingIdentifierList.
@Override
public String[] getGroupingIdentifierList(String loginResponse) {
if (log.isDebugEnabled()) {
log.debug("Login response " + loginResponse);
}
ByteArrayInputStream samlResponseStream = null;
DocumentBuilder docBuilder;
String username = "";
String organization = "";
String[] groupIdArray = null;
try {
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String claim = config.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI);
if (StringUtils.isBlank(claim)) {
claim = "http://wso2.org/claims/organization";
}
samlResponseStream = getByteArrayInputStream(loginResponse);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
builderFactory.setNamespaceAware(true);
docBuilder = builderFactory.newDocumentBuilder();
Document document = docBuilder.parse(samlResponseStream);
Element element = document.getDocumentElement();
UnmarshallerFactory unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
Response response = (Response) unmarshaller.unmarshall(element);
List<Assertion> assertions = response.getAssertions();
if (assertions != null && assertions.size() > 0) {
Subject subject = assertions.get(0).getSubject();
if (subject != null) {
if (subject.getNameID() != null) {
username = subject.getNameID().getValue();
}
}
}
String isSAML2Enabled = System.getProperty(APIConstants.READ_ORGANIZATION_FROM_SAML_ASSERTION);
if (!StringUtils.isEmpty(isSAML2Enabled) && Boolean.parseBoolean(isSAML2Enabled)) {
organization = getOrganizationFromSamlAssertion(assertions);
} else {
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
String tenantDomain = MultitenantUtils.getTenantDomain(username);
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
UserStoreManager manager = realm.getUserStoreManager();
organization = manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), claim, null);
}
if (log.isDebugEnabled()) {
log.debug("User organization " + organization);
}
if (organization != null) {
if (organization.contains(",")) {
groupIdArray = organization.split(",");
for (int i = 0; i < groupIdArray.length; i++) {
groupIdArray[i] = groupIdArray[i].toString().trim();
}
} else {
organization = organization.trim();
groupIdArray = new String[] { organization };
}
} else {
// If claim is null then returning a empty string
groupIdArray = new String[] {};
}
} catch (ParserConfigurationException e) {
String msg = "Error while parsing SAML Assertion";
log.error(msg, e);
} catch (UnmarshallingException e) {
String msg = "Error while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (SAXException e) {
String msg = "Parsing exception occur while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (IOException e) {
String msg = "IO exception happen while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (UserStoreException e) {
log.error("User store exception occurred for user" + username, e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
log.error("Error while checking user existence for " + username, e);
} finally {
if (samlResponseStream != null) {
try {
samlResponseStream.close();
} catch (IOException e) {
// Ignore
log.error("ERROR_CLOSING_STREAM");
}
}
}
return groupIdArray;
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class SAMLGroupIDExtractorImpl method getGroupingIdentifiers.
public String getGroupingIdentifiers(String loginResponse) {
if (log.isDebugEnabled()) {
log.debug("Login response " + loginResponse);
}
ByteArrayInputStream samlResponseStream = null;
DocumentBuilder docBuilder;
String username = "";
String organization = "";
try {
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String claim = config.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI);
if (StringUtils.isBlank(claim)) {
claim = "http://wso2.org/claims/organization";
}
samlResponseStream = getByteArrayInputStream(loginResponse);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
builderFactory.setNamespaceAware(true);
docBuilder = builderFactory.newDocumentBuilder();
Document document = docBuilder.parse(samlResponseStream);
Element element = document.getDocumentElement();
UnmarshallerFactory unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
Response response = (Response) unmarshaller.unmarshall(element);
List<Assertion> assertions = response.getAssertions();
if (assertions != null && assertions.size() > 0) {
Subject subject = assertions.get(0).getSubject();
if (subject != null) {
if (subject.getNameID() != null) {
username = subject.getNameID().getValue();
}
}
}
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
String tenantDomain = MultitenantUtils.getTenantDomain(username);
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
UserStoreManager manager = realm.getUserStoreManager();
organization = manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), claim, null);
if (log.isDebugEnabled()) {
log.debug("User organization " + organization);
}
if (organization != null) {
organization = tenantDomain + "/" + organization.trim();
}
} catch (ParserConfigurationException e) {
String msg = "Error while parsing SAML Assertion";
log.error(msg, e);
} catch (UnmarshallingException e) {
String msg = "Error while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (SAXException e) {
String msg = "Parsing exception occur while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (IOException e) {
String msg = "IO exception happen while unmarshalling the SAML Assertion";
log.error(msg, e);
} catch (UserStoreException e) {
log.error("User store exception occurred for user" + username, e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
log.error("Error while checking user existence for " + username, e);
} finally {
if (samlResponseStream != null) {
try {
samlResponseStream.close();
} catch (IOException e) {
// Ignore
log.error("ERROR_CLOSING_STREAM");
}
}
}
return organization;
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class DefaultGroupIDExtractorImpl method getGroupingIdentifiers.
public String getGroupingIdentifiers(String loginResponse) {
JSONObject obj;
String username = null;
Boolean isSuperTenant;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String claim = config.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI);
if (StringUtils.isBlank(claim)) {
claim = "http://wso2.org/claims/organization";
}
String organization = null;
try {
obj = new JSONObject(loginResponse);
username = (String) obj.get("user");
isSuperTenant = (Boolean) obj.get("isSuperTenant");
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
// if the user is not in the super tenant domain then find the domain name and tenant id.
if (!isSuperTenant) {
tenantDomain = MultitenantUtils.getTenantDomain(username);
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
}
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
UserStoreManager manager = realm.getUserStoreManager();
organization = manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), claim, null);
if (organization != null) {
organization = tenantDomain + "/" + organization.trim();
}
} catch (JSONException e) {
log.error("Exception occured while trying to get group Identifier from login response", e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
log.error("Error while checking user existence for " + username, e);
}
return organization;
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration in project carbon-apimgt by wso2.
the class CacheProvider method createResourceCache.
/**
* Create and return RESOURCE_CACHE
*/
public static Cache createResourceCache() {
APIManagerConfiguration config = getApiManagerConfiguration();
String gatewayResourceCacheExpiry = config.getFirstProperty(APIConstants.GATEWAY_RESOURCE_CACHE_TIMEOUT);
if (gatewayResourceCacheExpiry != null) {
return getCache(APIConstants.API_MANAGER_CACHE_MANAGER, APIConstants.RESOURCE_CACHE_NAME, Long.parseLong(gatewayResourceCacheExpiry), Long.parseLong(gatewayResourceCacheExpiry));
} else {
long defaultCacheTimeout = getDefaultCacheTimeout();
return getCache(APIConstants.API_MANAGER_CACHE_MANAGER, APIConstants.RESOURCE_CACHE_NAME, defaultCacheTimeout, defaultCacheTimeout);
}
}
Aggregations