use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.
the class ChallengeValue method factory.
public static StoredValueFactory factory() {
return new StoredValueFactory() {
public ChallengeValue fromJson(final String input) {
if (input == null) {
return new ChallengeValue(Collections.<String, List<ChallengeItemConfiguration>>emptyMap());
} else {
Map<String, List<ChallengeItemConfiguration>> srcMap = JsonUtil.deserialize(input, new TypeToken<Map<String, List<ChallengeItemConfiguration>>>() {
});
srcMap = srcMap == null ? Collections.<String, List<ChallengeItemConfiguration>>emptyMap() : new TreeMap<>(srcMap);
return new ChallengeValue(Collections.unmodifiableMap(srcMap));
}
}
public ChallengeValue fromXmlElement(final Element settingElement, final PwmSecurityKey input) {
final List valueElements = settingElement.getChildren("value");
final Map<String, List<ChallengeItemConfiguration>> values = new TreeMap<>();
final boolean oldStyle = "LOCALIZED_STRING_ARRAY".equals(settingElement.getAttributeValue("syntax"));
for (final Object loopValue : valueElements) {
final Element loopValueElement = (Element) loopValue;
final String localeString = loopValueElement.getAttributeValue("locale") == null ? "" : loopValueElement.getAttributeValue("locale");
final String value = loopValueElement.getText();
if (!values.containsKey(localeString)) {
values.put(localeString, new ArrayList<ChallengeItemConfiguration>());
}
final ChallengeItemConfiguration challengeItemBean;
if (oldStyle) {
challengeItemBean = parseOldVersionString(value);
} else {
challengeItemBean = JsonUtil.deserialize(value, ChallengeItemConfiguration.class);
}
if (challengeItemBean != null) {
values.get(localeString).add(challengeItemBean);
}
}
return new ChallengeValue(values);
}
};
}
use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.
the class X509CertificateValue method factory.
public static StoredValueFactory factory() {
return new StoredValueFactory() {
public X509CertificateValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) {
final List<X509Certificate> certificates = new ArrayList<>();
final List<Element> valueElements = settingElement.getChildren("value");
for (final Element loopValueElement : valueElements) {
final String b64encodedStr = loopValueElement.getText();
try {
certificates.add(X509Utils.certificateFromBase64(b64encodedStr));
} catch (Exception e) {
LOGGER.error("error decoding certificate: " + e.getMessage());
}
}
return new X509CertificateValue(certificates.toArray(new X509Certificate[certificates.size()]));
}
public X509CertificateValue fromJson(final String input) {
return new X509CertificateValue(new X509Certificate[0]);
}
};
}
use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.
the class CryptoCookieBeanImpl method keyForSession.
private PwmSecurityKey keyForSession(final PwmRequest pwmRequest) throws PwmUnrecoverableException {
final PasswordData configKey = pwmRequest.getConfig().readSettingAsPassword(PwmSetting.PWM_SECURITY_KEY);
final String userGuid = pwmRequest.getPwmSession().getLoginInfoBean().getGuid();
return new PwmSecurityKey(configKey.getStringValue() + userGuid);
}
use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.
the class CryptoCookieBeanImpl method getSessionBean.
@Override
public <E extends PwmSessionBean> E getSessionBean(final PwmRequest pwmRequest, final Class<E> theClass) throws PwmUnrecoverableException {
final Map<Class<? extends PwmSessionBean>, PwmSessionBean> sessionBeans = getRequestBeanMap(pwmRequest);
if (sessionBeans.containsKey(theClass) && sessionBeans.get(theClass) != null) {
return (E) sessionBeans.get(theClass);
}
final String sessionGuid = pwmRequest.getPwmSession().getLoginInfoBean().getGuid();
final String cookieName = nameForClass(theClass);
try {
final String rawValue = pwmRequest.readCookie(cookieName);
final PwmSecurityKey key = keyForSession(pwmRequest);
final E cookieBean = pwmRequest.getPwmApplication().getSecureService().decryptObject(rawValue, key, theClass);
if (validateCookie(pwmRequest, cookieName, cookieBean)) {
sessionBeans.put(theClass, cookieBean);
return cookieBean;
}
} catch (PwmException e) {
LOGGER.debug(pwmRequest, "ignoring existing existing " + cookieName + " cookie bean due to error: " + e.getMessage());
}
final E newBean = SessionStateService.newBean(sessionGuid, theClass);
sessionBeans.put(theClass, newBean);
return newBean;
}
use of password.pwm.util.secure.PwmSecurityKey in project pwm by pwm-project.
the class CryptoCookieBeanImpl method saveSessionBeans.
public void saveSessionBeans(final PwmRequest pwmRequest) {
if (pwmRequest == null || pwmRequest.getPwmResponse().isCommitted()) {
return;
}
try {
if (pwmRequest != null && pwmRequest.getPwmResponse() != null) {
final Map<Class<? extends PwmSessionBean>, PwmSessionBean> beansInRequest = getRequestBeanMap(pwmRequest);
if (beansInRequest != null) {
for (final Map.Entry<Class<? extends PwmSessionBean>, PwmSessionBean> entry : beansInRequest.entrySet()) {
final Class<? extends PwmSessionBean> theClass = entry.getKey();
final String cookieName = nameForClass(theClass);
final PwmSessionBean bean = entry.getValue();
if (bean == null) {
pwmRequest.getPwmResponse().removeCookie(cookieName, COOKIE_PATH);
} else {
final PwmSecurityKey key = keyForSession(pwmRequest);
final String encrytedValue = pwmRequest.getPwmApplication().getSecureService().encryptObjectToString(entry.getValue(), key);
pwmRequest.getPwmResponse().writeCookie(cookieName, encrytedValue, -1, COOKIE_PATH);
}
}
}
}
} catch (PwmUnrecoverableException e) {
LOGGER.error(pwmRequest, "error writing cookie bean to response: " + e.getMessage(), e);
}
}
Aggregations