Skip to content

Commit

Permalink
ES-427 Add ASYNC: Invoke sms notification endpoint (mosip#32)
Browse files Browse the repository at this point in the history
* add send notification endpoint

* send sms otp with async

* ASYNC send OTP notification by SMS

* add endline in some file

* change sms content

* change to unicode

* encode UTF-8

* rollback to old message content

* change message content to utf-8

* change proprieties name

* recover test.properties file

---------

Co-authored-by: Mengleang <[email protected]>
Signed-off-by: Sreang Rathanak <[email protected]>
  • Loading branch information
2 people authored and Sreang Rathanak committed Jan 15, 2024
1 parent edd8e19 commit cfb8495
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableCaching
@EnableAsync
@SpringBootApplication(scanBasePackages = "io.mosip.signup.*," +
"io.mosip.esignet.core.config.RedisCacheConfig," +
"io.mosip.esignet.core.config.SimpleCacheConfig,"+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class RegistrationController {
RegistrationService registrationService;

@PostMapping("/generate-challenge")
public ResponseWrapper<GenerateChallengeResponse> generateChallenge (
@Valid @RequestBody RequestWrapper<GenerateChallengeRequest> requestWrapper, @CookieValue(name = SignUpConstants.TRANSACTION_ID, defaultValue = "") String transactionId) throws SignUpException {
public ResponseWrapper<GenerateChallengeResponse> generateChallenge (@Valid @RequestBody RequestWrapper<GenerateChallengeRequest> requestWrapper,
@CookieValue(name = SignUpConstants.TRANSACTION_ID, defaultValue = "") String transactionId)
throws SignUpException {
ResponseWrapper<GenerateChallengeResponse> responseWrapper = new ResponseWrapper<>();
responseWrapper.setResponse(registrationService.generateChallenge(requestWrapper.getRequest(), transactionId));
responseWrapper.setResponseTime(IdentityProviderUtil.getUTCDateTime());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.mosip.signup.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class NotificationRequest {
private String number;
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.mosip.signup.dto;

import lombok.Data;

@Data
public class NotificationResponse {
private String status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.util.concurrent.CompletableFuture;

import static io.mosip.signup.util.SignUpConstants.CONSENT_DISAGREE;

@Slf4j
Expand Down Expand Up @@ -66,6 +69,9 @@ public class RegistrationService {
@Value("${mosip.signup.get-uin.endpoint}")
private String getUinEndpoint;

@Value("${mosip.signup.send-notification.endpoint}")
private String sendNotificationEndpoint;

@Value("${mosip.signup.cookie.max-age}")
private int cookieMaxAge;

Expand All @@ -75,6 +81,18 @@ public class RegistrationService {
@Value("${mosip.signup.challenge.resend-delay}")
private long resendDelay;

@Value("${mosip.signup.otp-registration.sms.khm}")
private String otpRegistrationKhmMessage;

@Value("${mosip.signup.otp-registration.sms.eng}")
private String otpRegistrationEngMessage;

@Value("${mosip.signup.successfully.registration.sms.khm}")
private String registrationSuccessKhmMessage;

@Value("${mosip.signup.successfully.registration.sms.eng}")
private String registrationSuccessEngMessage;

/**
* Generate and regenerate challenge based on the "regenerate" flag in the request.
* if regenerate is false - always creates a new transaction and set-cookie header is sent in the response.
Expand Down Expand Up @@ -110,6 +128,11 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene
transaction.increaseAttempt();
transaction.setLocale(generateChallengeRequest.getLocale());
cacheUtilService.setChallengeGeneratedTransaction(transactionId, transaction);

sendNotificationAsync(generateChallengeRequest.getIdentifier(), transaction.getLocale(), challenge)
.thenAccept(notificationResponseRestResponseWrapper -> {
log.debug("Send SMS response {}", notificationResponseRestResponseWrapper.toString());
});
return new GenerateChallengeResponse(ActionStatus.SUCCESS);
}

Expand Down Expand Up @@ -265,6 +288,25 @@ private String getUniqueIdentifier(String transactionId) throws SignUpException
restResponseWrapper.getErrors().get(0).getErrorCode() : ErrorConstants.GET_UIN_FAILED);
}

@Async
private CompletableFuture<RestResponseWrapper<NotificationResponse>> sendNotificationAsync
(String number, String locale, String challenge) {

NotificationRequest notificationRequest = new NotificationRequest(number.substring(4),
locale == null || locale.equals("khm") ? otpRegistrationKhmMessage : otpRegistrationEngMessage );

notificationRequest.setMessage(notificationRequest.getMessage().replace("XXXXXX", challenge));

RestRequestWrapper<NotificationRequest> restRequestWrapper = new RestRequestWrapper<>();
restRequestWrapper.setRequest(notificationRequest);

return CompletableFuture.supplyAsync(() -> selfTokenRestTemplate
.exchange(sendNotificationEndpoint,
HttpMethod.POST,
new HttpEntity<>(restRequestWrapper),
new ParameterizedTypeReference<RestResponseWrapper<NotificationResponse>>(){}).getBody());
}

private void validateTransaction(RegistrationTransaction transaction, String identifier) {
if(transaction == null) {
log.error("generate-challenge failed: validate transaction null");
Expand Down
12 changes: 9 additions & 3 deletions signup-service/src/main/resources/application-default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mosip.signup.status.request.delay=20
mosip.signup.status.request.limit=10
mosip.signup.fullname.pattern=^[\p{Khmer}\s]{1,30}$
mosip.signup.challenge.timeout=60
mosip.signup.password.max-length=20

## ------------------------------------- Cache configuration -----------------------------------------------------------

Expand Down Expand Up @@ -46,7 +47,7 @@ mosip.kernel.auth.appid-realm-map={admin:'mosip',crereq:'mosip',creser:'mosip',i
mosip.kernel.auth.appids.realm.map={admin:'mosip',crereq:'mosip',creser:'mosip',idrepo:'mosip','regproc':'mosip', signup:'mosip'}

mosip.iam.adapter.clientid=mosip-signup-client
mosip.iam.adapter.clientsecret=QdDb3zAdFDgBvto7aNasLLJjxZ1xSSvu
mosip.iam.adapter.clientsecret=LBIZwf0IGrFlkuXi
mosip.iam.adapter.appid=signup
mosip.iam.adapter.issuerURL=${keycloak.external.url}/auth/realms/mosip
mosip.authmanager.client-token-endpoint=${mosip.kernel.authmanager.url}/v1/authmanager/authenticate/clientidsecretkey
Expand All @@ -63,6 +64,7 @@ mosip.signup.generate-challenge.endpoint=${mosip.kernel.authmanager.url}/v1/otpm
mosip.signup.add-identity.endpoint=https://api-internal.camdgc-dev.mosip.net/idrepository/v1/identity/
mosip.signup.generate-hash.endpoint=https://api-internal.camdgc-dev.mosip.net/v1/keymanager/generateArgon2Hash
mosip.signup.get-uin.endpoint=https://api-internal.camdgc-dev.mosip.net/v1/idgenerator/uin
mosip.signup.send-notification.endpoint=https://api-internal.camdgc-dev.mosip.net/v1/notifier/sms/send
mosip.signup.add-identity.request.id=mosip.id.create
mosip.signup.add-identity.request.version=v1

Expand All @@ -89,6 +91,10 @@ mosip.signup.ui.config.key-values={\
'status.request.delay': '${mosip.signup.status.request.delay}', \
'status.request.limit': '${mosip.signup.status.request.limit}' \
}

## ----------------------------- SMS-message -----------------------------------------------------------------------------
mosip.signup.otp-registration.sms.khm=ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។
mosip.signup.otp-registration.sms.eng=Use XXXXXX to verify your KhID account.
mosip.signup.successfully.registration.sms.khm=ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។
mosip.signup.successfully.registration.sms.eng=Use XXXXXX to verify your KhID account.
#------------------------------------------ Others ---------------------------------------------------------------------
logging.level.io.mosip.signup=DEBUG
logging.level.io.mosip.signup=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
Expand All @@ -41,7 +42,7 @@
import java.time.LocalDateTime;

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-test.properties")
@ActiveProfiles(value = {"test"})
public class RegistrationServiceTest {

@InjectMocks
Expand Down Expand Up @@ -75,6 +76,12 @@ public void setUp(){
registrationService, "resendAttempts", 3);
ReflectionTestUtils.setField(
registrationService, "resendDelay", 30);

ReflectionTestUtils.setField(
registrationService, "otpRegistrationKhmMessage", "ប្រើ XXXXXX ដើម្បីផ្ទៀងផ្ទាត់គណនី KhID របស់អ្នក។");

ReflectionTestUtils.setField(
registrationService, "otpRegistrationEngMessage", "Use XXXXXX to verify your KhID account.");
}

@Test
Expand Down

0 comments on commit cfb8495

Please sign in to comment.