diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java new file mode 100644 index 00000000..f57155ca --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRegistrationRepository.java @@ -0,0 +1,8 @@ +package com.adorsys.gis.powerpay.powerpaybackend.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; + +public interface UserRegistrationRepository extends JpaRepository { + +} diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java new file mode 100644 index 00000000..9fce7dba --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationService.java @@ -0,0 +1,13 @@ +package com.adorsys.gis.powerpay.powerpaybackend.services; + +import com.adorsys.gis.powerpay.powerpaybackend.domain.User; +import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; +import jakarta.validation.constraints.NotNull; + +public interface UserRegistrationService { + + User registerUser(@NotNull Integer registrationId, String pin, String otp); + + UserRegistration createProcedure(String phoneNumber, String userName); + +} diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java new file mode 100644 index 00000000..d006d4af --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/UserRegistrationServiceImpl.java @@ -0,0 +1,69 @@ +package com.adorsys.gis.powerpay.powerpaybackend.services; + +import java.security.SecureRandom; + +import org.springframework.stereotype.Service; + +import com.adorsys.gis.powerpay.powerpaybackend.domain.User; +import com.adorsys.gis.powerpay.powerpaybackend.domain.UserRegistration; +import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRegistrationRepository; +import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRepository; +import com.adorsys.gis.powerpay.powerpaybackend.utils.DataSecurityService; +import com.adorsys.gis.powerpay.powerpaybackend.utils.UserRegistrationException; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; + +@Service +@AllArgsConstructor +public class UserRegistrationServiceImpl implements UserRegistrationService { + + private final UserRegistrationRepository userRegistrationRepository; + private final UserRepository userRepository; + private final DataSecurityService dataSecurityService; + + private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + private static final int OTP_LENGTH = 8; + + @Override + public User registerUser(@NotNull Integer registrationId, String pin, String otp) { + UserRegistration userRegistration = userRegistrationRepository.findById(registrationId) + .orElseThrow(() -> new UserRegistrationException("User registration not found")); + + User newUser = new User(); + newUser.setUserName(userRegistration.getUserName()); + newUser.setPin(dataSecurityService.hashData(pin)); + + try { + userRepository.save(newUser); + } catch (Exception e) { + throw new UserRegistrationException("User registration failed: " + e.getMessage()); + } + + return newUser; + } + + @Override + public UserRegistration createProcedure(String phoneNumber, String userName) { + UserRegistration userRegistration = new UserRegistration(); + + // generating an otp + StringBuilder otp = new StringBuilder(); + SecureRandom secureRandom = new SecureRandom(); + + for (int i = 0; i < OTP_LENGTH; i++) { + int index = secureRandom.nextInt(CHARACTERS.length()); + char character = CHARACTERS.charAt(index); + otp.append(character); + } + + var otpValue = otp.toString(); + + userRegistration.setPhoneNumber(phoneNumber); + userRegistration.setUserName(userName); + userRegistration.setOpt(dataSecurityService.hashData(otpValue)); + userRegistrationRepository.save(userRegistration); + + return userRegistration; + } +} diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/DataSecurityService.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/DataSecurityService.java index 0aef2fdb..0e7e5e8a 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/DataSecurityService.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/DataSecurityService.java @@ -15,6 +15,7 @@ public String hashData(String data) { //Method to compare a given String with hashed value public boolean compareHashedData(String data, String hashedData) { + return passwordEncoder.matches(data, hashedData); } } diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/UserRegistrationException.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/UserRegistrationException.java new file mode 100644 index 00000000..8d51b552 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/utils/UserRegistrationException.java @@ -0,0 +1,11 @@ +package com.adorsys.gis.powerpay.powerpaybackend.utils; + +public class UserRegistrationException extends RuntimeException { + public UserRegistrationException(String message, Exception exception) { + super(message, exception); + } + + public UserRegistrationException(String userRegistrationNotFound) { + super(userRegistrationNotFound); + } +} diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/utils_test/CompareHashPasswordTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/utils_test/CompareHashPasswordTest.java index a6e70cfe..d0c66e0c 100644 --- a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/utils_test/CompareHashPasswordTest.java +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/utils_test/CompareHashPasswordTest.java @@ -20,6 +20,7 @@ public class CompareHashPasswordTest { @Test void compareHashPassword(){ + String rawData = "mypin"; String hashedData = dataSecurityService.hashData(rawData); assertTrue(dataSecurityService.compareHashedData(rawData, hashedData));