Skip to content

Commit

Permalink
Merge pull request #107 from Awambeng/feat/12-implement-user-registra…
Browse files Browse the repository at this point in the history
…tion-logic

Implement the user registration logic
  • Loading branch information
NkwaTambe authored Apr 12, 2024
2 parents 8f24cc7 + a49a63d commit 98b9979
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<UserRegistration, Integer> {

}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CompareHashPasswordTest {

@Test
void compareHashPassword(){

String rawData = "mypin";
String hashedData = dataSecurityService.hashData(rawData);
assertTrue(dataSecurityService.compareHashedData(rawData, hashedData));
Expand Down

0 comments on commit 98b9979

Please sign in to comment.