use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.
the class RegistryRecoveryDataStore method load.
@Override
public UserRecoveryDataDO load(String code) throws IdentityException {
Registry registry = null;
UserRecoveryDataDO dataDO = new UserRecoveryDataDO();
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
IdentityTenantUtil.initializeRegistry(tenantId);
registry = IdentityMgtServiceComponent.getRegistryService().getConfigSystemRegistry(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
registry.beginTransaction();
String secretKeyPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_DATA + RegistryConstants.PATH_SEPARATOR + code.toLowerCase();
if (registry.resourceExists(secretKeyPath)) {
Resource resource = registry.get(secretKeyPath);
Properties props = resource.getProperties();
for (Object o : props.keySet()) {
String key = (String) o;
if (key.equals(USER_ID)) {
dataDO.setUserName(resource.getProperty(key));
} else if (key.equals(SECRET_KEY)) {
dataDO.setSecret(resource.getProperty(key));
} else if (key.equals(EXPIRE_TIME)) {
String time = resource.getProperty(key);
dataDO.setExpireTime(time);
if (System.currentTimeMillis() > Long.parseLong(time)) {
dataDO.setValid(false);
break;
} else {
dataDO.setValid(true);
}
}
}
} else {
return null;
}
} catch (RegistryException e) {
log.error(e);
throw IdentityException.error("Error while loading user recovery data for code : " + code);
} finally {
if (registry != null) {
try {
registry.commitTransaction();
} catch (RegistryException e) {
log.error("Error while processing registry transaction", e);
}
}
}
return dataDO;
}
use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.
the class UserIdentityMetadataStore method invalidateMetadataSet.
/**
* @param metadataSet
* @throws IdentityException
*/
public void invalidateMetadataSet(IdentityMetadataDO[] metadataSet) throws IdentityException {
Connection connection = IdentityDatabaseUtil.getDBConnection();
PreparedStatement prepStmt = null;
try {
prepStmt = connection.prepareStatement(SQLQuery.INVALIDATE_METADATA);
for (IdentityMetadataDO metadata : metadataSet) {
prepStmt.setString(1, metadata.getUserName());
prepStmt.setInt(2, metadata.getTenantId());
prepStmt.setString(3, metadata.getMetadataType());
prepStmt.setString(4, metadata.getMetadata());
prepStmt.addBatch();
}
prepStmt.executeBatch();
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw IdentityException.error("Error while invalidating user identity data", e);
} finally {
IdentityDatabaseUtil.closeStatement(prepStmt);
IdentityDatabaseUtil.closeConnection(connection);
}
}
use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.
the class UserIdentityMetadataStore method loadMetadata.
/**
* Can be used to return primary security questions etc
*
* @param userName
* @param tenantId
* @param metadataType
* @return
* @throws IdentityException
*/
public IdentityMetadataDO[] loadMetadata(String userName, int tenantId, String metadataType) throws IdentityException {
Connection connection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement prepStmt = null;
ResultSet results = null;
try {
prepStmt = connection.prepareStatement(SQLQuery.LOAD_TENANT_METADATA);
prepStmt.setInt(1, tenantId);
prepStmt.setString(2, metadataType);
results = prepStmt.executeQuery();
List<IdentityMetadataDO> metada = new ArrayList<IdentityMetadataDO>();
while (results.next()) {
metada.add(new IdentityMetadataDO(results.getString(1), results.getInt(2), results.getString(3), results.getString(4), Boolean.parseBoolean(results.getString(5))));
}
IdentityMetadataDO[] resultMetadata = new IdentityMetadataDO[metada.size()];
return metada.toArray(resultMetadata);
} catch (SQLException e) {
throw IdentityException.error("Error while reading user identity data", e);
} finally {
IdentityDatabaseUtil.closeResultSet(results);
IdentityDatabaseUtil.closeStatement(prepStmt);
IdentityDatabaseUtil.closeConnection(connection);
}
}
use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.
the class UserStoreBasedIdentityDataStore method load.
/**
* This method loads identity and security questions from the user stores
*/
@Override
public UserIdentityClaimsDO load(String userName, UserStoreManager userStoreManager) {
UserIdentityClaimsDO userIdentityDTO = super.load(userName, userStoreManager);
if (userIdentityDTO != null) {
return userIdentityDTO;
}
// which happen calling getUserClaimValues()
if (TRUE_STRING.equals(userStoreInvoked.get())) {
if (log.isDebugEnabled()) {
log.debug("UserStoreBasedIdentityDataStore.load() already been called in the stack." + "Hence returning without processing load() again.");
}
return null;
} else {
if (log.isDebugEnabled()) {
log.debug("Set flag to indicate method UserStoreBasedIdentityDataStore.load() been called");
}
userStoreInvoked.set(TRUE_STRING);
}
Map<String, String> userDataMap = new HashMap<String, String>();
try {
// reading all claims of the user
Claim[] claims = ((AbstractUserStoreManager) userStoreManager).getUserClaimValues(userName, null);
// select the security questions and identity claims
if (claims != null) {
for (Claim claim : claims) {
String claimUri = claim.getClaimUri();
if (claimUri.contains(UserCoreConstants.ClaimTypeURIs.IDENTITY_CLAIM_URI) || claimUri.contains(UserCoreConstants.ClaimTypeURIs.CHALLENGE_QUESTION_URI)) {
if (log.isDebugEnabled()) {
log.debug("Adding UserIdentityClaim : " + claimUri + " with the value : " + claim.getValue());
}
userDataMap.put(claimUri, claim.getValue());
}
}
} else {
// null is returned when the user doesn't exist
return null;
}
} catch (UserStoreException e) {
if (!e.getMessage().startsWith(IdentityCoreConstants.USER_NOT_FOUND)) {
log.error("Error while reading identity user data from user store", e);
} else if (log.isDebugEnabled()) {
String message = null;
if (userStoreManager instanceof AbstractUserStoreManager) {
String domain = ((AbstractUserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
if (domain != null) {
message = "User: " + userName + " does not exist in " + domain;
}
}
if (message == null) {
message = "User: " + userName + " does not exist";
}
log.debug(message);
}
return null;
} finally {
// reset to initial value
if (log.isDebugEnabled()) {
log.debug("Reset flag to indicate method UserStoreBasedIdentityDataStore.load() being completing");
}
userStoreInvoked.set(FALSE_STRING);
}
userIdentityDTO = new UserIdentityClaimsDO(userName, userDataMap);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
userIdentityDTO.setTenantId(tenantId);
org.wso2.carbon.user.core.UserStoreManager store = (org.wso2.carbon.user.core.UserStoreManager) userStoreManager;
String domainName = store.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
try {
super.store(userIdentityDTO, userStoreManager);
} catch (IdentityException e) {
log.error("Error while reading user identity data", e);
}
return userIdentityDTO;
}
use of org.wso2.carbon.identity.base.IdentityException in project carbon-identity-framework by wso2.
the class UserStoreBasedIdentityDataStore method store.
/**
* This method stores data in the read write user stores.
*/
@Override
public void store(UserIdentityClaimsDO userIdentityDTO, UserStoreManager userStoreManager) throws IdentityException {
UserIdentityClaimsDO newIdentityClaimDO = new UserIdentityClaimsDO(userIdentityDTO.getUserName(), userIdentityDTO.getUserDataMap());
int tenantId;
try {
tenantId = userStoreManager.getTenantId();
} catch (UserStoreException e) {
throw IdentityException.error("Error while getting tenant Id.", e);
}
newIdentityClaimDO.setTenantId(tenantId);
super.store(newIdentityClaimDO, userStoreManager);
if (userIdentityDTO.getUserName() == null) {
log.error("Error while persisting user data. Null user name is provided.");
return;
}
String username = UserCoreUtil.removeDomainFromName(userIdentityDTO.getUserName());
try {
// store then log a warn.
if (!userStoreManager.isReadOnly()) {
// Need to clone the map. If not iterative calls will refer the same map
setUserClaimsValuesInUserStore(userStoreManager, username, new HashMap<>(userIdentityDTO.getUserDataMap()), null);
} else {
// If the user store is read only and still uses UserStoreBasedIdentityDataStore, then log a warn
log.warn("User store is read only. Changes to identities are only stored in memory, " + "and not updated in user store.");
return;
}
} catch (UserStoreException e) {
if (!e.getMessage().startsWith(IdentityCoreConstants.USER_NOT_FOUND)) {
throw IdentityException.error("Error while persisting identity user data in to user store", e);
} else if (log.isDebugEnabled()) {
String message = null;
if (userStoreManager instanceof AbstractUserStoreManager) {
String domain = ((AbstractUserStoreManager) userStoreManager).getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
if (domain != null) {
message = "User: " + username + " does not exist in " + domain;
}
}
if (message == null) {
message = "User: " + username + " does not exist";
}
log.debug(message);
return;
}
}
}
Aggregations