Skip to content

Commit

Permalink
Fixes sending SMS messages to unknown phonenumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mapidentity committed Sep 8, 2023
1 parent 4203119 commit 2644f3c
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cc.coopersoft.keycloak.phone.providers.exception.MessageSendException;
import cc.coopersoft.keycloak.phone.providers.representations.TokenCodeRepresentation;
import cc.coopersoft.keycloak.phone.providers.spi.MessageSenderService;
import cc.coopersoft.keycloak.phone.Utils;
import org.jboss.logging.Logger;
import org.keycloak.Config.Scope;
import org.keycloak.models.KeycloakSession;
Expand Down Expand Up @@ -131,17 +132,21 @@ public int sendTokenCode(String phoneNumber,String sourceAddr,TokenCodeType type

TokenCodeRepresentation token = TokenCodeRepresentation.forPhoneNumber(phoneNumber);

try {
session.getProvider(MessageSenderService.class, service).sendSmsMessage(type,phoneNumber,token.getCode(),tokenExpiresIn,kind);
getTokenCodeService().persistCode(token, type, tokenExpiresIn);
if (TokenCodeType.REGISTRATION.equals(type) || Utils.findUserByPhone(session, session.getContext().getRealm(), phoneNumber).isPresent()) {
try {
session.getProvider(MessageSenderService.class, service).sendSmsMessage(type,phoneNumber,token.getCode(),tokenExpiresIn,kind);
getTokenCodeService().persistCode(token, type, tokenExpiresIn);

logger.info(String.format("Sent %s code to %s over %s",type.label, phoneNumber, service));
logger.info(String.format("Sent %s code to %s over %s",type.label, phoneNumber, service));

} catch (MessageSendException e) {
} catch (MessageSendException e) {

logger.error(String.format("Message sending to %s failed with %s: %s",
phoneNumber, e.getErrorCode(), e.getErrorMessage()));
throw new ServiceUnavailableException("Internal server error");
logger.error(String.format("Message sending to %s failed with %s: %s",
phoneNumber, e.getErrorCode(), e.getErrorMessage()));
throw new ServiceUnavailableException("Internal server error");
}
} else {
logger.info(String.format("Unknown phone number %s, not sending %s code to %s", phoneNumber, type.label, sourceAddr));
}

return tokenExpiresIn;
Expand Down

0 comments on commit 2644f3c

Please sign in to comment.