use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.
the class LdapProfile method readCanonicalDN.
public String readCanonicalDN(final PwmApplication pwmApplication, final String dnValue) throws PwmUnrecoverableException {
{
final boolean doCanonicalDnResolve = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_RESOLVE_CANONICAL_DN));
if (!doCanonicalDnResolve) {
return dnValue;
}
}
final boolean enableCanonicalCache = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_CANONICAL_ENABLE));
String canonicalValue = null;
final CacheKey cacheKey = CacheKey.makeCacheKey(LdapPermissionTester.class, null, "canonicalDN-" + this.getIdentifier() + "-" + dnValue);
if (enableCanonicalCache) {
final String cachedDN = pwmApplication.getCacheService().get(cacheKey);
if (cachedDN != null) {
canonicalValue = cachedDN;
}
}
if (canonicalValue == null) {
try {
final ChaiProvider chaiProvider = this.getProxyChaiProvider(pwmApplication);
final ChaiEntry chaiEntry = chaiProvider.getEntryFactory().newChaiEntry(dnValue);
canonicalValue = chaiEntry.readCanonicalDN();
if (enableCanonicalCache) {
final long cacheSeconds = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_CANONICAL_SECONDS));
final CachePolicy cachePolicy = CachePolicy.makePolicyWithExpiration(new TimeDuration(cacheSeconds, TimeUnit.SECONDS));
pwmApplication.getCacheService().put(cacheKey, cachePolicy, canonicalValue);
}
LOGGER.trace("read and cached canonical ldap DN value for input '" + dnValue + "' as '" + canonicalValue + "'");
} catch (ChaiUnavailableException | ChaiOperationException e) {
LOGGER.error("error while reading canonicalDN for dn value '" + dnValue + "', error: " + e.getMessage());
return dnValue;
}
}
return canonicalValue;
}
use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.
the class LdapOperationsHelper method readLdapGuidValue.
public static String readLdapGuidValue(final PwmApplication pwmApplication, final SessionLabel sessionLabel, final UserIdentity userIdentity, final boolean throwExceptionOnError) throws ChaiUnavailableException, PwmUnrecoverableException {
final boolean enableCache = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_USER_GUID_ENABLE));
final CacheKey cacheKey = CacheKey.makeCacheKey(LdapOperationsHelper.class, null, "guidValue-" + userIdentity.toDelimitedKey());
if (enableCache) {
final String cachedValue = pwmApplication.getCacheService().get(cacheKey);
if (cachedValue != null) {
return NULL_CACHE_GUID.equals(cachedValue) ? null : cachedValue;
}
}
final String existingValue = GUIDHelper.readExistingGuidValue(pwmApplication, sessionLabel, userIdentity, throwExceptionOnError);
final LdapProfile ldapProfile = pwmApplication.getConfig().getLdapProfiles().get(userIdentity.getLdapProfileID());
final String guidAttributeName = ldapProfile.readSettingAsString(PwmSetting.LDAP_GUID_ATTRIBUTE);
if (StringUtil.isEmpty(existingValue)) {
if (!"DN".equalsIgnoreCase(guidAttributeName) && !"VENDORGUID".equalsIgnoreCase(guidAttributeName)) {
if (ldapProfile.readSettingAsBoolean(PwmSetting.LDAP_GUID_AUTO_ADD)) {
LOGGER.trace("assigning new GUID to user " + userIdentity);
return GUIDHelper.assignGuidToUser(pwmApplication, sessionLabel, userIdentity, guidAttributeName);
}
}
final String errorMsg = "unable to resolve GUID value for user " + userIdentity.toString();
GUIDHelper.processError(errorMsg, throwExceptionOnError);
}
if (enableCache) {
final long cacheSeconds = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_USER_GUID_SECONDS));
final CachePolicy cachePolicy = CachePolicy.makePolicyWithExpiration(new TimeDuration(cacheSeconds, TimeUnit.SECONDS));
final String cacheValue = existingValue == null ? NULL_CACHE_GUID : existingValue;
pwmApplication.getCacheService().put(cacheKey, cachePolicy, cacheValue);
}
return existingValue;
}
use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.
the class UserIdentity method toObfuscatedKey.
public String toObfuscatedKey(final PwmApplication pwmApplication) throws PwmUnrecoverableException {
// use local cache first.
if (!StringUtil.isEmpty(obfuscatedValue)) {
return obfuscatedValue;
}
// check app cache. This is used primarily so that keys are static over some meaningful lifetime, allowing browser caching based on keys.
final CacheService cacheService = pwmApplication.getCacheService();
final CacheKey cacheKey = CacheKey.makeCacheKey(this.getClass(), null, "userKey" + "|" + this.toDelimitedKey());
final String cachedValue = cacheService.get(cacheKey);
if (!StringUtil.isEmpty(cachedValue)) {
obfuscatedValue = cachedValue;
return cachedValue;
}
// generate key
try {
final String jsonValue = JsonUtil.serialize(this);
final String localValue = CRYPO_HEADER + pwmApplication.getSecureService().encryptToString(jsonValue);
this.obfuscatedValue = localValue;
cacheService.put(cacheKey, CachePolicy.makePolicyWithExpiration(TimeDuration.DAY), localValue);
return localValue;
} catch (Exception e) {
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "unexpected error making obfuscated user key: " + e.getMessage()));
}
}
use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.
the class PeopleSearchDataReader method makeOrgChartData.
OrgChartDataBean makeOrgChartData(final UserIdentity userIdentity, final boolean noChildren) throws PwmUnrecoverableException {
final Instant startTime = Instant.now();
final CacheKey cacheKey = makeCacheKey(OrgChartDataBean.class.getSimpleName(), userIdentity.toDelimitedKey() + "|" + noChildren);
{
// if value is cached then return;
final String cachedOutput = pwmRequest.getPwmApplication().getCacheService().get(cacheKey);
if (cachedOutput != null) {
StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_HITS);
LOGGER.trace(pwmRequest, "completed makeOrgChartData of " + userIdentity.toDisplayString() + " from cache");
return JsonUtil.deserialize(cachedOutput, OrgChartDataBean.class);
} else {
StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_MISSES);
}
}
final OrgChartDataBean orgChartData = new OrgChartDataBean();
// make self reference
orgChartData.setSelf(makeOrgChartReferenceForIdentity(userIdentity));
{
// make parent reference
final List<UserIdentity> parentIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartParentAttr());
if (parentIdentities != null && !parentIdentities.isEmpty()) {
final UserIdentity parentIdentity = parentIdentities.iterator().next();
orgChartData.setParent(makeOrgChartReferenceForIdentity(parentIdentity));
}
}
int childCount = 0;
if (!noChildren) {
// make children reference
final Map<String, OrgChartReferenceBean> sortedChildren = new TreeMap<>();
final List<UserIdentity> childIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartChildAttr());
for (final UserIdentity childIdentity : childIdentities) {
final OrgChartReferenceBean childReference = makeOrgChartReferenceForIdentity(childIdentity);
if (childReference != null) {
if (childReference.getDisplayNames() != null && !childReference.getDisplayNames().isEmpty()) {
final String firstDisplayName = childReference.getDisplayNames().iterator().next();
sortedChildren.put(firstDisplayName, childReference);
} else {
sortedChildren.put(String.valueOf(childCount), childReference);
}
childCount++;
}
}
orgChartData.setChildren(Collections.unmodifiableList(new ArrayList<>(sortedChildren.values())));
}
if (!StringUtil.isEmpty(peopleSearchConfiguration.getOrgChartAssistantAttr())) {
final List<UserIdentity> assistantIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartAssistantAttr());
if (assistantIdentities != null && !assistantIdentities.isEmpty()) {
final UserIdentity assistantIdentity = assistantIdentities.iterator().next();
final OrgChartReferenceBean assistantReference = makeOrgChartReferenceForIdentity(assistantIdentity);
if (assistantReference != null) {
orgChartData.setAssistant(assistantReference);
}
}
}
final TimeDuration totalTime = TimeDuration.fromCurrent(startTime);
storeDataInCache(pwmRequest.getPwmApplication(), cacheKey, orgChartData);
LOGGER.trace(pwmRequest, "completed makeOrgChartData in " + totalTime.asCompactString() + " with " + childCount + " children");
return orgChartData;
}
use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.
the class PeopleSearchDataReader method makeUserDetailRequest.
UserDetailBean makeUserDetailRequest(final String userKey) throws PwmUnrecoverableException, PwmOperationalException, ChaiUnavailableException {
final Instant startTime = Instant.now();
final UserIdentity userIdentity = UserIdentity.fromKey(userKey, pwmRequest.getPwmApplication());
final CacheKey cacheKey = makeCacheKey(UserDetailBean.class.getSimpleName(), userIdentity.toDelimitedKey());
{
final String cachedOutput = pwmRequest.getPwmApplication().getCacheService().get(cacheKey);
if (cachedOutput != null) {
StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_HITS);
return JsonUtil.deserialize(cachedOutput, UserDetailBean.class);
} else {
StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_MISSES);
}
}
try {
checkIfUserIdentityViewable(userIdentity);
} catch (PwmOperationalException e) {
LOGGER.error(pwmRequest.getPwmSession(), "error during detail results request while checking if requested userIdentity is within search scope: " + e.getMessage());
throw e;
}
final UserSearchResults detailResults = doDetailLookup(userIdentity);
final Map<String, String> searchResults = detailResults.getResults().get(userIdentity);
final UserDetailBean userDetailBean = new UserDetailBean();
userDetailBean.setUserKey(userKey);
final List<FormConfiguration> detailFormConfig = pwmRequest.getConfig().readSettingAsForm(PwmSetting.PEOPLE_SEARCH_DETAIL_FORM);
final Map<String, AttributeDetailBean> attributeBeans = convertResultMapToBeans(pwmRequest, userIdentity, detailFormConfig, searchResults);
userDetailBean.setDetail(attributeBeans);
final String photoURL = figurePhotoURL(pwmRequest, userIdentity);
if (photoURL != null) {
userDetailBean.setPhotoURL(photoURL);
}
final List<String> displayName = figureDisplaynames(pwmRequest, userIdentity);
if (displayName != null) {
userDetailBean.setDisplayNames(displayName);
}
userDetailBean.setLinks(makeUserDetailLinks(userIdentity));
LOGGER.trace(pwmRequest.getPwmSession(), "finished building userDetail result in " + TimeDuration.fromCurrent(startTime).asCompactString());
storeDataInCache(pwmRequest.getPwmApplication(), cacheKey, userDetailBean);
return userDetailBean;
}
Aggregations