One-time key authorization

Authenticate users with one-time keys in the Voximplant mobile SDK.

View as Markdown

There are some cases when you need to log in users into a web phone automatically, but do not want a plain text password (which is accepted by the login method) to be available in JavaScript code. This is where we can use the loginWithOneTimeKey method to log in.

Let us assume that you need to log a user myuser into an application myapp in your Voximplant account myaccount, and this user has the password mypass.

  1. Add a handler for the AuthResult event.
  2. Request a one-time authentication login key using the requestOneTimeLoginKey function.
Login key TTL

After you request your login key with requestOneTimeLoginKey, it expires after 5 minutes. If you require a one-time login key for this account from another device, the current login key expires.

  1. Calculate the token on your backend.
1MD5(`${login_key}|${MD5(`${myuser}:voximplant.com:${mypass}`)}`)
Please note
  1. myuser does not include @appname.accname.voximplant.com;
    1. this formula allows you to store only hashes on the backend, not passwords.
  1. Send this token using the loginWithOneTimeKey function.
  2. Finally, you will receive AuthResult with result == true if the correct password was specified.
Example
1// app.js on client
2// Please, change this data before go.
3const appName = 'VOXAPPLICATION';
4const account = 'ACCOUNT';
5const username = `${appUser}@${appName}.${account}.voximplant.com`;
6
7const voximplant = VoxImplant.getInstance();
8voximplant.init();
9// Connect to the cloud and request a key
10voximplant.connect().then(() => voximplant.requestOneTimeLoginKey(username));
11
12// Listen to the server response
13voximplant.addEventListener(VoxImplant.Events.AuthResult, (e) => {
14 console.log(`AuthResult: ${e.result}`);
15 console.log(`Auth code: ${e.code}`);
16 if (e.result) {
17 // Login is successful
18 } else if (e.code == 302) {
19 console.log(e.key);
20 // IMPORTANT: You should always calculate the token on your backend!
21 $.post(
22 'https://your.backend.com/',
23 {
24 key: e.key
25 },
26 (token) => {
27 voximplant.loginWithOneTimeKey(username, token);
28 },
29 'text'
30 );
31 }
32});

Example backend function PHP:

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

Ruby:

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

Node.js:

1const app = express();
2
3let md5 = crypto.createHash('md5');
4
5app.use(bodyParser.urlencoded({ extended: false }));
6
7app.post('/', cors(), (req, res) => {
8 res.writeHead(200, { 'Content-Type': 'text/plain' });
9 const hash = md5
10 .update(`${req.body.appUser}:voximplant.com:${req.body.appUserPassword}`)
11 .digest('hex');
12 md5 = crypto.createHash('md5');
13 const token = md5.update(`${req.body.key}\|${hash}`).digest('hex');
14 res.end(token);
15});

Java(Spring):

1@RestController
2@EnableAutoConfiguration
3public class StartPoint {
4
5 private static final String login = "";
6 private static final String pass = "";
7 private static final String HASH;
8
9 static {
10 HASH = DigestUtils.md5DigestAsHex((login + ":voximplant.com:" + pass).getBytes());
11 }
12
13 @RequestMapping("/hash/{login}")
14 public String getHash(@PathVariable String login) {
15 String key = login + "|" + HASH;
16 return DigestUtils.md5DigestAsHex(key.getBytes());
17 }
18
19 public static void main(String[] args) {
20 SpringApplication.run(StartPoint.class, args);
21 }
22}