From cbaf5a3a308719a6a6515a647fcf05eb0e286e2a Mon Sep 17 00:00:00 2001 From: aasaru Date: Fri, 14 Jan 2022 14:45:38 +0200 Subject: [PATCH] SLIB-13 non-Baltic countries date-of-birth parsing caused error (#62) Co-authored-by: Juhan Aasaru --- CHANGELOG.md | 5 +++++ .../AuthenticationResponseValidator.java | 2 +- .../util/NationalIdentityNumberUtil.java | 9 ++++++--- .../util/NationalIdentityNumberUtilTest.java | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf29df..2232434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [2.1.4] - Upcoming + +### Fixed +- bug where non-Baltic certificates without date-of-birth resulted with an exception + ## [2.1.3] - 2021-12-22 ### Fixed diff --git a/src/main/java/ee/sk/smartid/AuthenticationResponseValidator.java b/src/main/java/ee/sk/smartid/AuthenticationResponseValidator.java index d0e8402..3c44daa 100644 --- a/src/main/java/ee/sk/smartid/AuthenticationResponseValidator.java +++ b/src/main/java/ee/sk/smartid/AuthenticationResponseValidator.java @@ -299,7 +299,7 @@ else if (rdn.getType().equalsIgnoreCase("C")) { } } - private static LocalDate getDateOfBirth(AuthenticationIdentity identity) { + public static LocalDate getDateOfBirth(AuthenticationIdentity identity) { return Optional.ofNullable( CertificateAttributeUtil.getDateOfBirth(identity.getAuthCertificate())) .orElse(NationalIdentityNumberUtil.getDateOfBirth(identity)); diff --git a/src/main/java/ee/sk/smartid/util/NationalIdentityNumberUtil.java b/src/main/java/ee/sk/smartid/util/NationalIdentityNumberUtil.java index dc439c9..7a688bf 100644 --- a/src/main/java/ee/sk/smartid/util/NationalIdentityNumberUtil.java +++ b/src/main/java/ee/sk/smartid/util/NationalIdentityNumberUtil.java @@ -17,14 +17,17 @@ public class NationalIdentityNumberUtil { .withResolverStyle(ResolverStyle.STRICT); /** - * Detect date-of-birth from national identification number if possible or return null. + * Detect date-of-birth from a Baltic national identification number if possible or return null. * * This method always returns the value for all Estonian and Lithuanian national identification numbers. * * It also works for older Latvian personal codes but Latvian personal codes issued after July 1st 2017 * (starting with "32") do not carry date-of-birth. * - * Some (but not all) Smart-ID certificates have date-of-birth on a separate attribute. + * For non-Baltic countries (countries other than Estonia, Latvia or Lithuania) it always returns null + * (even if it would be possible to deduce date of birth from national identity number). + * + * Newer (but not all) Smart-ID certificates have date-of-birth on a separate attribute. * It is recommended to use that value if present. * @see CertificateAttributeUtil#getDateOfBirth(java.security.cert.X509Certificate) * @@ -41,7 +44,7 @@ public static LocalDate getDateOfBirth(AuthenticationIdentity authenticationIden case "LV": return parseLvDateOfBirth(identityNumber); default: - throw new UnprocessableSmartIdResponseException("Unknown country: " + authenticationIdentity.getCountry()); + return null; } } diff --git a/src/test/java/ee/sk/smartid/util/NationalIdentityNumberUtilTest.java b/src/test/java/ee/sk/smartid/util/NationalIdentityNumberUtilTest.java index 614b8da..7c311cd 100644 --- a/src/test/java/ee/sk/smartid/util/NationalIdentityNumberUtilTest.java +++ b/src/test/java/ee/sk/smartid/util/NationalIdentityNumberUtilTest.java @@ -96,4 +96,22 @@ public void parseLvDateOfBirth_invalidIdCode_throwsException() { assertThat(exception.getMessage(), is("Unable get birthdate from Latvian personal code 331265-0234")); } + @Test + public void getDateOfBirthFromIdCode_sweden_returnsNull() { + AuthenticationIdentity identity = new AuthenticationIdentity(); + identity.setCountry("SE"); + identity.setIdentityNumber("1995012-79039"); + + assertThat(NationalIdentityNumberUtil.getDateOfBirth(identity), is(nullValue())); + } + + @Test + public void getDateOfBirthFromIdCode_poland_returnsNull() { + AuthenticationIdentity identity = new AuthenticationIdentity(); + identity.setCountry("PL"); + identity.setIdentityNumber("64120301283"); + + assertThat(NationalIdentityNumberUtil.getDateOfBirth(identity), is(nullValue())); + } + } \ No newline at end of file