diff options
Diffstat (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java')
-rw-r--r-- | eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java | 139 |
1 files changed, 115 insertions, 24 deletions
diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java index e76d0728..dc391902 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthProcessDataWrapper.java @@ -19,9 +19,9 @@ package at.gv.egiz.eaaf.core.impl.idp.auth.data; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import java.io.Serializable; import java.time.Instant; +import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; @@ -33,19 +33,25 @@ import java.util.stream.Stream; import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.api.idp.EaafAuthProcessDataConstants; import at.gv.egiz.eaaf.core.api.idp.auth.data.IAuthProcessDataContainer; import at.gv.egiz.eaaf.core.api.idp.auth.data.IIdentityLink; +import at.gv.egiz.eaaf.core.exceptions.EaafJsonMapperException; import at.gv.egiz.eaaf.core.exceptions.EaafStorageException; - +import at.gv.egiz.eaaf.core.impl.utils.DefaultJsonMapper; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class AuthProcessDataWrapper implements IAuthProcessDataContainer, EaafAuthProcessDataConstants { - private static final Logger log = LoggerFactory.getLogger(AuthProcessDataWrapper.class); + public static final String GENERIC_DATA_PREFIX = "$GENERIC$_"; public static final String PATTERN_ISSUE_INSTANT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; protected Map<String, Object> authProcessData; @@ -57,7 +63,7 @@ public class AuthProcessDataWrapper @Override public Instant getIssueInstant() { - return wrapStoredObject(VALUE_ISSUEINSTANT, null, Instant.class); + return buildDateTimeUtcDate(wrapStoredObject(VALUE_ISSUEINSTANT, null, String.class)); } /* @@ -68,8 +74,7 @@ public class AuthProcessDataWrapper */ @Override public String getIssueInstantString() { - return buildDateTimeUtcString( - wrapStoredObject(VALUE_ISSUEINSTANT, null, Instant.class)); + return wrapStoredObject(VALUE_ISSUEINSTANT, null, String.class); } /* @@ -81,9 +86,13 @@ public class AuthProcessDataWrapper */ @Override public void setIssueInstant(final String issueInstant) { - authProcessData.put(VALUE_ISSUEINSTANT, - buildDateTimeUtcDate(issueInstant)); + if (buildDateTimeUtcDate(issueInstant) != null) { + authProcessData.put(VALUE_ISSUEINSTANT, issueInstant); + + } else if (StringUtils.isNotEmpty(issueInstant)) { + log.warn("Can not parse IssueInstant of authentication: {}", issueInstant); + } } /* @@ -95,7 +104,7 @@ public class AuthProcessDataWrapper */ @Override public void setIssueInstant(final Instant issueInstant) { - authProcessData.put(VALUE_ISSUEINSTANT, issueInstant); + authProcessData.put(VALUE_ISSUEINSTANT, buildDateTimeUtcString(issueInstant)); } @@ -257,8 +266,8 @@ public class AuthProcessDataWrapper * at.gv.egovernment.moa.id.auth.data.IAuthenticationSession#getSessionCreated() */ @Override - public Date getSessionCreated() { - return wrapStoredObject(EaafConstants.AUTH_DATA_CREATED, null, Date.class); + public Instant getSessionCreated() { + return buildDateTimeUtcDate(wrapStoredObject(EaafConstants.AUTH_DATA_CREATED, null, String.class)); } /* @@ -271,6 +280,15 @@ public class AuthProcessDataWrapper public Map<String, Object> getGenericSessionDataStorage() { return authProcessData.entrySet().stream() .filter(el -> el.getKey().startsWith(GENERIC_PREFIX)) + .map(el -> { + Object obj = el.getValue(); + if (obj instanceof String && ((String) obj).startsWith(GENERIC_DATA_PREFIX)) { + log.trace("Find generic JSON serialized data. Starting de-serialization ... "); + el.setValue(objectSaveJsonDeserialization(obj)); + + } + return el; + }) .collect( Collectors.toMap( el -> el.getKey().substring(GENERIC_PREFIX.length()), @@ -292,7 +310,8 @@ public class AuthProcessDataWrapper */ @Override public Object getGenericDataFromSession(final String key) { - return authProcessData.get(GENERIC_PREFIX + key); + return wrapStoredObject(GENERIC_PREFIX + key, null, Object.class); + } /* @@ -304,6 +323,7 @@ public class AuthProcessDataWrapper @Override public <T> T getGenericDataFromSession(final String key, final Class<T> clazz) { return wrapStoredObject(GENERIC_PREFIX + key, null, clazz); + } /* @@ -315,7 +335,7 @@ public class AuthProcessDataWrapper @Override public void setGenericDataToSession(final String key, final Object object) throws EaafStorageException { - authProcessData.put(GENERIC_PREFIX + key, object); + authProcessData.put(GENERIC_PREFIX + key, objectSaveJsonSerialization(object)); } @@ -323,19 +343,40 @@ public class AuthProcessDataWrapper final Class<T> clazz) { if (StringUtils.isNotEmpty(key)) { final Object obj = authProcessData.get(key); - if (obj != null && clazz.isInstance(obj)) { + + // check if it is a generic container + if (obj instanceof String && ((String) obj).startsWith(GENERIC_DATA_PREFIX)) { + log.trace("Find generic JSON serialized data. Starting de-serialization ... "); + return (T) objectSaveJsonDeserialization(obj); + + // check if object is already of valid type + } else if (obj != null && clazz.isInstance(obj)) { return (T) obj; + // check if byte[] was requested and object is of type String in that case it's + // B64 content } else if (obj instanceof String && clazz.equals(byte[].class)) { - return (T) java.util.Base64.getDecoder().decode((String) obj); + try { + return (T) java.util.Base64.getDecoder().decode((String) obj); + + } catch (IllegalArgumentException e) { + log.warn("Can not decode session-object with key: {}", key, e); + + } + } else if (obj != null) { + log.warn("Can not unwrap data object with key: {} because requires class: {} but has: {}", + key, clazz.getName(), obj.getClass().getName()); } } + // return default value if we get not other result if (defaultValue == null) { return null; + } else if (clazz.isInstance(defaultValue)) { return (T) defaultValue; + } else { log.error("DefaultValue: " + defaultValue.getClass().getName() + " is not of Type:" + clazz.getName()); @@ -376,20 +417,70 @@ public class AuthProcessDataWrapper */ @Nullable public static Instant buildDateTimeUtcDate(@Nullable final String date) { - final SimpleDateFormat f = new SimpleDateFormat(PATTERN_ISSUE_INSTANT); + if (date != null) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN_ISSUE_INSTANT); + return LocalDateTime.parse(date, dateTimeFormatter).atZone(ZoneId.of("UTC")).toInstant(); + + } + + return null; + + } + + private Object objectSaveJsonSerialization(Object object) throws EaafStorageException { try { - if (date != null) { - return f.parse(date).toInstant(); + if (object == null || object instanceof String || object instanceof Boolean + || object instanceof Integer) { + return object; - } + } else { + return GENERIC_DATA_PREFIX + DefaultJsonMapper.serialize(RawDataHolder.builder() + .object(DefaultJsonMapper.serialize(object)) + .clazzType(object.getClass().getName()) + .build()); - } catch (final ParseException e) { - log.error("Can NOT parse Date from String: {}", date, null, e); + } + } catch (EaafJsonMapperException e) { + throw new EaafStorageException("Can no serialize object to JSON", e); } + } + private Object objectSaveJsonDeserialization(Object data) { + try { + if (data == null) { + return null; + + } else if (data instanceof String) { + RawDataHolder holder = (RawDataHolder) DefaultJsonMapper.deserialize( + ((String) data).substring(GENERIC_DATA_PREFIX.length()), RawDataHolder.class); + Class<?> clz = Class.forName(holder.getClazzType()); + return DefaultJsonMapper.deserialize(holder.getObject(), clz); + + } else { + log.error("Can not deserialize: {} because it's not of type String", data.getClass().getName()); + + } + } catch (final Exception e) { + log.warn("Generic request-data object can not be casted to requested type", e); + + } return null; } + @Data + @Builder + @NoArgsConstructor + @AllArgsConstructor + private static class RawDataHolder implements Serializable { + + private static final long serialVersionUID = -3827971372737399528L; + + private String object; + + private String clazzType; + + } + } |