SIGN UP

Automated Voximplant SDK login

Automated Voximplant SDK login

There are some cases when you need to login users into a web phone automatically, but don't want plain text password (that is accepted by VoxImplant.getInstance().login()) to be available in JavaScript code.

We have a solution for this problem. Let's assume that you need to log in a user named myuser to application myapp in your Voximplant account named myaccount, and that user has password mypass.

  1. Add a handler for the AuthResult event.
  2. Request one-time authentication login key using the requestOneTimeLoginKey function.
  3. Calculate token on your backend
    MD5(`${login_key}|${MD5(`${myuser}:voximplant.com:${mypass}`)}`)

    PLEASE NOTICE that myuser doesn’t include @appname.accname.voximplant.com 

  4. Send this token using the loginWithOneTimeKey function
  5. Finally, you should receive AuthResult with result == true if the correct password was specified.

Example:

// app.js on client
// Please, change this data before go.
const appUser = 'APPLICATIONUSER';
const appUserPassword = 'SECUREPASSWORD';
const appName = 'VOXAPPLICATION';
const account = 'ACCOUNT';
const URI = `${appUser}@${appName}.${account}.voximplant.com`;

const voximplant = VoxImplant.getInstance();
voximplant.init();
// Connect to the cloud and request a key
voximplant.connect().then(() => voximplant.requestOneTimeLoginKey(URI));

// Listen to the server response
voximplant.addEventListener(VoxImplant.Events.AuthResult, e => {
  console.log(`AuthResult: ${e.result}`);
  console.log(`Auth code: ${e.code}`);
  if (e.result) {
    // Login is successful
  } else if (e.code == 302) {
    console.log(e.key);
    // IMPORTANT: You should always calculate the token on your backend!
    $.post('https://your.backend.com/', {
      key: e.key,
      appUser: appUser,
      appUserPassword: appUserPassword
    }, token => {
      voximplant.loginWithOneTimeKey(URI, token);
    }, 'text');
  }
  
});

 

Example backend function PHP:

echo md5($_REQUEST['key'].'|'.md5($myuser.':voximplant.com:'.$mypass));

 

Ruby:

require 'digest/md5' concat Digest::MD5.hexdigest(request.POST['key']+'|'+Digest::MD5.hexdigest(myuser+":voximplant.com:"+mypass))

 

Node.js:

const app = express();

let md5 = crypto.createHash('md5');

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/', cors(), (req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  const hash = md5.update(`${req.body.appUser}:voximplant.com:${req.body.appUserPassword}`).digest('hex');
  md5 = crypto.createHash('md5')
  const token = md5.update(`${req.body.key}\|${hash}`).digest('hex');
  res.end(token);
});

 

Java(Spring):

@RestController
@EnableAutoConfiguration
public class StartPoint {
  private static final String login = "";
  private static final String pass = "";
  private static final String HASH;
  static {
    HASH = DigestUtils.md5DigestAsHex((login + ":voximplant.com:" + pass).getBytes());
  }
  @RequestMapping("/hash/{login}")
  public String getHash(@PathVariable String login) {
    String key = login + "|" + HASH;
    return DigestUtils.md5DigestAsHex(key.getBytes());
  }
  public static void main(String[] args) {
    SpringApplication.run(StartPoint.class, args);
  }
}
Tags:ios sdkweb sdkandroid sdk
B6A24216-9891-45D1-9D1D-E7359CEB8282 Created with sketchtool.

Comments(0)

Add your comment

Please complete this field.

Recommended

Sign up for a free Voximplant developer account or talk to our experts
SIGN UP