Skip to content

Commit

Permalink
Issue #10 feat: Capturing configuration for SMS providers
Browse files Browse the repository at this point in the history
  • Loading branch information
shriharshs committed Nov 3, 2017
1 parent d5d3a6e commit 582d9a6
Show file tree
Hide file tree
Showing 15 changed files with 441 additions and 12 deletions.
4 changes: 4 additions & 0 deletions keycloak/sms-provider/Msg91Creds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"token": "AKIAI4XIRSGGFIW5VOKQ",
"secret": "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"
}
4 changes: 4 additions & 0 deletions keycloak/sms-provider/awsSnsCreds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"token": "AKIAI4XIRSGGFIW5VOKQ",
"secret": "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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public class KeycloakSmsAuthenticatorConstants {
// User credentials (used to persist the sent sms code + expiration time cluster wide)
public static final String USR_CRED_MDL_SMS_CODE = "sms-auth.code";
public static final String USR_CRED_MDL_SMS_EXP_TIME = "sms-auth.exp-time";

public static final String SMS_PROVIDER_CONFIGURATIONS_PATH = "sms-provider/awsSnsCreds.json";


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.sunbird.keycloak;

import com.amazonaws.services.sns.model.PublishResult;
import com.amazonaws.util.StringUtils;
import org.jboss.logging.Logger;
import org.keycloak.models.AuthenticatorConfigModel;
import org.keycloak.models.UserModel;
import org.sunbird.aws.snsclient.SnsNotificationService;
import org.sunbird.sms.MessageProviderFactory;
import org.sunbird.sms.provider.ISmsProvider;
import org.sunbird.utils.JsonUtil;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
Expand Down Expand Up @@ -85,19 +89,25 @@ public static String setDefaultCountryCodeIfZero(String mobileNumber) {

static boolean sendSmsCode(String mobileNumber, String code, AuthenticatorConfigModel config) {
// Send an SMS
KeycloakSmsAuthenticatorUtil.logger.debug("Sending " + code + " to mobileNumber " + mobileNumber);

String smsUsr = getConfigString(config, KeycloakSmsAuthenticatorConstants.CONF_PRP_SMS_CLIENTTOKEN);
String smsPwd = getConfigString(config, KeycloakSmsAuthenticatorConstants.CONF_PRP_SMS_CLIENTSECRET);

String smsText = createMessage(code, mobileNumber, config);
try {
PublishResult send_result = new SnsNotificationService().send(setDefaultCountryCodeIfZero(mobileNumber), smsText, smsUsr, smsPwd);
return true;
} catch (Exception e) {
//Just like pokemon
return false;
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : smsText - " + smsText);

String filePath = new File(KeycloakSmsAuthenticatorConstants.SMS_PROVIDER_CONFIGURATIONS_PATH).getAbsolutePath();
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : filePath - " + filePath);

if (!StringUtils.isNullOrEmpty(filePath)) {
Map<String, String> configurations = JsonUtil.readFromJson(filePath);
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : configurations - " + configurations);

ISmsProvider amazonSmsProvider = MessageProviderFactory.getSnsClient(configurations);

if (amazonSmsProvider != null) {
return amazonSmsProvider.send(setDefaultCountryCodeIfZero(mobileNumber), smsText);
}
}

return false;
}

static String getSmsCode(long nrOfDigits) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sunbird.sms;

import org.sunbird.sms.amazonsns.AmazonSnsFactory;
import org.sunbird.sms.msg91.Msg91SmsProviderFactory;
import org.sunbird.sms.provider.ISmsProvider;

import java.util.Map;

public class MessageProviderFactory {

private static Msg91SmsProviderFactory msg91SmsProviderFactory;
private static AmazonSnsFactory amazonSnsFactory = null;


public static ISmsProvider getMsg91SmsProvider(Map<String, String> configurations) {
if (msg91SmsProviderFactory == null) {
msg91SmsProviderFactory = new Msg91SmsProviderFactory();
}

return msg91SmsProviderFactory.create(configurations);
}


public static ISmsProvider getSnsClient(Map<String, String> configurations) {
if (amazonSnsFactory == null) {
amazonSnsFactory = new AmazonSnsFactory();
}
return amazonSnsFactory.create(configurations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sunbird.sms;

import java.util.Map;

/**
* Created by joris on 18/11/2016.
*/
public class SMSAuthenticatorUtil {

public static String getConfigString(Map<String, String> config, String configName) {
return getConfigString(config, configName, null);
}

public static String getConfigString(Map<String, String> config, String configName, String defaultValue) {

String value = defaultValue;

if (config.containsKey(configName)) {
// Get value
value = config.get(configName);
}

return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sunbird.sms;

public interface SmsConfigurationType {
String CONF_SMS_TOKEN = "token";
String CONF_SMS_SECRET = "secret";
String CONF_SMS_URL = "sms_url";
String CONF_SMS_METHOD_TYPE = "sms_method_type";
String CONF_SMS_USERNAME = "sms_username";
String CONF_SMS_PASSWORD = "sms_password";
String CONF_SMS_AUTHTYPE = "sms_authtype";
String CONF_SMS_CONTENT_TYPE = "sms_content_type";
String CONF_SMS_PROXY_URL = "sms_proxy_url";
String CONF_SMS_PROXY_USERNAME = "sms_proxy_username";
String CONF_SMS_PROXY_PASSWORD = "sms_proxy_password";
String CONF_AUTH_METHOD_BASIC = "basic_authentication";
String CONF_AUTH_METHOD_INMESSAGE = "in_message_authentication";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.sunbird.sms.amazonsns;

import org.sunbird.sms.provider.ISmsProvider;
import org.sunbird.sms.provider.ISmsProviderFactory;

import java.util.Map;

public class AmazonSnsFactory implements ISmsProviderFactory {
private static AmazonSnsProvider amazonSnsProvider = null;

@Override
public ISmsProvider create(Map<String, String> configurations) {
if (amazonSnsProvider == null) {
amazonSnsProvider = new AmazonSnsProvider();
amazonSnsProvider.configure(configurations);
}

return amazonSnsProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.sunbird.sms.amazonsns;

import com.amazonaws.services.sns.model.MessageAttributeValue;
import com.amazonaws.services.sns.model.PublishRequest;
import org.jboss.logging.Logger;
import org.sunbird.aws.snsclient.SnsClientFactory;
import org.sunbird.sms.SMSAuthenticatorUtil;
import org.sunbird.sms.SmsConfigurationType;
import org.sunbird.sms.provider.ISmsProvider;

import java.util.HashMap;
import java.util.Map;

public class AmazonSnsProvider implements ISmsProvider {

private static Logger logger = Logger.getLogger(AmazonSnsProvider.class);

private Map<String, String> configurations;

@Override
public boolean send(String phoneNumber, String smsText) {
logger.debug("AmazonSnsProvider@send : phoneNumber - " + phoneNumber + " & Sms text - " + smsText);

Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
.withStringValue("HomeOffice")
.withDataType("String"));

String clientToken = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_TOKEN);
String clientSecret = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_SECRET);

logger.debug("AmazonSnsProvider@send : clientToken - " + clientToken + " & clientSecret - " + clientSecret);


try {
SnsClientFactory.getSnsClient(clientToken, clientSecret).publish(new PublishRequest()
.withMessage(smsText)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));

return true;
} catch (Exception e) {
logger.debug("AmazonSnsProvider@Send : Exception Caught -" + e.getMessage());
return false;
}
}

@Override
public void configure(Map<String, String> configurations) {
this.configurations = configurations;
}
}
Loading

0 comments on commit 582d9a6

Please sign in to comment.