Monday, September 25, 2017

Implementation of 2 factor for OTP verification

Go to register on https://2factor.in/CP/register.php for start OTP messaging for verification or you can use it for send bulk send messages
After successfully register go to login and create authetication token for using this on your code and make this entry in your application properties.
 ex- apiKey.dev=e20a51a6-82c8-11e7-94da-0200cd936042
Go to register on https://2factor.in/CP/register.php for start OTP messaging for verification or you can use it for send bulk send messages
After successfully register go to log in and create authentication token for using this on your code and make this entry in your application properties.
 ex- apiKey.dev=e20a51a6-82c8-11e7-94da-0200cd936042
If you want to use send otp messages then first you need to create an otp templates by link https://2factor.in/CP/2FA_SMS_OTP_Templates_list.php and you need to click on 'Create new OTP Template' after that you see page like below-
There is a field you need to fill it your value.
Template Name - This is like your template identifier name
Template - In this field,
OTP template must be less than 150 characters and must contain string XXXX ( 4 'X' characters ),
which will be replaced with actual OTP value.
Ex.
To send message
'1234 is your one-time password for 2Factor.in' OR
'123456 is your one-time password for 2Factor.in'
OTP template must be defined as
'XXXX is your one-time password for 2Factor.in'
and some additional details need to be filled as
Company Name and Website.
After you need to be integrate 2factor API for it-

Below code explains how to implement API in Java code-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public Map<string,object> sendOtpWithTwoFactor(Long mobNum,String phoneCode,String otp){
        LOGGER.info("Enter into send OTP method for indian number...");
        Map<string,object> result = new HashMap<string,object>();
        RestTemplate restTemp = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        HttpEntity<string> entity = new HttpEntity<string>("parameters", headers);
         
        String apiKey = envConfiguration.getTwoFactorApiKey();
         
        String url="https://2factor.in/API/V1/"+apiKey+"/SMS/+"+phoneCode+mobNum+"/"+otp+"/"+"BELICO";
        LOGGER.info("URL:::::::::"+url);
        ResponseEntity<string> response = null;
        response = restTemp.exchange(url, HttpMethod.GET, entity, String.class);
        LOGGER.info("Response :::::::"+response.getBody());
         
        JSONObject txnObject = new JSONObject(response.getBody());
        LOGGER.info("txnObject"+txnObject);
        if(txnObject.get("Status").equals("Success")){
            LOGGER.info("OTP send successfully");
            result.put("otpResponse", txnObject);
            return result;
        }
        else
        {
            LOGGER.info("OTP not send");
            result.put("otpResponse", txnObject);
            return result;
        }
         
    }
  
 
  
</string></string></string></string,object></string,object></string,object>

No comments:

Post a Comment