diff --git a/README.md b/README.md index 3ed8bd6..471edd6 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,62 @@ This will also install the Telesign Self-service Java SDK since it is a dependen If you use a Telesign SDK to make your request, authentication is handled behind-the-scenes for you. All you need to provide is your Customer ID and API Key. The SDKs apply Digest authentication whenever they make a request to a Telesign service where it is supported. When Digest authentication is not supported, the SDKs apply Basic authentication. +## SMPP TLSv1.2 Configuration + +To configure SMPP TLSv1.2 in your Java application, follow these steps: + +1. Ensure that your Java application is using Java 8 or later, as specified in the `build.gradle` file. +2. Use the `javax.net.ssl` package to configure the SSL/TLS settings in your Java application. +3. Set the desired encryption cipher by specifying it in the SSL/TLS configuration. + +Here is an example of how to configure SSL/TLS settings and set the desired encryption cipher: + +```java +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.io.InputStream; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; + +public class SmppTlsClient extends RestClient { + + public SmppTlsClient(String customerId, String apiKey, String restEndpoint) { + super(customerId, apiKey, restEndpoint); + configureSslSettings(); + } + + private void configureSslSettings() { + try { + // Load the certificate + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream caInput = getClass().getResourceAsStream("/path/to/your/certificate.crt"); + X509Certificate ca = (X509Certificate) cf.generateCertificate(caInput); + + // Create a KeyStore containing the trusted certificate + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, null); + keyStore.setCertificateEntry("ca", ca); + + // Create a TrustManager that trusts the certificate in the KeyStore + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keyStore); + X509TrustManager trustManager = (X509TrustManager) tmf.getTrustManagers()[0]; + + // Create an SSLContext that uses the TrustManager + SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); + sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, null); + + // Set the SSLContext to be used by the RestClient + setSslSocketFactory(sslContext.getSocketFactory()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} +``` + ## What's next * Learn to send a request to Telesign with code with one of our [tutorials](https://developer.telesign.com/enterprise/docs/tutorials). diff --git a/src/main/java/com/telesign/enterprise/MessagingClient.java b/src/main/java/com/telesign/enterprise/MessagingClient.java index b04d723..ec7220f 100644 --- a/src/main/java/com/telesign/enterprise/MessagingClient.java +++ b/src/main/java/com/telesign/enterprise/MessagingClient.java @@ -1,7 +1,14 @@ package com.telesign.enterprise; -import java.io.IOException; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.io.InputStream; import java.net.Proxy; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.io.IOException; import java.security.GeneralSecurityException; import java.util.HashMap; import java.util.Map; @@ -15,10 +22,12 @@ public class MessagingClient extends com.telesign.RestClient { public MessagingClient(String customerId, String apiKey) { super(customerId, apiKey, "https://rest-ww.telesign.com"); + configureSslSettings(); } public MessagingClient(String customerId, String apiKey, String restEndpoint) { super(customerId, apiKey, restEndpoint); + configureSslSettings(); } public MessagingClient(String customerId, @@ -31,6 +40,35 @@ public MessagingClient(String customerId, final String proxyUsername, final String proxyPassword) { super(customerId, apiKey, restEndpoint, connectTimeout, readTimeout, writeTimeout, proxy, proxyUsername, proxyPassword); + configureSslSettings(); + } + + private void configureSslSettings() { + try { + // Load the certificate + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream caInput = getClass().getResourceAsStream("/path/to/your/certificate.crt"); + X509Certificate ca = (X509Certificate) cf.generateCertificate(caInput); + + // Create a KeyStore containing the trusted certificate + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, null); + keyStore.setCertificateEntry("ca", ca); + + // Create a TrustManager that trusts the certificate in the KeyStore + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keyStore); + X509TrustManager trustManager = (X509TrustManager) tmf.getTrustManagers()[0]; + + // Create an SSLContext that uses the TrustManager + SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); + sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, null); + + // Set the SSLContext to be used by the RestClient + setSslSocketFactory(sslContext.getSocketFactory()); + } catch (Exception e) { + e.printStackTrace(); + } } /** diff --git a/src/main/java/com/telesign/enterprise/SmppTlsClient.java b/src/main/java/com/telesign/enterprise/SmppTlsClient.java new file mode 100644 index 0000000..388bfa8 --- /dev/null +++ b/src/main/java/com/telesign/enterprise/SmppTlsClient.java @@ -0,0 +1,48 @@ +package com.telesign.enterprise; + +import com.telesign.RestClient; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.io.InputStream; +import java.net.Proxy; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; + +public class SmppTlsClient extends RestClient { + + public SmppTlsClient(String customerId, String apiKey, String restEndpoint) { + super(customerId, apiKey, restEndpoint); + configureSslSettings(); + } + + private void configureSslSettings() { + try { + // Load the certificate + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream caInput = getClass().getResourceAsStream("/path/to/your/certificate.crt"); + X509Certificate ca = (X509Certificate) cf.generateCertificate(caInput); + + // Create a KeyStore containing the trusted certificate + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, null); + keyStore.setCertificateEntry("ca", ca); + + // Create a TrustManager that trusts the certificate in the KeyStore + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keyStore); + X509TrustManager trustManager = (X509TrustManager) tmf.getTrustManagers()[0]; + + // Create an SSLContext that uses the TrustManager + SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); + sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, null); + + // Set the SSLContext to be used by the RestClient + setSslSocketFactory(sslContext.getSocketFactory()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/telesign/enterprise/VerifyClient.java b/src/main/java/com/telesign/enterprise/VerifyClient.java index bb49905..ac7e70b 100644 --- a/src/main/java/com/telesign/enterprise/VerifyClient.java +++ b/src/main/java/com/telesign/enterprise/VerifyClient.java @@ -1,148 +1,186 @@ -package com.telesign.enterprise; - -import com.telesign.RestClient; - -import java.io.IOException; -import java.net.Proxy; -import java.security.GeneralSecurityException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * The Verify API delivers phone-based verification and two-factor authentication using a time-based, one-time passcode - * sent via SMS message, Voice call or Push Notification. - */ -public class VerifyClient extends RestClient { - - private static final String VERIFY_SMS_RESOURCE = "/v1/verify/sms"; - private static final String VERIFY_VOICE_RESOURCE = "/v1/verify/call"; - private static final String VERIFY_SMART_RESOURCE = "/v1/verify/smart"; - private static final String VERIFY_STATUS_RESOURCE = "/v1/verify/%s"; - private static final String VERIFY_COMPLETION_RESOURCE = "/v1/verify/completion/%s"; - - private static final String BASE_URL_VERIFICATION_PROCESS = "https://verify.telesign.com"; - private static final String DEFAULT_FS_BASE_URL = "https://rest-ww.telesign.com"; - private static final String PATH_VERIFICATION = "/verification"; - - public VerifyClient(String customerId, String apiKey) { - super(customerId, apiKey, DEFAULT_FS_BASE_URL); - } - - public VerifyClient(String customerId, String apiKey, String restEndpoint) { - super(customerId, apiKey, restEndpoint); - } - - public VerifyClient(String customerId, - String apiKey, - String restEndpoint, - Integer connectTimeout, - Integer readTimeout, - Integer writeTimeout, - Proxy proxy, - final String proxyUsername, - final String proxyPassword) { - super(customerId, apiKey, restEndpoint, connectTimeout, readTimeout, writeTimeout, proxy, proxyUsername, proxyPassword); - } - - /** - * The SMS Verify API delivers phone-based verification and two-factor authentication using a time-based, - * one-time passcode sent over SMS. - *

- * See https://developer.telesign.com/docs/rest_api-verify-sms for detailed API documentation. - */ - public TelesignResponse sms(String phoneNumber, Map params) throws IOException, GeneralSecurityException { - - if (params == null) { - params = new HashMap<>(); - } - - params.put("phone_number", phoneNumber); - this.setRestEndpoint(DEFAULT_FS_BASE_URL); - return this.post(VERIFY_SMS_RESOURCE, params); - } - - /** - * Use this action to create a verification process for the specified phone number. - *

- * See https://developer.telesign.com/enterprise/reference/createverificationprocess for detailed API documentation. - */ - public TelesignResponse createVerificationProcess(String phoneNumber, HashMap params) throws IOException, GeneralSecurityException { - - if (params == null) { - params = new HashMap(); - } - - HashMap infoRecipient = new HashMap<>(); - infoRecipient.put("phone_number", phoneNumber); - params.put("recipient", infoRecipient); - - if (!params.containsKey("verification_policy")) { - List infoVerificationPolicy = new ArrayList<>(); - HashMap infoMethod = new HashMap<>(); - infoMethod.put("method", "sms"); - infoVerificationPolicy.add(infoMethod); - params.put("verification_policy", infoVerificationPolicy); - } - - this.setRestEndpoint(BASE_URL_VERIFICATION_PROCESS); - return this.post(PATH_VERIFICATION, params, "application/json", "Basic"); - } - - /** - * The Voice Verify API delivers patented phone-based verification and two-factor authentication using a one-time - * passcode sent over verify_voice message. - *

- * See https://developer.telesign.com/docs/rest_api-verify-call for detailed API documentation. - */ - public TelesignResponse voice(String phoneNumber, Map params) throws IOException, GeneralSecurityException { - - if (params == null) { - params = new HashMap<>(); - } - - params.put("phone_number", phoneNumber); - - return this.post(VERIFY_VOICE_RESOURCE, params); - } - - /** - * The Smart Verify web service simplifies the process of verifying user identity by integrating several TeleSign - * web services into a single API call. This eliminates the need for you to make multiple calls to the TeleSign - * Verify resource. - *

- * See https://developer.telesign.com/docs/rest_api-smart-verify for detailed API documentation. - */ - public TelesignResponse smart(String phoneNumber, String ucid, Map params) throws IOException, GeneralSecurityException { - - if (params == null) { - params = new HashMap<>(); - } - - params.put("phone_number", phoneNumber); - params.put("ucid", ucid); - - return this.post(VERIFY_SMART_RESOURCE, params); - } - - /** - * Retrieves the verification result for any verify resource. - *

- * See https://developer.telesign.com/docs/rest_api-verify-transaction-callback for detailed API documentation. - */ - public TelesignResponse status(String referenceId, Map params) throws IOException, GeneralSecurityException { - - return this.get(String.format(VERIFY_STATUS_RESOURCE, referenceId), params); - } - - /** - * Notifies TeleSign that a verification was successfully delivered to the user in order to help improve the - * quality of message delivery routes. - *

- * See https://developer.telesign.com/docs/completion-service-for-verify-products for detailed API documentation. - */ - public TelesignResponse completion(String referenceId, Map params) throws IOException, GeneralSecurityException { - return this.put(String.format(VERIFY_COMPLETION_RESOURCE, referenceId), params); - } -} +package com.telesign.enterprise; + +import com.telesign.RestClient; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.io.InputStream; +import java.net.Proxy; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.io.IOException; +import java.net.Proxy; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * The Verify API delivers phone-based verification and two-factor authentication using a time-based, one-time passcode + * sent via SMS message, Voice call or Push Notification. + */ +public class VerifyClient extends RestClient { + + private static final String VERIFY_SMS_RESOURCE = "/v1/verify/sms"; + private static final String VERIFY_VOICE_RESOURCE = "/v1/verify/call"; + private static final String VERIFY_SMART_RESOURCE = "/v1/verify/smart"; + private static final String VERIFY_STATUS_RESOURCE = "/v1/verify/%s"; + private static final String VERIFY_COMPLETION_RESOURCE = "/v1/verify/completion/%s"; + + private static final String BASE_URL_VERIFICATION_PROCESS = "https://verify.telesign.com"; + private static final String DEFAULT_FS_BASE_URL = "https://rest-ww.telesign.com"; + private static final String PATH_VERIFICATION = "/verification"; + + public VerifyClient(String customerId, String apiKey) { + super(customerId, apiKey, DEFAULT_FS_BASE_URL); + } + + public VerifyClient(String customerId, String apiKey, String restEndpoint) { + super(customerId, apiKey, restEndpoint); + configureSslSettings(); + } + + public VerifyClient(String customerId, + String apiKey, + String restEndpoint, + Integer connectTimeout, + Integer readTimeout, + Integer writeTimeout, + Proxy proxy, + final String proxyUsername, + final String proxyPassword) { + super(customerId, apiKey, restEndpoint, connectTimeout, readTimeout, writeTimeout, proxy, proxyUsername, proxyPassword); + configureSslSettings(); + } + + private void configureSslSettings() { + try { + // Load the certificate + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream caInput = getClass().getResourceAsStream("/path/to/your/certificate.crt"); + X509Certificate ca = (X509Certificate) cf.generateCertificate(caInput); + + // Create a KeyStore containing the trusted certificate + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, null); + keyStore.setCertificateEntry("ca", ca); + + // Create a TrustManager that trusts the certificate in the KeyStore + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(keyStore); + X509TrustManager trustManager = (X509TrustManager) tmf.getTrustManagers()[0]; + + // Create an SSLContext that uses the TrustManager + SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); + sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, null); + + // Set the SSLContext to be used by the RestClient + setSslSocketFactory(sslContext.getSocketFactory()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * The SMS Verify API delivers phone-based verification and two-factor authentication using a time-based, + * one-time passcode sent over SMS. + *

+ * See https://developer.telesign.com/docs/rest_api-verify-sms for detailed API documentation. + */ + public TelesignResponse sms(String phoneNumber, Map params) throws IOException, GeneralSecurityException { + + if (params == null) { + params = new HashMap<>(); + } + + params.put("phone_number", phoneNumber); + this.setRestEndpoint(DEFAULT_FS_BASE_URL); + return this.post(VERIFY_SMS_RESOURCE, params); + } + + /** + * Use this action to create a verification process for the specified phone number. + *

+ * See https://developer.telesign.com/enterprise/reference/createverificationprocess for detailed API documentation. + */ + public TelesignResponse createVerificationProcess(String phoneNumber, HashMap params) throws IOException, GeneralSecurityException { + + if (params == null) { + params = new HashMap(); + } + + HashMap infoRecipient = new HashMap<>(); + infoRecipient.put("phone_number", phoneNumber); + params.put("recipient", infoRecipient); + + if (!params.containsKey("verification_policy")) { + List infoVerificationPolicy = new ArrayList<>(); + HashMap infoMethod = new HashMap<>(); + infoMethod.put("method", "sms"); + infoVerificationPolicy.add(infoMethod); + params.put("verification_policy", infoVerificationPolicy); + } + + this.setRestEndpoint(BASE_URL_VERIFICATION_PROCESS); + return this.post(PATH_VERIFICATION, params, "application/json", "Basic"); + } + + /** + * The Voice Verify API delivers patented phone-based verification and two-factor authentication using a one-time + * passcode sent over verify_voice message. + *

+ * See https://developer.telesign.com/docs/rest_api-verify-call for detailed API documentation. + */ + public TelesignResponse voice(String phoneNumber, Map params) throws IOException, GeneralSecurityException { + + if (params == null) { + params = new HashMap<>(); + } + + params.put("phone_number", phoneNumber); + + return this.post(VERIFY_VOICE_RESOURCE, params); + } + + /** + * The Smart Verify web service simplifies the process of verifying user identity by integrating several TeleSign + * web services into a single API call. This eliminates the need for you to make multiple calls to the TeleSign + * Verify resource. + *

+ * See https://developer.telesign.com/docs/rest_api-smart-verify for detailed API documentation. + */ + public TelesignResponse smart(String phoneNumber, String ucid, Map params) throws IOException, GeneralSecurityException { + + if (params == null) { + params = new HashMap<>(); + } + + params.put("phone_number", phoneNumber); + params.put("ucid", ucid); + + return this.post(VERIFY_SMART_RESOURCE, params); + } + + /** + * Retrieves the verification result for any verify resource. + *

+ * See https://developer.telesign.com/docs/rest_api-verify-transaction-callback for detailed API documentation. + */ + public TelesignResponse status(String referenceId, Map params) throws IOException, GeneralSecurityException { + + return this.get(String.format(VERIFY_STATUS_RESOURCE, referenceId), params); + } + + /** + * Notifies TeleSign that a verification was successfully delivered to the user in order to help improve the + * quality of message delivery routes. + *

+ * See https://developer.telesign.com/docs/completion-service-for-verify-products for detailed API documentation. + */ + public TelesignResponse completion(String referenceId, Map params) throws IOException, GeneralSecurityException { + return this.put(String.format(VERIFY_COMPLETION_RESOURCE, referenceId), params); + } +}