UserInfoReader.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2017 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. package password.pwm.ldap;
  23. import com.novell.ldapchai.ChaiFactory;
  24. import com.novell.ldapchai.ChaiUser;
  25. import com.novell.ldapchai.exception.ChaiException;
  26. import com.novell.ldapchai.exception.ChaiOperationException;
  27. import com.novell.ldapchai.exception.ChaiUnavailableException;
  28. import com.novell.ldapchai.provider.ChaiProvider;
  29. import password.pwm.PwmApplication;
  30. import password.pwm.bean.PasswordStatus;
  31. import password.pwm.bean.ResponseInfoBean;
  32. import password.pwm.bean.SessionLabel;
  33. import password.pwm.bean.UserIdentity;
  34. import password.pwm.config.Configuration;
  35. import password.pwm.config.value.data.FormConfiguration;
  36. import password.pwm.config.FormUtility;
  37. import password.pwm.config.PwmSetting;
  38. import password.pwm.config.value.data.UserPermission;
  39. import password.pwm.config.option.ADPolicyComplexity;
  40. import password.pwm.config.option.ForceSetupPolicy;
  41. import password.pwm.config.profile.ChallengeProfile;
  42. import password.pwm.config.profile.LdapProfile;
  43. import password.pwm.config.profile.ProfileType;
  44. import password.pwm.config.profile.ProfileUtility;
  45. import password.pwm.config.profile.PwmPasswordPolicy;
  46. import password.pwm.config.profile.PwmPasswordRule;
  47. import password.pwm.config.profile.UpdateAttributesProfile;
  48. import password.pwm.error.ErrorInformation;
  49. import password.pwm.error.PwmDataValidationException;
  50. import password.pwm.error.PwmError;
  51. import password.pwm.error.PwmUnrecoverableException;
  52. import password.pwm.svc.PwmService;
  53. import password.pwm.util.PasswordData;
  54. import password.pwm.util.PwmPasswordRuleValidator;
  55. import password.pwm.util.java.CachingProxyWrapper;
  56. import password.pwm.util.java.JavaHelper;
  57. import password.pwm.util.java.TimeDuration;
  58. import password.pwm.util.logging.PwmLogger;
  59. import password.pwm.util.operations.CrService;
  60. import password.pwm.util.operations.OtpService;
  61. import password.pwm.util.operations.PasswordUtility;
  62. import password.pwm.util.operations.otp.OTPUserRecord;
  63. import java.time.Instant;
  64. import java.util.Collection;
  65. import java.util.Collections;
  66. import java.util.Date;
  67. import java.util.HashMap;
  68. import java.util.HashSet;
  69. import java.util.LinkedHashMap;
  70. import java.util.List;
  71. import java.util.Locale;
  72. import java.util.Map;
  73. import java.util.Set;
  74. public class UserInfoReader implements UserInfo {
  75. private static final PwmLogger LOGGER = PwmLogger.forClass(UserInfoReader.class);
  76. private final UserIdentity userIdentity;
  77. private final PasswordData currentPassword;
  78. private final Locale locale;
  79. private final ChaiUser chaiUser;
  80. private final SessionLabel sessionLabel;
  81. private final PwmApplication pwmApplication;
  82. /** A reference to this object, but with memorized (cached) method implementations. In most cases references to 'this'
  83. inside this class should use this {@code selfCachedReference} instead.
  84. */
  85. private UserInfo selfCachedReference;
  86. private UserInfoReader(
  87. final UserIdentity userIdentity,
  88. final PasswordData currentPassword,
  89. final SessionLabel sessionLabel,
  90. final Locale locale,
  91. final PwmApplication pwmApplication,
  92. final ChaiProvider chaiProvider
  93. )
  94. throws ChaiUnavailableException
  95. {
  96. this.userIdentity = userIdentity;
  97. this.currentPassword = currentPassword;
  98. this.pwmApplication = pwmApplication;
  99. this.locale = locale;
  100. this.sessionLabel = sessionLabel;
  101. final ChaiProvider cachingProvider = CachingProxyWrapper.create(ChaiProvider.class, chaiProvider);
  102. this.chaiUser = ChaiFactory.createChaiUser(userIdentity.getUserDN(), cachingProvider);
  103. }
  104. static UserInfo create(
  105. final UserIdentity userIdentity,
  106. final PasswordData currentPassword,
  107. final SessionLabel sessionLabel,
  108. final Locale locale,
  109. final PwmApplication pwmApplication,
  110. final ChaiProvider chaiProvider
  111. )
  112. throws ChaiUnavailableException
  113. {
  114. final UserInfoReader userInfo = new UserInfoReader(userIdentity, currentPassword, sessionLabel, locale, pwmApplication, chaiProvider);
  115. final UserInfo selfCachedReference = CachingProxyWrapper.create(UserInfo.class, userInfo);
  116. userInfo.selfCachedReference = selfCachedReference;
  117. return selfCachedReference;
  118. }
  119. @Override
  120. public Map<String, String> getCachedPasswordRuleAttributes() throws PwmUnrecoverableException
  121. {
  122. final Set<String> interestingUserAttributes = figurePasswordRuleAttributes(selfCachedReference);
  123. final Map<String, String> allUserAttrs = readStringAttributes(interestingUserAttributes);
  124. return Collections.unmodifiableMap(allUserAttrs);
  125. }
  126. @Override
  127. public Map<String, String> getCachedAttributeValues() throws PwmUnrecoverableException
  128. {
  129. final LdapProfile ldapProfile = getUserIdentity().getLdapProfile(pwmApplication.getConfig());
  130. final List<String> cachedAttributeNames = ldapProfile.readSettingAsStringArray(PwmSetting.CACHED_USER_ATTRIBUTES);
  131. if (cachedAttributeNames != null && !cachedAttributeNames.isEmpty()) {
  132. final Map<String, String> attributeValues = readStringAttributes(new HashSet<>(cachedAttributeNames));
  133. return Collections.unmodifiableMap(attributeValues);
  134. }
  135. return Collections.emptyMap();
  136. }
  137. @Override
  138. public Instant getLastLdapLoginTime() throws PwmUnrecoverableException
  139. {
  140. try {
  141. final Date lastLoginTime = chaiUser.readLastLoginTime();
  142. return lastLoginTime == null
  143. ? null
  144. : lastLoginTime.toInstant();
  145. } catch (ChaiOperationException e) {
  146. LOGGER.warn(sessionLabel, "error reading user's last ldap login time: " + e.getMessage());
  147. } catch (ChaiUnavailableException e) {
  148. throw PwmUnrecoverableException.fromChaiException(e);
  149. }
  150. return null;
  151. }
  152. @Override
  153. public ChallengeProfile getChallengeProfile() throws PwmUnrecoverableException
  154. {
  155. final PwmPasswordPolicy pwmPasswordPolicy = selfCachedReference.getPasswordPolicy();
  156. final CrService crService = pwmApplication.getCrService();
  157. return crService.readUserChallengeProfile(
  158. sessionLabel,
  159. getUserIdentity(),
  160. chaiUser,
  161. pwmPasswordPolicy,
  162. locale
  163. );
  164. }
  165. @Override
  166. public PwmPasswordPolicy getPasswordPolicy() throws PwmUnrecoverableException
  167. {
  168. return PasswordUtility.readPasswordPolicyForUser(pwmApplication, sessionLabel, getUserIdentity(), chaiUser, locale);
  169. }
  170. @Override
  171. public UserIdentity getUserIdentity()
  172. {
  173. return userIdentity;
  174. }
  175. @Override
  176. public Instant getPasswordExpirationTime() throws PwmUnrecoverableException
  177. {
  178. return LdapOperationsHelper.readPasswordExpirationTime(chaiUser);
  179. }
  180. @Override
  181. public String getUsername() throws PwmUnrecoverableException
  182. {
  183. final LdapProfile ldapProfile = getUserIdentity().getLdapProfile(pwmApplication.getConfig());
  184. final String uIDattr = ldapProfile.getUsernameAttribute();
  185. return readStringAttribute(uIDattr);
  186. }
  187. @Override
  188. public PasswordStatus getPasswordStatus() throws PwmUnrecoverableException
  189. {
  190. final Configuration config = pwmApplication.getConfig();
  191. final PasswordStatus.PasswordStatusBuilder passwordStatusBuilder = PasswordStatus.builder();
  192. final String userDN = chaiUser.getEntryDN();
  193. final PwmPasswordPolicy passwordPolicy = selfCachedReference.getPasswordPolicy();
  194. final long startTime = System.currentTimeMillis();
  195. LOGGER.trace(sessionLabel, "beginning password status check process for " + userDN);
  196. // check if password meets existing policy.
  197. if (passwordPolicy.getRuleHelper().readBooleanValue(PwmPasswordRule.EnforceAtLogin)) {
  198. if (currentPassword != null) {
  199. try {
  200. final PwmPasswordRuleValidator passwordRuleValidator = new PwmPasswordRuleValidator(pwmApplication, passwordPolicy);
  201. passwordRuleValidator.testPassword(currentPassword, null, selfCachedReference, chaiUser);
  202. } catch (PwmDataValidationException | PwmUnrecoverableException e) {
  203. LOGGER.debug(sessionLabel, "user " + userDN + " password does not conform to current password policy (" + e.getMessage() + "), marking as requiring change.");
  204. passwordStatusBuilder.violatesPolicy(true);
  205. } catch (ChaiUnavailableException e) {
  206. throw PwmUnrecoverableException.fromChaiException(e);
  207. }
  208. }
  209. }
  210. boolean ldapPasswordExpired = false;
  211. try {
  212. ldapPasswordExpired = chaiUser.isPasswordExpired();
  213. if (ldapPasswordExpired) {
  214. LOGGER.trace(sessionLabel, "password for " + userDN + " appears to be expired");
  215. } else {
  216. LOGGER.trace(sessionLabel, "password for " + userDN + " does not appear to be expired");
  217. }
  218. } catch (ChaiOperationException e) {
  219. LOGGER.info(sessionLabel, "error reading LDAP attributes for " + userDN + " while reading isPasswordExpired(): " + e.getMessage());
  220. } catch (ChaiUnavailableException e) {
  221. throw PwmUnrecoverableException.fromChaiException(e);
  222. }
  223. final Instant ldapPasswordExpirationTime = selfCachedReference.getPasswordExpirationTime();
  224. boolean preExpired = false;
  225. if (ldapPasswordExpirationTime != null) {
  226. final TimeDuration expirationInterval = TimeDuration.fromCurrent(ldapPasswordExpirationTime);
  227. LOGGER.trace(sessionLabel, "read password expiration time: "
  228. + JavaHelper.toIsoDate(ldapPasswordExpirationTime)
  229. + ", " + expirationInterval.asCompactString() + " from now"
  230. );
  231. final TimeDuration diff = TimeDuration.fromCurrent(ldapPasswordExpirationTime);
  232. // now check to see if the user's expire time is within the 'preExpireTime' setting.
  233. final long preExpireMs = config.readSettingAsLong(PwmSetting.PASSWORD_EXPIRE_PRE_TIME) * 1000;
  234. if (diff.getTotalMilliseconds() > 0 && diff.getTotalMilliseconds() < preExpireMs) {
  235. LOGGER.debug(sessionLabel, "user " + userDN + " password will expire within "
  236. + diff.asCompactString()
  237. + ", marking as pre-expired");
  238. preExpired = true;
  239. } else if (ldapPasswordExpired) {
  240. preExpired = true;
  241. LOGGER.debug(sessionLabel, "user " + userDN + " password is expired, marking as pre-expired.");
  242. }
  243. // now check to see if the user's expire time is within the 'preWarnTime' setting.
  244. final long preWarnMs = config.readSettingAsLong(PwmSetting.PASSWORD_EXPIRE_WARN_TIME) * 1000;
  245. // don't check if the 'preWarnTime' setting is zero or less than the expirePreTime
  246. if (!ldapPasswordExpired && !preExpired) {
  247. if (!(preWarnMs == 0 || preWarnMs < preExpireMs)) {
  248. if (diff.getTotalMilliseconds() > 0 && diff.getTotalMilliseconds() < preWarnMs) {
  249. LOGGER.debug(sessionLabel,
  250. "user " + userDN + " password will expire within "
  251. + diff.asCompactString()
  252. + ", marking as within warn period");
  253. passwordStatusBuilder.warnPeriod(true);
  254. } else if (ldapPasswordExpired) {
  255. LOGGER.debug(sessionLabel,
  256. "user " + userDN + " password is expired, marking as within warn period");
  257. passwordStatusBuilder.warnPeriod(true);
  258. }
  259. }
  260. }
  261. passwordStatusBuilder.preExpired(preExpired);
  262. }
  263. LOGGER.debug(sessionLabel, "completed user password status check for " + userDN + " " + passwordStatusBuilder + " (" + TimeDuration.fromCurrent(startTime).asCompactString() + ")");
  264. passwordStatusBuilder.expired(ldapPasswordExpired);
  265. return passwordStatusBuilder.build();
  266. }
  267. @Override
  268. public boolean isRequiresNewPassword() throws PwmUnrecoverableException
  269. {
  270. final PasswordStatus passwordStatus = selfCachedReference.getPasswordStatus();
  271. final List<UserPermission> updateProfilePermission = pwmApplication.getConfig().readSettingAsUserPermission(
  272. PwmSetting.QUERY_MATCH_CHANGE_PASSWORD);
  273. if (!LdapPermissionTester.testUserPermissions(pwmApplication, sessionLabel, userIdentity, updateProfilePermission)) {
  274. LOGGER.debug(sessionLabel,
  275. "checkPassword: " + userIdentity.toString() + " user does not have permission to change password");
  276. return false;
  277. }
  278. if (passwordStatus.isExpired()) {
  279. LOGGER.debug(sessionLabel, "checkPassword: password is expired, marking new password as required");
  280. return true;
  281. }
  282. if (passwordStatus.isPreExpired()) {
  283. LOGGER.debug(sessionLabel, "checkPassword: password is pre-expired, marking new password as required");
  284. return true;
  285. }
  286. if (passwordStatus.isWarnPeriod()) {
  287. LOGGER.debug(sessionLabel, "checkPassword: password is within warn period, marking new password as required");
  288. return true;
  289. }
  290. if (passwordStatus.isViolatesPolicy()) {
  291. LOGGER.debug(sessionLabel, "checkPassword: current password violates password policy, marking new password as required");
  292. return true;
  293. }
  294. return false;
  295. }
  296. @Override
  297. public boolean isRequiresResponseConfig() throws PwmUnrecoverableException
  298. {
  299. final CrService crService = pwmApplication.getCrService();
  300. try {
  301. return crService.checkIfResponseConfigNeeded(
  302. pwmApplication,
  303. sessionLabel,
  304. getUserIdentity(),
  305. selfCachedReference.getChallengeProfile().getChallengeSet(),
  306. selfCachedReference.getResponseInfoBean());
  307. } catch (ChaiUnavailableException e) {
  308. throw PwmUnrecoverableException.fromChaiException(e);
  309. }
  310. }
  311. @Override
  312. public boolean isRequiresOtpConfig() throws PwmUnrecoverableException
  313. {
  314. LOGGER.trace(sessionLabel, "checkOtp: beginning process to check if user OTP setup is required");
  315. final UserIdentity userIdentity = getUserIdentity();
  316. if (!pwmApplication.getConfig().readSettingAsBoolean(PwmSetting.OTP_ENABLED)) {
  317. LOGGER.trace(sessionLabel, "checkOtp: OTP is not enabled, user OTP setup is not required");
  318. return false;
  319. }
  320. final OTPUserRecord otpUserRecord = selfCachedReference.getOtpUserRecord();
  321. final boolean hasStoredOtp = otpUserRecord != null && otpUserRecord.getSecret() != null;
  322. if (hasStoredOtp) {
  323. LOGGER.trace(sessionLabel, "checkOtp: user has existing valid otp record, user OTP setup is not required");
  324. return false;
  325. }
  326. final List<UserPermission> setupOtpPermission = pwmApplication.getConfig().readSettingAsUserPermission(PwmSetting.OTP_SETUP_USER_PERMISSION);
  327. if (!LdapPermissionTester.testUserPermissions(pwmApplication, sessionLabel, userIdentity, setupOtpPermission)) {
  328. LOGGER.trace(sessionLabel, "checkOtp: " + userIdentity.toString() + " is not eligible for checkOtp due to query match");
  329. return false;
  330. }
  331. final ForceSetupPolicy policy = pwmApplication.getConfig().readSettingAsEnum(PwmSetting.OTP_FORCE_SETUP, ForceSetupPolicy.class);
  332. // hasStoredOtp is always true at this point, so if forced then update needed
  333. LOGGER.debug(sessionLabel, "checkOtp: user does not have existing valid otp record, user OTP setup is required");
  334. return policy == ForceSetupPolicy.FORCE || policy == ForceSetupPolicy.FORCE_ALLOW_SKIP;
  335. }
  336. @Override
  337. public boolean isRequiresUpdateProfile() throws PwmUnrecoverableException
  338. {
  339. final Configuration configuration = pwmApplication.getConfig();
  340. if (!pwmApplication.getConfig().readSettingAsBoolean(PwmSetting.UPDATE_PROFILE_ENABLE)) {
  341. LOGGER.debug(sessionLabel, "checkProfiles: " + userIdentity.toString() + " profile module is not enabled");
  342. return false;
  343. }
  344. UpdateAttributesProfile updateAttributesProfile = null;
  345. final Map<ProfileType, String> profileIDs = selfCachedReference.getProfileIDs();
  346. if (profileIDs.containsKey(ProfileType.UpdateAttributes)) {
  347. updateAttributesProfile = configuration.getUpdateAttributesProfile().get(profileIDs.get(ProfileType.UpdateAttributes));
  348. }
  349. if (updateAttributesProfile == null) {
  350. return false;
  351. }
  352. if (!updateAttributesProfile.readSettingAsBoolean(PwmSetting.UPDATE_PROFILE_FORCE_SETUP)) {
  353. LOGGER.debug(sessionLabel, "checkProfiles: " + userIdentity.toString() + " profile force setup is not enabled");
  354. return false;
  355. }
  356. final List<FormConfiguration> updateFormFields = updateAttributesProfile.readSettingAsForm(PwmSetting.UPDATE_PROFILE_FORM);
  357. // populate the map from ldap
  358. try {
  359. final Map<FormConfiguration, List<String>> valueMap = FormUtility.populateFormMapFromLdap(
  360. updateFormFields,
  361. sessionLabel,
  362. selfCachedReference,
  363. FormUtility.Flag.ReturnEmptyValues
  364. );
  365. final Map<FormConfiguration, String> singleValueMap = FormUtility.multiValueMapToSingleValue(valueMap);
  366. FormUtility.validateFormValues(configuration, singleValueMap, locale);
  367. LOGGER.debug(sessionLabel, "checkProfile: " + userIdentity + " has value for attributes, update profile will not be required");
  368. return false;
  369. } catch (PwmDataValidationException e) {
  370. LOGGER.debug(sessionLabel, "checkProfile: " + userIdentity + " does not have good attributes (" + e.getMessage() + "), update profile will be required");
  371. return true;
  372. } catch (PwmUnrecoverableException e) {
  373. e.printStackTrace();
  374. }
  375. return false;
  376. }
  377. @Override
  378. public Instant getPasswordLastModifiedTime() throws PwmUnrecoverableException
  379. {
  380. try {
  381. return PasswordUtility.determinePwdLastModified(pwmApplication, sessionLabel, userIdentity);
  382. } catch (ChaiUnavailableException e) {
  383. throw PwmUnrecoverableException.fromChaiException(e);
  384. }
  385. }
  386. @Override
  387. public String getUserEmailAddress() throws PwmUnrecoverableException
  388. {
  389. final LdapProfile ldapProfile = getUserIdentity().getLdapProfile(pwmApplication.getConfig());
  390. final String ldapEmailAttribute = ldapProfile.readSettingAsString(PwmSetting.EMAIL_USER_MAIL_ATTRIBUTE);
  391. return readStringAttribute(ldapEmailAttribute);
  392. }
  393. @Override
  394. public String getUserSmsNumber() throws PwmUnrecoverableException
  395. {
  396. final LdapProfile ldapProfile = getUserIdentity().getLdapProfile(pwmApplication.getConfig());
  397. final String ldapSmsAttribute = ldapProfile.readSettingAsString(PwmSetting.SMS_USER_PHONE_ATTRIBUTE);
  398. return readStringAttribute(ldapSmsAttribute);
  399. }
  400. @Override
  401. public String getUserGuid() throws PwmUnrecoverableException
  402. {
  403. try {
  404. return LdapOperationsHelper.readLdapGuidValue(pwmApplication, sessionLabel, userIdentity, false);
  405. } catch (ChaiUnavailableException e) {
  406. throw PwmUnrecoverableException.fromChaiException(e);
  407. }
  408. }
  409. @Override
  410. public ResponseInfoBean getResponseInfoBean() throws PwmUnrecoverableException
  411. {
  412. final CrService crService = pwmApplication.getCrService();
  413. try {
  414. return crService.readUserResponseInfo(sessionLabel, getUserIdentity(), chaiUser);
  415. } catch (ChaiUnavailableException e) {
  416. throw PwmUnrecoverableException.fromChaiException(e);
  417. }
  418. }
  419. @Override
  420. public OTPUserRecord getOtpUserRecord() throws PwmUnrecoverableException
  421. {
  422. final OtpService otpService = pwmApplication.getOtpService();
  423. if (otpService != null && otpService.status() == PwmService.STATUS.OPEN) {
  424. try {
  425. return otpService.readOTPUserConfiguration(sessionLabel, userIdentity);
  426. } catch (ChaiUnavailableException e) {
  427. throw PwmUnrecoverableException.fromChaiException(e);
  428. }
  429. }
  430. return null;
  431. }
  432. @Override
  433. public Instant getAccountExpirationTime() throws PwmUnrecoverableException
  434. {
  435. try {
  436. final Date accountExpireDate = chaiUser.readAccountExpirationDate();
  437. return accountExpireDate == null
  438. ? null
  439. : accountExpireDate.toInstant();
  440. } catch (ChaiOperationException e) {
  441. LOGGER.warn(sessionLabel, "error reading user's account expiration time: " + e.getMessage());
  442. } catch (ChaiUnavailableException e) {
  443. throw PwmUnrecoverableException.fromChaiException(e);
  444. }
  445. return null;
  446. }
  447. @Override
  448. public Map<ProfileType, String> getProfileIDs() throws PwmUnrecoverableException
  449. {
  450. final Map<ProfileType, String> returnMap = new HashMap<>();
  451. for (final ProfileType profileType : ProfileType.values()) {
  452. if (profileType.isAuthenticated()) {
  453. final String profileID = ProfileUtility.discoverProfileIDforUser(pwmApplication, sessionLabel, userIdentity, profileType);
  454. returnMap.put(profileType, profileID);
  455. if (profileID != null) {
  456. LOGGER.debug(sessionLabel, "assigned " + profileType.toString() + " profileID \"" + profileID + "\" to " + userIdentity.toDisplayString());
  457. } else {
  458. LOGGER.debug(sessionLabel, profileType.toString() + " has no matching profiles for user " + userIdentity.toDisplayString());
  459. }
  460. }
  461. }
  462. return Collections.unmodifiableMap(returnMap);
  463. }
  464. private static Set<String> figurePasswordRuleAttributes(
  465. final UserInfo uiBean
  466. ) throws PwmUnrecoverableException
  467. {
  468. final Set<String> interestingUserAttributes = new HashSet<>();
  469. interestingUserAttributes.addAll(uiBean.getPasswordPolicy().getRuleHelper().getDisallowedAttributes());
  470. if (uiBean.getPasswordPolicy().getRuleHelper().getADComplexityLevel() == ADPolicyComplexity.AD2003
  471. || uiBean.getPasswordPolicy().getRuleHelper().getADComplexityLevel() == ADPolicyComplexity.AD2008) {
  472. interestingUserAttributes.add("sAMAccountName");
  473. interestingUserAttributes.add("displayName");
  474. interestingUserAttributes.add("fullname");
  475. interestingUserAttributes.add("cn");
  476. }
  477. return interestingUserAttributes;
  478. }
  479. private final Map<String,List<String>> cacheMap = new HashMap<>();
  480. @Override
  481. public String readStringAttribute(
  482. final String attribute
  483. )
  484. throws PwmUnrecoverableException
  485. {
  486. final Map<String,String> results = readStringAttributes(Collections.singletonList(attribute));
  487. if (results == null || results.isEmpty()) {
  488. return null;
  489. }
  490. return results.values().iterator().next();
  491. }
  492. @Override
  493. public Date readDateAttribute(final String attribute)
  494. throws PwmUnrecoverableException
  495. {
  496. try {
  497. return chaiUser.readDateAttribute(attribute);
  498. } catch (ChaiException e) {
  499. throw PwmUnrecoverableException.fromChaiException(e);
  500. }
  501. }
  502. @Override
  503. public List<String> readMultiStringAttribute(final String attribute)
  504. throws PwmUnrecoverableException
  505. {
  506. return readMultiStringAttributesImpl(Collections.singletonList(attribute)).get(attribute);
  507. }
  508. @Override
  509. public Map<String,String> readStringAttributes(
  510. final Collection<String> attributes
  511. )
  512. throws PwmUnrecoverableException
  513. {
  514. final Map<String,List<String>> valueMap = readMultiStringAttributesImpl(attributes);
  515. final Map<String,String> returnValue = new LinkedHashMap<>();
  516. for (final String key : valueMap.keySet()) {
  517. final List<String> values = valueMap.get(key);
  518. if (values != null && !values.isEmpty()) {
  519. returnValue.put(key, values.iterator().next());
  520. }
  521. }
  522. return returnValue;
  523. }
  524. private Map<String,List<String>> readMultiStringAttributesImpl(
  525. final Collection<String> attributes
  526. )
  527. throws PwmUnrecoverableException
  528. {
  529. if (chaiUser == null || attributes == null || attributes.isEmpty()) {
  530. return Collections.emptyMap();
  531. }
  532. // figure out uncached attributes.
  533. final Set<String> uncachedAttributes = new HashSet<>(attributes);
  534. uncachedAttributes.removeAll(cacheMap.keySet());
  535. // read uncached attributes into cache
  536. if (!uncachedAttributes.isEmpty()) {
  537. final Map<String,Map<String,List<String>>> results;
  538. try {
  539. results = chaiUser.getChaiProvider().searchMultiValues(
  540. chaiUser.getEntryDN(),
  541. "(objectclass=*)",
  542. uncachedAttributes,
  543. ChaiProvider.SEARCH_SCOPE.BASE
  544. );
  545. } catch (ChaiOperationException e) {
  546. final String msg = "ldap operational error while reading user data" + e.getMessage();
  547. LOGGER.error(sessionLabel, msg);
  548. throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_LDAP_DATA_ERROR,msg));
  549. } catch (ChaiUnavailableException e) {
  550. throw PwmUnrecoverableException.fromChaiException(e);
  551. }
  552. if (results == null || results.size() != 1) {
  553. final String msg = "ldap server did not return requested user entry "
  554. + chaiUser.getEntryDN()
  555. + " while attempting to read attribute data";
  556. LOGGER.error(sessionLabel, msg);
  557. throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_LDAP_DATA_ERROR,msg));
  558. }
  559. final Map<String,List<String>> allAttributeValues = results.values().iterator().next();
  560. for (final String attribute : uncachedAttributes) {
  561. final List<String> attributeValues = allAttributeValues.get(attribute);
  562. if (attributeValues == null) {
  563. cacheMap.put(attribute, Collections.emptyList());
  564. } else {
  565. cacheMap.put(attribute, Collections.unmodifiableList(attributeValues));
  566. }
  567. }
  568. }
  569. // build result data from cache
  570. final Map<String,List<String>> returnMap = new HashMap<>();
  571. for (final String attribute : attributes) {
  572. final List<String> cachedValue = cacheMap.get(attribute);
  573. returnMap.put(attribute, cachedValue);
  574. }
  575. return Collections.unmodifiableMap(returnMap);
  576. }
  577. @Override
  578. public boolean isRequiresInteraction() throws PwmUnrecoverableException
  579. {
  580. return selfCachedReference.isRequiresNewPassword()
  581. || selfCachedReference.isRequiresResponseConfig()
  582. || selfCachedReference.isRequiresUpdateProfile()
  583. || selfCachedReference.isRequiresOtpConfig()
  584. || selfCachedReference.getPasswordStatus().isWarnPeriod();
  585. }
  586. }