From 3cd52ab38e4d57b6f056dbf47897d80f419522df Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 19 Apr 2021 18:58:31 +0200 Subject: refactor DateOfBirth in IAuthData and all implementations because DateOfBirth from SZR has not be a valid Date object --- .../eaaf/core/impl/idp/AuthenticationData.java | 70 ++++++++++-------- .../core/impl/idp/auth/services/IErrorService.java | 10 ++- .../attributes/BirthdateAttributeBuilder.java | 10 +-- .../idp/auth/AuthenticationDataBuilderTest.java | 2 +- .../auth/attributes/BirthdayAttrBuilderTest.java | 2 +- .../impl/idp/auth/data/AuthenticationDataTest.java | 85 ++++++++++++++++++++++ 6 files changed, 136 insertions(+), 43 deletions(-) create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthenticationDataTest.java (limited to 'eaaf_core/src') diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/AuthenticationData.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/AuthenticationData.java index 00324a9a..4fe22feb 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/AuthenticationData.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/AuthenticationData.java @@ -26,6 +26,9 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.TimeZone; +import java.util.regex.Pattern; + +import javax.annotation.Nullable; import org.apache.commons.collections4.map.HashedMap; import org.apache.commons.lang3.StringUtils; @@ -47,6 +50,7 @@ public class AuthenticationData implements IAuthData, Serializable { private static final long serialVersionUID = -1042697056735596866L; public static final String IDENTITY_LINK_DATE_FORMAT = "yyyy-MM-dd"; + public static final String IDENTITY_LINK_DATE_REGEX = "([0-9]{4})-([0-9]{2})-([0-9]{2})"; public static final String ISSUE_INSTANT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; private boolean isBaseIdTransferRestrication = true; @@ -64,7 +68,7 @@ public class AuthenticationData implements IAuthData, Serializable { private String familyName; private String givenName; - private Date dateOfBirth; + private String dateOfBirth; private String encSourceId; private String encSourceIdType; @@ -147,22 +151,36 @@ public class AuthenticationData implements IAuthData, Serializable { } @Override - public Date getDateOfBirth() { - return getDateCopyOrNull(this.dateOfBirth); + public String getDateOfBirth() { + return this.dateOfBirth; } @Override - public String getFormatedDateOfBirth() { - final DateFormat pvpDateFormat = new SimpleDateFormat(IDENTITY_LINK_DATE_FORMAT); - if (getDateOfBirth() != null) { - return pvpDateFormat.format(getDateOfBirth()); - } else { - return "2999-12-31"; - } - + public String getDateOfBirthFormated(String pattern) { + if (StringUtils.isNotEmpty(getDateOfBirth())) { + try { + final DateFormat dateFormat = new SimpleDateFormat(IDENTITY_LINK_DATE_FORMAT); + Date parsedDate = dateFormat.parse(getDateOfBirth()); + final String dateString = dateFormat.format(parsedDate); + if (getDateOfBirth().equals(dateString)) { + final DateFormat destDateFormat = new SimpleDateFormat(pattern); + return destDateFormat.format(parsedDate); + + } else { + log.info("DateOfBirth has an unusal format. Can not be converted to: {}", pattern); + + } + + } catch (ParseException | IllegalArgumentException e) { + log.error("Can not parse DateOfBirth.", e); + + } + } + return null; + } - + @Override public String getFamilyName() { return this.familyName; @@ -216,26 +234,16 @@ public class AuthenticationData implements IAuthData, Serializable { * * @param dateOfBirth The dateOfBirth to set */ - public void setDateOfBirth(final Date dateOfBirth) { - this.dateOfBirth = getDateCopyOrNull(dateOfBirth); - } - - /** - * Set the date of birth. - * - * @param dateOfBirth date of birth String as "yyyy-MM-dd" - */ - public void setDateOfBirth(final String dateOfBirth) { - try { - if (StringUtils.isNotEmpty(dateOfBirth)) { - final DateFormat identityLinkFormat = new SimpleDateFormat(IDENTITY_LINK_DATE_FORMAT); - this.dateOfBirth = identityLinkFormat.parse(dateOfBirth); - + public void setDateOfBirth(@Nullable final String dateOfBirth) { + if (dateOfBirth != null) { + if (Pattern.matches(IDENTITY_LINK_DATE_REGEX, dateOfBirth)) { + this.dateOfBirth = dateOfBirth; + + } else { + log.error("DateOfBirth: {} does NOT match to pattern: {}", + dateOfBirth, IDENTITY_LINK_DATE_REGEX); + } - - } catch (final ParseException e) { - log.warn("Parse dateOfBirht from IdentityLink FAILED", e); - } } diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorService.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorService.java index 812a5171..4e04a87f 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorService.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorService.java @@ -1,10 +1,12 @@ package at.gv.egiz.eaaf.core.impl.idp.auth.services; -import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; -import at.gv.egiz.eaaf.core.exceptions.EaafException; +import java.util.HashSet; +import javax.annotation.Nonnull; import javax.servlet.http.HttpServletRequest; -import java.util.HashSet; + +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafException; public interface IErrorService { /** @@ -35,6 +37,7 @@ public interface IErrorService { * @param internalCode internal error code * @return external error code */ + @Nonnull String getExternalCodeFromInternal(String internalCode); /** @@ -45,6 +48,7 @@ public interface IErrorService { * @return eror handle Data * @throws EaafException In case of an internal error */ + @Nonnull IHandleData createHandleData(Throwable throwable, HttpServletRequest req) throws EaafException; /** diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/builder/attributes/BirthdateAttributeBuilder.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/builder/attributes/BirthdateAttributeBuilder.java index 19500cb3..a82a1a55 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/builder/attributes/BirthdateAttributeBuilder.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/builder/attributes/BirthdateAttributeBuilder.java @@ -19,8 +19,7 @@ package at.gv.egiz.eaaf.core.impl.idp.builder.attributes; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import org.apache.commons.lang3.StringUtils; import at.gv.egiz.eaaf.core.api.idp.IAttributeGenerator; import at.gv.egiz.eaaf.core.api.idp.IAuthData; @@ -41,11 +40,8 @@ public class BirthdateAttributeBuilder implements IPvpAttributeBuilder { public ATT build(final ISpConfiguration oaParam, final IAuthData authData, final IAttributeGenerator g) throws AttributeBuilderException { - if (authData.getDateOfBirth() != null) { - final DateFormat pvpDateFormat = new SimpleDateFormat(BIRTHDATE_FORMAT_PATTERN); - final String dateString = pvpDateFormat.format(authData.getDateOfBirth()); - - return g.buildStringAttribute(BIRTHDATE_FRIENDLY_NAME, BIRTHDATE_NAME, dateString); + if (StringUtils.isNotEmpty(authData.getDateOfBirth())) { + return g.buildStringAttribute(BIRTHDATE_FRIENDLY_NAME, BIRTHDATE_NAME, authData.getDateOfBirth()); } else { throw new UnavailableAttributeException(BIRTHDATE_NAME); diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/AuthenticationDataBuilderTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/AuthenticationDataBuilderTest.java index 33bd1010..19054634 100644 --- a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/AuthenticationDataBuilderTest.java +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/AuthenticationDataBuilderTest.java @@ -307,7 +307,7 @@ public class AuthenticationDataBuilderTest { throw new Exception("GivenName wrong"); } - if (!authData.getFormatedDateOfBirth().equals("1973-06-04")) { + if (!authData.getDateOfBirth().equals("1973-06-04")) { throw new Exception("DateOfBirth wrong"); } diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/attributes/BirthdayAttrBuilderTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/attributes/BirthdayAttrBuilderTest.java index 21cf71a9..f155b3b4 100644 --- a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/attributes/BirthdayAttrBuilderTest.java +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/attributes/BirthdayAttrBuilderTest.java @@ -29,7 +29,7 @@ public class BirthdayAttrBuilderTest extends AbstractAttributeBuilderTest { final DateFormat format = new SimpleDateFormat(PvpAttributeDefinitions.BIRTHDATE_FORMAT_PATTERN); Assert.assertEquals("Birthday does NOT match", authData.getDateOfBirth(), - format.parse(value)); + value); } catch (final Exception e) { Assert.assertTrue("Attr. builder has an exception", e == null); diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthenticationDataTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthenticationDataTest.java new file mode 100644 index 00000000..9d99b158 --- /dev/null +++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/auth/data/AuthenticationDataTest.java @@ -0,0 +1,85 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.data; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; + +import at.gv.egiz.eaaf.core.impl.idp.AuthenticationData; + +@RunWith(BlockJUnit4ClassRunner.class) +public class AuthenticationDataTest { + + @Test + public void formatedDateButNotSet() { + AuthenticationData authData = new AuthenticationData(); + assertNull("formatedDateOfBirth", authData.getDateOfBirthFormated("dd/MM/yyy")); + + } + + @Test + public void formatedDateButInvalidPattern() { + AuthenticationData authData = new AuthenticationData(); + authData.setDateOfBirth("1940-01-01"); + assertNull("formatedDateOfBirth", authData.getDateOfBirthFormated("aa-bb-cccc")); + + } + + @Test + public void validDateStatic() { + AuthenticationData authData = new AuthenticationData(); + + String date = "1940-01-01"; + authData.setDateOfBirth(date); + + assertNotNull("birthday", authData.getDateOfBirth()); + assertEquals("birthday attribute", date, + authData.getDateOfBirth()); + assertEquals("birthday attribute", "01/01/1940", + authData.getDateOfBirthFormated("dd/MM/yyy")); + + } + + + @Test + public void validButNotUsal() { + AuthenticationData authData = new AuthenticationData(); + authData.setDateOfBirth("1970-00-00"); + + assertNotNull("birthday", authData.getDateOfBirth()); + assertEquals("birthday attribute", "1970-00-00", + authData.getDateOfBirth()); + assertNull("formatedDateOfBirth", authData.getDateOfBirthFormated("dd/MM/yyy")); + + } + + @Test + public void invalidDate() { + AuthenticationData authData = new AuthenticationData(); + authData.setDateOfBirth("1970/00/00"); + + assertNull("birthday", authData.getDateOfBirth()); + assertNull("formatedDateOfBirth", authData.getDateOfBirthFormated("dd/MM/yyy")); + + } + + + @Test + public void validDateRandom() { + AuthenticationData authData = new AuthenticationData(); + + String date = RandomStringUtils.randomNumeric(4) + "-" + RandomStringUtils.randomNumeric(2) + + "-" + RandomStringUtils.randomNumeric(2); + authData.setDateOfBirth(date); + + assertNotNull("birthday", authData.getDateOfBirth()); + assertEquals("birthday attribute", date, + authData.getDateOfBirth()); + + } + +} -- cgit v1.2.3