diff --git a/keycloak/sms-provider/Msg91Creds.json b/keycloak/sms-provider/Msg91Creds.json index 4887665e..f674f555 100644 --- a/keycloak/sms-provider/Msg91Creds.json +++ b/keycloak/sms-provider/Msg91Creds.json @@ -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?" } \ No newline at end of file diff --git a/keycloak/sms-provider/dependency-reduced-pom.xml b/keycloak/sms-provider/dependency-reduced-pom.xml new file mode 100644 index 00000000..d7f9ecd5 --- /dev/null +++ b/keycloak/sms-provider/dependency-reduced-pom.xml @@ -0,0 +1,156 @@ + + + 4.0.0 + org.sunbird + keycloak-email-phone-autthenticator + 1.0-SNAPSHOT + + + + maven-shade-plugin + 3.0.0 + + + package + + shade + + + + + classworlds:classworlds + junit:junit + jmock:* + *:xml-apis + org.apache.maven:lib:tests + + + + + + + + + + + org.keycloak + keycloak-core + 3.2.0.Final + provided + + + keycloak-common + org.keycloak + + + bcprov-jdk15on + org.bouncycastle + + + bcpkix-jdk15on + org.bouncycastle + + + + + org.keycloak + keycloak-server-spi + 3.2.0.Final + provided + + + org.keycloak + keycloak-server-spi-private + 3.2.0.Final + provided + + + org.jboss.logging + jboss-logging + 3.3.1.Final + provided + + + org.keycloak + keycloak-services + 3.2.0.Final + provided + + + javax.mail-api + javax.mail + + + jboss-servlet-api_3.0_spec + org.jboss.spec.javax.servlet + + + twitter4j-core + org.twitter4j + + + resteasy-jaxrs + org.jboss.resteasy + + + jboss-jaxrs-api_2.0_spec + org.jboss.spec.javax.ws.rs + + + jboss-transaction-api_1.2_spec + org.jboss.spec.javax.transaction + + + resteasy-multipart-provider + org.jboss.resteasy + + + javase + com.google.zxing + + + bcprov-jdk15on + org.bouncycastle + + + bcpkix-jdk15on + org.bouncycastle + + + + + org.apache.httpcomponents + httpclient + 4.5 + provided + + + httpcore + org.apache.httpcomponents + + + commons-codec + commons-codec + + + + + junit + junit + 4.12 + test + + + hamcrest-core + org.hamcrest + + + + + + UTF-8 + 1.8 + 1.8 + + + diff --git a/keycloak/sms-provider/keycloak-email-phone-autthenticator.iml b/keycloak/sms-provider/keycloak-email-phone-autthenticator.iml index 8c11a5d4..499d7e3f 100644 --- a/keycloak/sms-provider/keycloak-email-phone-autthenticator.iml +++ b/keycloak/sms-provider/keycloak-email-phone-autthenticator.iml @@ -6,6 +6,7 @@ + @@ -56,5 +57,7 @@ + + \ No newline at end of file diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorConstants.java b/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorConstants.java index 77837852..90e6b458 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorConstants.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorConstants.java @@ -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"; } diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorUtil.java b/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorUtil.java index fec8b5b2..d5c11548 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorUtil.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/keycloak/KeycloakSmsAuthenticatorUtil.java @@ -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)) { @@ -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 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) { diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/sms/MessageProviderFactory.java b/keycloak/sms-provider/src/main/java/org/sunbird/sms/MessageProviderFactory.java index 97399766..406dc4fb 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/sms/MessageProviderFactory.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/sms/MessageProviderFactory.java @@ -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; diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/sms/SmsConfigurationType.java b/keycloak/sms-provider/src/main/java/org/sunbird/sms/SmsConfigurationType.java index 4ed62d51..0ca7cac4 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/sms/SmsConfigurationType.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/sms/SmsConfigurationType.java @@ -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"; } diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/sms/amazonsns/AmazonSnsFactory.java b/keycloak/sms-provider/src/main/java/org/sunbird/sms/amazonsns/AmazonSnsFactory.java index 90b51e46..a7e0fc08 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/sms/amazonsns/AmazonSnsFactory.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/sms/amazonsns/AmazonSnsFactory.java @@ -1,5 +1,6 @@ package org.sunbird.sms.amazonsns; + import org.sunbird.sms.provider.ISmsProvider; import org.sunbird.sms.provider.ISmsProviderFactory; diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/sms/msg91/Msg91SmsProvider.java b/keycloak/sms-provider/src/main/java/org/sunbird/sms/msg91/Msg91SmsProvider.java index acf610f1..c099c790 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/sms/msg91/Msg91SmsProvider.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/sms/msg91/Msg91SmsProvider.java @@ -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; @@ -39,9 +40,91 @@ public void configure(Map 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); @@ -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")); diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/sms/provider/ISmsProvider.java b/keycloak/sms-provider/src/main/java/org/sunbird/sms/provider/ISmsProvider.java index b6e41e70..91dda602 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/sms/provider/ISmsProvider.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/sms/provider/ISmsProvider.java @@ -2,5 +2,5 @@ public interface ISmsProvider extends ISmsProviderConfigurations { - boolean send(String phoneNumber,String smsText); + boolean send(String phoneNumber, String smsText); } diff --git a/keycloak/sms-provider/src/main/java/org/sunbird/utils/JsonUtil.java b/keycloak/sms-provider/src/main/java/org/sunbird/utils/JsonUtil.java index 98a68c51..e955b40d 100644 --- a/keycloak/sms-provider/src/main/java/org/sunbird/utils/JsonUtil.java +++ b/keycloak/sms-provider/src/main/java/org/sunbird/utils/JsonUtil.java @@ -24,16 +24,6 @@ public static Map readFromJson(String jsonFile) { e.printStackTrace(); } -// String json = null; -// try { -// JSONParser parser = new JSONParser(); -// //Use JSONObject for simple JSON and JSONArray for array of JSON. -// JSONObject data = (JSONObject) parser.parse(new FileReader(jsonFile));//path to the JSON file. -// json = data.toString(); -// } catch (IOException | ParseException e) { -// e.printStackTrace(); -// } - return map; }