From 4755e70c14d2eb22fc3eb496f3e530ce07d75bf1 Mon Sep 17 00:00:00 2001 From: Angel Botev Date: Sat, 10 Aug 2019 16:36:31 +0300 Subject: [PATCH 1/5] NY-7952 Fix validation of phone number - add only parse, remove validation; Signed-off-by: Angel Botev --- .../account/kafka/KafkaProducerConfig.java | 2 ++ .../nynja/account/validation/Validators.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java b/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java index b9f2ebc..16ed9c7 100644 --- a/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java +++ b/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java @@ -41,6 +41,8 @@ public class KafkaProducerConfig { configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_HOST + ":" + KAFKA_PORT); configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); + // Check config below if kakfa is down + //configProps.put(ProducerConfig.RETRIES_CONFIG, 10); return new DefaultKafkaProducerFactory(configProps); diff --git a/src/main/java/biz/nynja/account/validation/Validators.java b/src/main/java/biz/nynja/account/validation/Validators.java index 3d589f3..e52244d 100644 --- a/src/main/java/biz/nynja/account/validation/Validators.java +++ b/src/main/java/biz/nynja/account/validation/Validators.java @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import biz.nynja.account.grpc.AddAuthenticationProviderRequest; import biz.nynja.account.grpc.AuthProviderDetails; @@ -465,6 +466,27 @@ public class Validators { return true; } + + public boolean isParsablePhoneNumber(String phoneNumber) { + + logger.debug("Checking validity of phone number {}", phoneNumber); + + phoneNumber = phoneNumber.replaceAll("^0*", "").replaceAll("[^\\d]", ""); + // add + to phoneNumber. It must be with plus. Read documentation of phoneUtil.parse + + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + Phonenumber.PhoneNumber pn = new PhoneNumber(); + try { + pn = phoneUtil.parse(phoneNumber, ""); + } catch (NumberParseException e) { + logger.error("Phone number parse exception for number: {}", phoneNumber); + return false; + } + + logger.debug("PhoneNumber: {} can be parsed!", phoneNumber); + + return true; + } } public static class Util { -- GitLab From 9d351ff2b67e81494cdcfda63adde96005089b49 Mon Sep 17 00:00:00 2001 From: Angel Botev Date: Mon, 12 Aug 2019 20:20:25 +0300 Subject: [PATCH 2/5] NY-7952 Fix validation of phone number - add normalizer; - remove validator; - change searchByPhoneNumber endpoint; Signed-off-by: Angel Botev --- .../account/phone/PhoneNumberNormalizer.java | 24 +++++++++++++++++++ .../account/services/AccountServiceImpl.java | 2 +- .../decomposition/AccountProvider.java | 16 +++++++++++++ .../nynja/account/validation/Validators.java | 20 ---------------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java index 6c09984..c2d54e4 100644 --- a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java +++ b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import biz.nynja.account.grpc.AddContactInfoRequest; import biz.nynja.account.grpc.AuthProviderDetails; @@ -147,4 +148,27 @@ public class PhoneNumberNormalizer { } return country + ":" + normalizedPhoneNumber; } + + public String getNormalizedPhoneNumberWithoutSelector(String rawPhoneNumber) throws InvalidPhoneNumberException { + logger.info("libphone: New phone number normalization request received - phone number: {}", + rawPhoneNumber); + + String phone = rawPhoneNumber.replaceAll("^0*", "").replaceAll("[^\\d]", ""); + phone = "+" + phone; + + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + + String normalizedPhoneNumber = ""; + Phonenumber.PhoneNumber pn = new PhoneNumber(); + try { + pn = phoneUtil.parse(phone, ""); + normalizedPhoneNumber = Integer.toString(pn.getCountryCode()) + Long.toString(pn.getNationalNumber()); + logger.info("libphone: Normalized phone number: " + normalizedPhoneNumber); + } catch (NumberParseException e) { + logger.error("libphone: NumberParseException was thrown: {}", e.toString()); + logger.debug("libphone: NumberParseException was thrown: {}", e.getCause()); + } + return normalizedPhoneNumber; + } + } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index e443b9e..8af84ac 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -231,7 +231,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas Optional account = Optional.empty(); try { - account = accountProvider.searchAccountByLoginOption(AuthenticationType.PHONE, request.getPhoneNumber()); + account = accountProvider.searchAccountByLoginOptionWithoutSelector(AuthenticationType.PHONE, request.getPhoneNumber()); } catch (IncorrectAccountCountException e) { logAndBuildGrpcSearchResponse(responseObserver, SearchResponse.newBuilder(), "Error while searching for phone: ", request.getPhoneNumber(), Cause.INTERNAL_SERVER_ERROR, ""); diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java index a062706..1e82f08 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java @@ -136,6 +136,22 @@ public class AccountProvider { return Optional.of(accountByProfileId.get().toAccount()); } + public Optional searchAccountByLoginOptionWithoutSelector(AuthenticationType type, String authenticationIdentifier) + throws IncorrectAccountCountException { + if (type == AuthenticationType.PHONE) { + authenticationIdentifier = phoneNumberNormalizer.getNormalizedPhoneNumberWithoutSelector(authenticationIdentifier); + } + + Optional accountByProfileId = accountRepositoryAdditional.searchAccountByLoginOption( + AuthenticationProvider.createAuthenticationProviderFromStringsWithDefaultSearchableOption( + type.toString(), authenticationIdentifier)); + if (!accountByProfileId.isPresent()) { + return Optional.empty(); + } + + return Optional.of(accountByProfileId.get().toAccount()); + } + public Optional getAccountResponseByAuthenticationProvider(AuthenticationType type, String authenticationIdentifier) { Optional account = getAccountByAuthenticationProvider(type, authenticationIdentifier); diff --git a/src/main/java/biz/nynja/account/validation/Validators.java b/src/main/java/biz/nynja/account/validation/Validators.java index e52244d..78d2080 100644 --- a/src/main/java/biz/nynja/account/validation/Validators.java +++ b/src/main/java/biz/nynja/account/validation/Validators.java @@ -467,26 +467,6 @@ public class Validators { return true; } - public boolean isParsablePhoneNumber(String phoneNumber) { - - logger.debug("Checking validity of phone number {}", phoneNumber); - - phoneNumber = phoneNumber.replaceAll("^0*", "").replaceAll("[^\\d]", ""); - // add + to phoneNumber. It must be with plus. Read documentation of phoneUtil.parse - - PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); - Phonenumber.PhoneNumber pn = new PhoneNumber(); - try { - pn = phoneUtil.parse(phoneNumber, ""); - } catch (NumberParseException e) { - logger.error("Phone number parse exception for number: {}", phoneNumber); - return false; - } - - logger.debug("PhoneNumber: {} can be parsed!", phoneNumber); - - return true; - } } public static class Util { -- GitLab From c5c5d6b60647af587f99382c2017b3dee7cee9b6 Mon Sep 17 00:00:00 2001 From: Stoyan Tzenkov Date: Tue, 13 Aug 2019 11:53:46 +0300 Subject: [PATCH 3/5] NY-7952: Fix implemented to handle NumberParseException. Signed-off-by: Stoyan Tzenkov --- .../nynja/account/phone/PhoneNumberNormalizer.java | 3 ++- .../nynja/account/services/AccountServiceImpl.java | 13 +++++++------ .../services/decomposition/AccountProvider.java | 3 ++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java index c2d54e4..0b98c4a 100644 --- a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java +++ b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java @@ -153,7 +153,7 @@ public class PhoneNumberNormalizer { logger.info("libphone: New phone number normalization request received - phone number: {}", rawPhoneNumber); - String phone = rawPhoneNumber.replaceAll("^0*", "").replaceAll("[^\\d]", ""); + String phone = rawPhoneNumber.replaceAll("[^\\d]", "").replaceAll("^0*", ""); phone = "+" + phone; PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); @@ -167,6 +167,7 @@ public class PhoneNumberNormalizer { } catch (NumberParseException e) { logger.error("libphone: NumberParseException was thrown: {}", e.toString()); logger.debug("libphone: NumberParseException was thrown: {}", e.getCause()); + throw new InvalidPhoneNumberException("Phone number with wrong format: " + rawPhoneNumber); } return normalizedPhoneNumber; } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index 8af84ac..f5bcaee 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -67,6 +67,7 @@ import biz.nynja.account.permissions.PerformPermissionCheck; import biz.nynja.account.permissions.PermissionsValidator; import biz.nynja.account.permissions.Permitted; import biz.nynja.account.permissions.RoleConstants; +import biz.nynja.account.phone.InvalidPhoneNumberException; import biz.nynja.account.phone.PhoneNumberNormalizer; import biz.nynja.account.repositories.AccountByProfileIdRepository; import biz.nynja.account.repositories.AccountByQrCodeRepository; @@ -222,12 +223,6 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas Cause.MISSING_PHONENUMBER, ""); return; } - if (!phoneValidator.isPhoneNumberValid(request.getPhoneNumber())) { - logAndBuildGrpcSearchResponse(responseObserver, SearchResponse.newBuilder(), - "Invalid phone number. Value: {}", request.getPhoneNumber(), Cause.INVALID_PHONENUMBER, - "Phone number parameter has invalid format."); - return; - } Optional account = Optional.empty(); try { @@ -235,6 +230,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } catch (IncorrectAccountCountException e) { logAndBuildGrpcSearchResponse(responseObserver, SearchResponse.newBuilder(), "Error while searching for phone: ", request.getPhoneNumber(), Cause.INTERNAL_SERVER_ERROR, ""); + return; + } catch (InvalidPhoneNumberException e) { + logAndBuildGrpcSearchResponse(responseObserver, SearchResponse.newBuilder(), + "Invalid phone number. Value: {}", request.getPhoneNumber(), Cause.INVALID_PHONENUMBER, + "Phone number parameter has invalid format."); + return; } if (!account.isPresent()) { diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java index 1e82f08..3065030 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java @@ -26,6 +26,7 @@ import biz.nynja.account.models.AccountByProfileId; import biz.nynja.account.models.AccountByQrCode; import biz.nynja.account.models.AccountByUsername; import biz.nynja.account.models.AuthenticationProvider; +import biz.nynja.account.phone.InvalidPhoneNumberException; import biz.nynja.account.phone.PhoneNumberNormalizer; import biz.nynja.account.repositories.AccountByProfileIdRepository; import biz.nynja.account.repositories.AccountByQrCodeRepository; @@ -137,7 +138,7 @@ public class AccountProvider { } public Optional searchAccountByLoginOptionWithoutSelector(AuthenticationType type, String authenticationIdentifier) - throws IncorrectAccountCountException { + throws IncorrectAccountCountException, InvalidPhoneNumberException { if (type == AuthenticationType.PHONE) { authenticationIdentifier = phoneNumberNormalizer.getNormalizedPhoneNumberWithoutSelector(authenticationIdentifier); } -- GitLab From c1ca99023194ad18341072316034d21659a2728f Mon Sep 17 00:00:00 2001 From: Stoyan Tzenkov Date: Tue, 13 Aug 2019 15:16:48 +0300 Subject: [PATCH 4/5] NY-7952: Unit tests fixed. Signed-off-by: Stoyan Tzenkov --- .../nynja/account/services/AccountServiceTests.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 955277f..148df8d 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -8,7 +8,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.LinkedList; @@ -104,6 +106,7 @@ import biz.nynja.account.services.decomposition.IncorrectAccountCountException; import biz.nynja.account.services.decomposition.ProfileProvider; import biz.nynja.account.utils.GrpcServerTestBase; import biz.nynja.account.utils.Util; +import biz.nynja.account.phone.InvalidPhoneNumberException; import io.grpc.Metadata; import io.grpc.stub.MetadataUtils; @@ -1546,7 +1549,7 @@ public class AccountServiceTests extends GrpcServerTestBase { Optional response = Optional.of(savedAccount); - given(accountProvider.searchAccountByLoginOption(AuthenticationType.PHONE, request.getPhoneNumber())) + given(accountProvider.searchAccountByLoginOptionWithoutSelector(AuthenticationType.PHONE, request.getPhoneNumber())) .willReturn(response); final SearchResponse reply = searchServiceBlockingStub.searchByPhoneNumber(request); @@ -1585,10 +1588,13 @@ public class AccountServiceTests extends GrpcServerTestBase { } @Test - public void testSearchByPhoneNumberInvalid() { + public void testSearchByPhoneNumberInvalid() throws InvalidPhoneNumberException, IncorrectAccountCountException { + Exception ex = new InvalidPhoneNumberException("Invalid phone number"); final GetByPhoneNumberRequest request = GetByPhoneNumberRequest.newBuilder() .setPhoneNumber(Util.S_INVALID_PHONENUMBER).build(); + given(accountProvider.searchAccountByLoginOptionWithoutSelector(AuthenticationType.PHONE, request.getPhoneNumber())).willThrow(ex); + final SearchResponse reply = searchServiceBlockingStub.searchByPhoneNumber(request); assertNotNull("Reply should not be null", reply); -- GitLab From 9f21e52752ef7f3f2260b7c59bdf348476102f60 Mon Sep 17 00:00:00 2001 From: Stoyan Tzenkov Date: Tue, 13 Aug 2019 16:24:16 +0300 Subject: [PATCH 5/5] NY-7952: Got rid of some commented lines. Signed-off-by: Stoyan Tzenkov --- src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java b/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java index 16ed9c7..b9f2ebc 100644 --- a/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java +++ b/src/main/java/biz/nynja/account/kafka/KafkaProducerConfig.java @@ -41,8 +41,6 @@ public class KafkaProducerConfig { configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_HOST + ":" + KAFKA_PORT); configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); - // Check config below if kakfa is down - //configProps.put(ProducerConfig.RETRIES_CONFIG, 10); return new DefaultKafkaProducerFactory(configProps); -- GitLab