Skip to content

Commit

Permalink
Merge branch 'main' into feat/OTP-generation-mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Elwizzy12 authored Jan 8, 2025
2 parents 64bb2e8 + c761c04 commit 5f68b7c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
9 changes: 9 additions & 0 deletions prs/prs-rest-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
springdoc.api-docs.enabled=true
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html

twilio.account.sid=${TWILIO_ACCOUNT_SID}
twilio.auth.token=${TWILIO_AUTH_TOKEN}
twilio.phone.number=${TWILIO_PHONE_NUMBER}
otp.salt=${OTP_SALT}




7 changes: 7 additions & 0 deletions prs/prs-service-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--- twillor -->
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>10.6.4</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.adorsys.webank.exceptions;

public class FailedToSendOTPException extends RuntimeException {
public FailedToSendOTPException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,43 @@
import com.adorsys.webank.service.OtpServiceApi;
import org.springframework.stereotype.Service;
import com.adorsys.webank.exceptions.HashComputationException;
import com.adorsys.webank.exceptions.FailedToSendOTPException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import jakarta.annotation.PostConstruct;
import com.twilio.type.PhoneNumber;


@Service
public class OtpServiceImpl implements OtpServiceApi {
private static final Logger logger = LoggerFactory.getLogger(OtpServiceImpl.class);

// Twilio credentials
@Value("${twilio.account.sid}")
private String accountSid;

@Value("${twilio.auth.token}")
private String authToken;

@Value("${twilio.phone.number}")
private String fromPhoneNumber;

@Value("${otp.salt}")
private String salt;

@PostConstruct
public void initTwilio() {
Twilio.init(accountSid, authToken); // Initialize Twilio once
}


@Override
public String generateOtp() {
Expand All @@ -20,16 +49,33 @@ public String generateOtp() {
}

@Override
public String sendOtp(String phoneNumber, String publicKey) {
String otp = generateOtp();
public String sendOtp(String phoneNumber, String publicKey) {
if (phoneNumber == null || !phoneNumber.matches("\\+?[1-9]\\d{1,14}")) {
logger.error("Invalid phone number format.");
throw new IllegalArgumentException("Invalid phone number format");
}

try {

String salt = "5";
String otp = generateOtp();
String otpHash = computeHash(otp, phoneNumber,publicKey, salt);

String otpHash = computeHash(otp, phoneNumber,publicKey, salt);
// Send OTP via Twilio
Message message = Message.creator(
new PhoneNumber(phoneNumber),
new PhoneNumber(fromPhoneNumber),
"Your OTP is: " + otp
).create();

return otpHash + ":" + otp;
}
logger.info("OTP sent successfully to {}. SID: {}", phoneNumber, message.getSid());

return otpHash;

} catch (Exception e) {
logger.error("Failed to send OTP: {}", e.getMessage());
throw new FailedToSendOTPException("Failed to send OTP");
}
}

@Override
public boolean validateOtp(String phoneNumber, String otp) {
Expand All @@ -51,4 +97,4 @@ public String computeHash(String otp, String phoneNumber, String publicKey, Stri
throw new HashComputationException("Error computing hash");
}
}
}
}

0 comments on commit 5f68b7c

Please sign in to comment.