Skip to content

Commit cb4d646

Browse files
authored
Merge pull request #11 from project-sunbird/sms_provider_configuraton
Sms provider configuraton
2 parents d5d3a6e + 30c5796 commit cb4d646

16 files changed

+744
-13
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.sunbird</groupId>
5+
<artifactId>keycloak-email-phone-autthenticator</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-shade-plugin</artifactId>
11+
<version>3.0.0</version>
12+
<executions>
13+
<execution>
14+
<phase>package</phase>
15+
<goals>
16+
<goal>shade</goal>
17+
</goals>
18+
<configuration>
19+
<artifactSet>
20+
<excludes>
21+
<exclude>classworlds:classworlds</exclude>
22+
<exclude>junit:junit</exclude>
23+
<exclude>jmock:*</exclude>
24+
<exclude>*:xml-apis</exclude>
25+
<exclude>org.apache.maven:lib:tests</exclude>
26+
</excludes>
27+
</artifactSet>
28+
</configuration>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.keycloak</groupId>
37+
<artifactId>keycloak-core</artifactId>
38+
<version>3.2.0.Final</version>
39+
<scope>provided</scope>
40+
<exclusions>
41+
<exclusion>
42+
<artifactId>keycloak-common</artifactId>
43+
<groupId>org.keycloak</groupId>
44+
</exclusion>
45+
<exclusion>
46+
<artifactId>bcprov-jdk15on</artifactId>
47+
<groupId>org.bouncycastle</groupId>
48+
</exclusion>
49+
<exclusion>
50+
<artifactId>bcpkix-jdk15on</artifactId>
51+
<groupId>org.bouncycastle</groupId>
52+
</exclusion>
53+
</exclusions>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.keycloak</groupId>
57+
<artifactId>keycloak-server-spi</artifactId>
58+
<version>3.2.0.Final</version>
59+
<scope>provided</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.keycloak</groupId>
63+
<artifactId>keycloak-server-spi-private</artifactId>
64+
<version>3.2.0.Final</version>
65+
<scope>provided</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.jboss.logging</groupId>
69+
<artifactId>jboss-logging</artifactId>
70+
<version>3.3.1.Final</version>
71+
<scope>provided</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.keycloak</groupId>
75+
<artifactId>keycloak-services</artifactId>
76+
<version>3.2.0.Final</version>
77+
<scope>provided</scope>
78+
<exclusions>
79+
<exclusion>
80+
<artifactId>javax.mail-api</artifactId>
81+
<groupId>javax.mail</groupId>
82+
</exclusion>
83+
<exclusion>
84+
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
85+
<groupId>org.jboss.spec.javax.servlet</groupId>
86+
</exclusion>
87+
<exclusion>
88+
<artifactId>twitter4j-core</artifactId>
89+
<groupId>org.twitter4j</groupId>
90+
</exclusion>
91+
<exclusion>
92+
<artifactId>resteasy-jaxrs</artifactId>
93+
<groupId>org.jboss.resteasy</groupId>
94+
</exclusion>
95+
<exclusion>
96+
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
97+
<groupId>org.jboss.spec.javax.ws.rs</groupId>
98+
</exclusion>
99+
<exclusion>
100+
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
101+
<groupId>org.jboss.spec.javax.transaction</groupId>
102+
</exclusion>
103+
<exclusion>
104+
<artifactId>resteasy-multipart-provider</artifactId>
105+
<groupId>org.jboss.resteasy</groupId>
106+
</exclusion>
107+
<exclusion>
108+
<artifactId>javase</artifactId>
109+
<groupId>com.google.zxing</groupId>
110+
</exclusion>
111+
<exclusion>
112+
<artifactId>bcprov-jdk15on</artifactId>
113+
<groupId>org.bouncycastle</groupId>
114+
</exclusion>
115+
<exclusion>
116+
<artifactId>bcpkix-jdk15on</artifactId>
117+
<groupId>org.bouncycastle</groupId>
118+
</exclusion>
119+
</exclusions>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.apache.httpcomponents</groupId>
123+
<artifactId>httpclient</artifactId>
124+
<version>4.5</version>
125+
<scope>provided</scope>
126+
<exclusions>
127+
<exclusion>
128+
<artifactId>httpcore</artifactId>
129+
<groupId>org.apache.httpcomponents</groupId>
130+
</exclusion>
131+
<exclusion>
132+
<artifactId>commons-codec</artifactId>
133+
<groupId>commons-codec</groupId>
134+
</exclusion>
135+
</exclusions>
136+
</dependency>
137+
<dependency>
138+
<groupId>junit</groupId>
139+
<artifactId>junit</artifactId>
140+
<version>4.12</version>
141+
<scope>test</scope>
142+
<exclusions>
143+
<exclusion>
144+
<artifactId>hamcrest-core</artifactId>
145+
<groupId>org.hamcrest</groupId>
146+
</exclusion>
147+
</exclusions>
148+
</dependency>
149+
</dependencies>
150+
<properties>
151+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
152+
<maven.compiler.source>1.8</maven.compiler.source>
153+
<maven.compiler.target>1.8</maven.compiler.target>
154+
</properties>
155+
</project>
156+

keycloak/sms-provider/keycloak-email-phone-autthenticator.iml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<content url="file://$MODULE_DIR$">
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
88
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
910
<excludeFolder url="file://$MODULE_DIR$/target" />
1011
</content>
1112
<orderEntry type="inheritedJdk" />
@@ -56,5 +57,8 @@
5657
<orderEntry type="library" name="Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.6.7" level="project" />
5758
<orderEntry type="library" name="Maven: joda-time:joda-time:2.8.1" level="project" />
5859
<orderEntry type="library" name="Maven: com.amazonaws:jmespath-java:1.11.173" level="project" />
60+
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.3.1" level="project" />
61+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
62+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
5963
</component>
6064
</module>

keycloak/sms-provider/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>aws-java-sdk-sns</artifactId>
8787
<version>1.11.173</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>com.google.code.gson</groupId>
91+
<artifactId>gson</artifactId>
92+
<version>2.3.1</version>
93+
</dependency>
8994
<dependency>
9095
<groupId>junit</groupId>
9196
<artifactId>junit</artifactId>

keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ public class KeycloakSmsAuthenticatorConstants {
2121
// User credentials (used to persist the sent sms code + expiration time cluster wide)
2222
public static final String USR_CRED_MDL_SMS_CODE = "sms-auth.code";
2323
public static final String USR_CRED_MDL_SMS_EXP_TIME = "sms-auth.exp-time";
24+
25+
public static final String AMAZON_SNS_PROVIDER_CONFIGURATIONS_PATH = "sms-provider/awsSnsCreds.json";
26+
public static final String MSG91_SMS_PROVIDER_CONFIGURATIONS_PATH = "sms-provider/Msg91Creds.json";
2427
}

keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorUtil.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.sunbird.keycloak;
22

3-
import com.amazonaws.services.sns.model.PublishResult;
3+
import com.amazonaws.util.StringUtils;
44
import org.jboss.logging.Logger;
55
import org.keycloak.models.AuthenticatorConfigModel;
66
import org.keycloak.models.UserModel;
7-
import org.sunbird.aws.snsclient.SnsNotificationService;
7+
import org.sunbird.sms.msg91.Msg91SmsProviderFactory;
8+
import org.sunbird.sms.provider.ISmsProvider;
9+
import org.sunbird.utils.JsonUtil;
810

11+
import java.io.File;
912
import java.util.List;
13+
import java.util.Map;
1014
import java.util.Random;
1115

1216
/**
@@ -84,20 +88,32 @@ public static String setDefaultCountryCodeIfZero(String mobileNumber) {
8488
}
8589

8690
static boolean sendSmsCode(String mobileNumber, String code, AuthenticatorConfigModel config) {
87-
// Send an SMS
88-
KeycloakSmsAuthenticatorUtil.logger.debug("Sending " + code + " to mobileNumber " + mobileNumber);
91+
String smsText = createMessage(code, mobileNumber, config);
92+
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : smsText - " + smsText);
8993

90-
String smsUsr = getConfigString(config, KeycloakSmsAuthenticatorConstants.CONF_PRP_SMS_CLIENTTOKEN);
91-
String smsPwd = getConfigString(config, KeycloakSmsAuthenticatorConstants.CONF_PRP_SMS_CLIENTSECRET);
94+
Boolean msg91SmsProviderStatus = send(mobileNumber, smsText);
95+
if (msg91SmsProviderStatus != null) return msg91SmsProviderStatus;
9296

93-
String smsText = createMessage(code, mobileNumber, config);
94-
try {
95-
PublishResult send_result = new SnsNotificationService().send(setDefaultCountryCodeIfZero(mobileNumber), smsText, smsUsr, smsPwd);
96-
return true;
97-
} catch (Exception e) {
98-
//Just like pokemon
99-
return false;
97+
return false;
98+
}
99+
100+
private static Boolean send(String mobileNumber, String code) {
101+
String filePath = new File(KeycloakSmsAuthenticatorConstants.MSG91_SMS_PROVIDER_CONFIGURATIONS_PATH).getAbsolutePath();
102+
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : filePath - " + filePath);
103+
104+
if (!StringUtils.isNullOrEmpty(filePath)) {
105+
Map<String, String> configurations = JsonUtil.readFromJson(filePath);
106+
logger.debug("KeycloakSmsAuthenticatorUtil@sendSmsCode : configurations - " + configurations);
107+
108+
Msg91SmsProviderFactory msg91SmsProviderFactory = new Msg91SmsProviderFactory();
109+
110+
ISmsProvider msg91SmsProvider = msg91SmsProviderFactory.create(configurations);
111+
112+
if (msg91SmsProvider != null) {
113+
return msg91SmsProvider.send(mobileNumber, code);
114+
}
100115
}
116+
return null;
101117
}
102118

103119
static String getSmsCode(long nrOfDigits) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.sunbird.sms;
2+
3+
import java.util.Map;
4+
5+
public class SMSConfigurationUtil {
6+
7+
public static String getConfigString(Map<String, String> config, String configName) {
8+
return getConfigString(config, configName, null);
9+
}
10+
11+
public static String getConfigString(Map<String, String> config, String configName, String defaultValue) {
12+
13+
String value = defaultValue;
14+
15+
if (config.containsKey(configName)) {
16+
// Get value
17+
value = config.get(configName);
18+
}
19+
20+
return value;
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.sunbird.sms;
2+
3+
public final class SmsConfigurationConstants {
4+
public static final String CONF_SMS_TOKEN = "token";
5+
public static final String CONF_SMS_SECRET = "secret";
6+
public static final String CONF_SMS_BASE_URL = "sms_base_url";
7+
public static final String CONF_SMS_GET_URL = "sms_get_url";
8+
public static final String CONF_SMS_POST_URL = "sms_post_url";
9+
public static final String CONF_SMS_METHOD_TYPE = "sms_method_type";
10+
public static final String CONF_SMS_USERNAME = "sms_username";
11+
public static final String CONF_SMS_PASSWORD = "sms_password";
12+
public static final String CONF_SMS_AUTHTYPE = "sms_authtype";
13+
public static final String CONF_SMS_CONTENT_TYPE = "sms_content_type";
14+
public static final String CONF_SMS_PROXY_URL = "sms_proxy_url";
15+
public static final String CONF_SMS_PROXY_USERNAME = "sms_proxy_username";
16+
public static final String CONF_SMS_PROXY_PASSWORD = "sms_proxy_password";
17+
public static final String CONF_AUTH_METHOD_BASIC = "basic_authentication";
18+
public static final String CONF_AUTH_METHOD_INMESSAGE = "in_message_authentication";
19+
public static final String CONF_AUTH_KEY = "auth_key";
20+
public static final String CONF_SMS_SENDER = "sender";
21+
public static final String CONF_SMS_COUNTRY = "country";
22+
public static final String CONF_SMS_ROUTE = "route";
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sunbird.sms.amazonsns;
2+
3+
4+
import org.sunbird.sms.provider.ISmsProvider;
5+
import org.sunbird.sms.provider.ISmsProviderFactory;
6+
7+
import java.util.Map;
8+
9+
public class AmazonSnsFactory implements ISmsProviderFactory {
10+
private static AmazonSnsProvider amazonSnsProvider = null;
11+
12+
@Override
13+
public ISmsProvider create(Map<String, String> configurations) {
14+
if (amazonSnsProvider == null) {
15+
amazonSnsProvider = new AmazonSnsProvider();
16+
amazonSnsProvider.configure(configurations);
17+
}
18+
19+
return amazonSnsProvider;
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.sunbird.sms.amazonsns;
2+
3+
import com.amazonaws.services.sns.model.MessageAttributeValue;
4+
import com.amazonaws.services.sns.model.PublishRequest;
5+
import org.jboss.logging.Logger;
6+
import org.sunbird.aws.snsclient.SnsClientFactory;
7+
import org.sunbird.sms.SMSConfigurationUtil;
8+
import org.sunbird.sms.SmsConfigurationConstants;
9+
import org.sunbird.sms.provider.ISmsProvider;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
public class AmazonSnsProvider implements ISmsProvider {
15+
16+
private static Logger logger = Logger.getLogger(AmazonSnsProvider.class);
17+
18+
private Map<String, String> configurations;
19+
20+
@Override
21+
public boolean send(String phoneNumber, String smsText) {
22+
logger.debug("AmazonSnsProvider@send : phoneNumber - " + phoneNumber + " & Sms text - " + smsText);
23+
24+
Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();
25+
smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue()
26+
.withStringValue("HomeOffice")
27+
.withDataType("String"));
28+
29+
String clientToken = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_TOKEN);
30+
String clientSecret = SMSConfigurationUtil.getConfigString(configurations, SmsConfigurationConstants.CONF_SMS_SECRET);
31+
32+
logger.debug("AmazonSnsProvider@send : clientToken - " + clientToken + " & clientSecret - " + clientSecret);
33+
34+
35+
try {
36+
SnsClientFactory.getSnsClient(clientToken, clientSecret).publish(new PublishRequest()
37+
.withMessage(smsText)
38+
.withPhoneNumber(phoneNumber)
39+
.withMessageAttributes(smsAttributes));
40+
41+
return true;
42+
} catch (Exception e) {
43+
logger.debug("AmazonSnsProvider@Send : Exception Caught -" + e.getMessage());
44+
return false;
45+
}
46+
}
47+
48+
@Override
49+
public void configure(Map<String, String> configurations) {
50+
this.configurations = configurations;
51+
}
52+
}

0 commit comments

Comments
 (0)