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 582d9a6 commit 502264b
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 22 deletions.
8 changes: 6 additions & 2 deletions keycloak/sms-provider/Msg91Creds.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"token": "AKIAI4XIRSGGFIW5VOKQ",
"secret": "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"
"auth_key": "181585AvzynW0NSmU59f84bd6",
"sender": "TestSunbird",
"country": "91",
"route": "4",
"sms_method_type": "GET",
"sms_url": "http://api.msg91.com/api/sendhttp.php?"
}
156 changes: 156 additions & 0 deletions keycloak/sms-provider/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sunbird</groupId>
<artifactId>keycloak-email-phone-autthenticator</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>3.2.0.Final</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>keycloak-common</artifactId>
<groupId>org.keycloak</groupId>
</exclusion>
<exclusion>
<artifactId>bcprov-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>bcpkix-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<version>3.2.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi-private</artifactId>
<version>3.2.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>3.2.0.Final</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>javax.mail-api</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
<exclusion>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
<groupId>org.jboss.spec.javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>twitter4j-core</artifactId>
<groupId>org.twitter4j</groupId>
</exclusion>
<exclusion>
<artifactId>resteasy-jaxrs</artifactId>
<groupId>org.jboss.resteasy</groupId>
</exclusion>
<exclusion>
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
</exclusion>
<exclusion>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<groupId>org.jboss.spec.javax.transaction</groupId>
</exclusion>
<exclusion>
<artifactId>resteasy-multipart-provider</artifactId>
<groupId>org.jboss.resteasy</groupId>
</exclusion>
<exclusion>
<artifactId>javase</artifactId>
<groupId>com.google.zxing</groupId>
</exclusion>
<exclusion>
<artifactId>bcprov-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>bcpkix-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>commons-codec</artifactId>
<groupId>commons-codec</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

3 changes: 3 additions & 0 deletions keycloak/sms-provider/keycloak-email-phone-autthenticator.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
Expand Down Expand Up @@ -56,5 +57,7 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.6.7" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.8.1" level="project" />
<orderEntry type="library" name="Maven: com.amazonaws:jmespath-java:1.11.173" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class KeycloakSmsAuthenticatorConstants {
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";


public static final String AMAZON_SNS_PROVIDER_CONFIGURATIONS_PATH = "sms-provider/awsSnsCreds.json";
public static final String MSG91_SMS_PROVIDER_CONFIGURATIONS_PATH = "sms-provider/Msg91Creds.json";
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ public static String setDefaultCountryCodeIfZero(String mobileNumber) {
}

static boolean sendSmsCode(String mobileNumber, String code, AuthenticatorConfigModel config) {
// Send an SMS

String smsText = createMessage(code, mobileNumber, config);
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : smsText - " + smsText);

String filePath = new File(KeycloakSmsAuthenticatorConstants.SMS_PROVIDER_CONFIGURATIONS_PATH).getAbsolutePath();
Boolean amazonSmsProviderStatus = msgViaAmazonSns(mobileNumber, smsText);
if (amazonSmsProviderStatus != null) return amazonSmsProviderStatus;

Boolean msg91SmsProviderStatus = sendViaMsg91(mobileNumber, code);
if (msg91SmsProviderStatus != null) return msg91SmsProviderStatus;

return false;
}

private static Boolean msgViaAmazonSns(String mobileNumber, String smsText) {
String filePath = new File(KeycloakSmsAuthenticatorConstants.AMAZON_SNS_PROVIDER_CONFIGURATIONS_PATH).getAbsolutePath();
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : filePath - " + filePath);

if (!StringUtils.isNullOrEmpty(filePath)) {
Expand All @@ -106,8 +114,24 @@ static boolean sendSmsCode(String mobileNumber, String code, AuthenticatorConfig
return amazonSmsProvider.send(setDefaultCountryCodeIfZero(mobileNumber), smsText);
}
}
return null;
}

return false;
private static Boolean sendViaMsg91(String mobileNumber, String code) {
String filePath = new File(KeycloakSmsAuthenticatorConstants.MSG91_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 msg91SmsProvider = MessageProviderFactory.getMsg91SmsProvider(configurations);

if (msg91SmsProvider != null) {
return msg91SmsProvider.send(setDefaultCountryCodeIfZero(mobileNumber), code);
}
}
return null;
}

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


import org.sunbird.sms.amazonsns.AmazonSnsFactory;
import org.sunbird.sms.msg91.Msg91SmsProviderFactory;
import org.sunbird.sms.provider.ISmsProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public interface SmsConfigurationType {
String CONF_SMS_PROXY_PASSWORD = "sms_proxy_password";
String CONF_AUTH_METHOD_BASIC = "basic_authentication";
String CONF_AUTH_METHOD_INMESSAGE = "in_message_authentication";
String CONF_AUTH_KEY = "auth_key";
String CONF_SMS_SENDER = "sender";
String CONF_SMS_COUNTRY = "country";
String CONF_SMS_ROUTE = "route";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sunbird.sms.amazonsns;


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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sunbird.sms.msg91;

import com.amazonaws.util.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
Expand Down Expand Up @@ -39,9 +40,91 @@ public void configure(Map<String, String> configurations) {

@Override
public boolean send(String phoneNumber, String smsText) {
return sendSmsCode(phoneNumber, smsText);
return sendSms(phoneNumber, smsText);
}

private boolean sendSms(String mobileNumber, String smsText) {
// Send an SMS
logger.debug("Msg91SmsProvider@Sending " + smsText + " to mobileNumber " + mobileNumber);

String gateWayUrl = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_URL);
String authKey = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_AUTH_KEY);
String sender = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_SENDER);
String country = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_COUNTRY);
String smsMethodType = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_METHOD_TYPE);
String smsRoute = SMSAuthenticatorUtil.getConfigString(configurations, SmsConfigurationType.CONF_SMS_ROUTE);

logger.debug("Msg91SmsProvider@SMS Provider parameters \n" +
"Gateway - " + gateWayUrl + "\n" +
"authKey - " + authKey + "\n" +
"sender - " + sender + "\n" +
"country - " + country + "\n" +
"smsMethodType - " + smsMethodType + "\n" +
"smsRoute - " + smsRoute + "\n"
);


CloseableHttpClient httpClient = null;
try {
URL smsURL = (gateWayUrl != null && gateWayUrl.length() > 0) ? new URL(gateWayUrl) : null;

if (smsURL == null) {
logger.error("Msg91SmsProvider@ SMS gateway URL is not configured.");
return false;
}

httpClient = HttpClients.createDefault();

String path = null;

if (!StringUtils.isNullOrEmpty(gateWayUrl) && !StringUtils.isNullOrEmpty(sender) && !StringUtils.isNullOrEmpty(smsRoute)
&& !StringUtils.isNullOrEmpty(mobileNumber) && !StringUtils.isNullOrEmpty(authKey) && !StringUtils.isNullOrEmpty(country)
&& !StringUtils.isNullOrEmpty(smsText)) {
path = getCompletePath(gateWayUrl, sender, smsRoute, mobileNumber, authKey, country, smsText);

logger.debug("Msg91SmsProvider -Executing request - " + path);

HttpGet httpGet = new HttpGet(path);

CloseableHttpResponse response = httpClient.execute(httpGet);
StatusLine sl = response.getStatusLine();
response.close();
if (sl.getStatusCode() != 200) {
logger.error("SMS code for " + mobileNumber + " could not be sent: " + sl.getStatusCode() + " - " + sl.getReasonPhrase());
}
return sl.getStatusCode() == 200;

} else {
logger.debug("Msg91SmsProvider - Some mandatory parameters are empty!");
return false;
}
} catch (IOException e) {
logger.error(e);
return false;
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException ignore) {
// Ignore ...
}
}
}

}

private String getCompletePath(String gateWayUrl, String sender, String smsRoute, String mobileNumber, String authKey, String country, String smsText) {
String completeUrl = gateWayUrl
+ "sender=" + sender
+ "&route=" + smsRoute
+ "&mobiles=" + mobileNumber
+ "&authkey=" + authKey
+ "&country=" + country
+ "&message=" + smsText;
return completeUrl;
}


private boolean sendSmsCode(String mobileNumber, String smsText) {
// Send an SMS
logger.debug("Sending " + smsText + " to mobileNumber " + mobileNumber);
Expand Down Expand Up @@ -166,7 +249,7 @@ private CredentialsProvider getCredentialsProvider(String smsUsr, String smsPwd,

private String getPath(String mobileNumber, URL smsURL, String smsText) throws UnsupportedEncodingException {
String path = smsURL.getPath();
if(smsURL.getQuery() != null && smsURL.getQuery().length() > 0) {
if (smsURL.getQuery() != null && smsURL.getQuery().length() > 0) {
path += smsURL.getQuery();
}
path = path.replaceFirst("\\{message\\}", URLEncoder.encode(smsText, "UTF-8"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface ISmsProvider extends ISmsProviderConfigurations {

boolean send(String phoneNumber,String smsText);
boolean send(String phoneNumber, String smsText);
}
Loading

0 comments on commit 502264b

Please sign in to comment.