Skip to content

Steps to create trust token.

Below examples give an overview how to create an trust token in NodeJS, Python and cSharp programming languages. Same methods using JWT to create access token in any programming languages you desire. We recommend creating Trust token using RPC functionality of dataBridges server library.

To create a trust token you need to have a payload and client Application secret.

Payload should be in below specified format.

{
    sid: sessionId,
    cname: channelName,
    pub: true,
    conn_info: conn_info
}

You need to replace URL and APP_KEY with the actual URL and Application Key.

Key Description
sid (string) session id of client. dbridge.sessionid
cname (string) ChannelName (in case of pub/sub) or ServerName (in case of RPC)
pub (boolean) Publishing allowed or not.
conn_info (string) Additional connection information. Format explained in Client library trust tokens.

Using JWT's sign/encode (depending on the programming language) functionality Encode your payload with client Application secret (string) which you can get from the dataBridges dashboard.

Full functional code for same is given below.

function gettokenJson(channelName,sessionid,conn_info,response) {
    // Define payload structure
    let payLoad = {
        sid: sessionId,
        cname: channelName,
        pub: true,
        conn_info: conn_info
    };
    try {
        // Create access key by using JWT sign. with application secret provided to you from dBridge management portal for particular client.
        const accesskey = jwt.sign(payLoad, '<YOUR CLIENT APPLICATION SECRET>');
        // return the access key and status to the library. 0= success, 1 =failed.
        response.end(JSON.stringify({
            statuscode: 0,
            error_message: "",
            accesskey: accesskey
        }));
    } catch (err) {
        // error handling.
        response.end(JSON.stringify({
            statuscode: 1,
            error_message: "Unauthrorized",
            accesskey: ''
        }));
    }
}
import jwt
import json

async def gettokenJson(channelName,sessionid,conn_info, response):
    try:
        appSecrt = '<YOUR CLIENT APPLICATION SECRET>'
        newpayload = {"sid": str(sessionid), "cname": channelName, "pub": True, "conn_info": conn_info}
        accesskey = jwt.encode(newpayload, appSecrt);
        newresult = {"statuscode": 0, "error_message": "", "accesskey": accesskey}
        await response.end(json.dumps(newresult))
    except Exception as e:
        newresult = {"statuscode": 1, "error_message": "failed", "accesskey": ""}
        await response.end(json.dumps(newresult))     
using System.Web.Script.Serialization;
using JWT.Algorithms;
using JWT.Serializers;
using JWT.Builder;
using JWT;

public string GetToken(string sessionid, string channelName, object conn_info = null) {
    try {
        IJwtAlgorithm algorithm;
        IJsonSerializer serializer;
        IBase64UrlEncoder urlEncoder;
        IJwtEncoder encoder;
        //*****************************************************************************************************
        algorithm = new HMACSHA256Algorithm(); // symmetric
        serializer = new JsonNetSerializer();
        urlEncoder = new JwtBase64UrlEncoder();
        encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

        //*****************************************************************************************************
        Dictionary<String, object> payload = new Dictionary<string, object>();
        payload.Add("sid", sessionid);
        payload.Add("cname", channelName);
        payload.Add("pub", true);
        if (conn_info == null) {
            payload.Add("coninfo", new new Dictionary<string, object>());
        } else {
            payload.Add("coninfo", conn_info);
        }
        string secret = '<YOUR CLIENT APPLICATION SECRET>';
        var token = encoder.Encode(payload, secret);

        Dictionary<string, object> result = new Dictionary<string, object>();
        result.Add("statuscode" , 0);
        result.Add("accesskey" , token);
        result.Add("error_message" , "");

        JavaScriptSerializer jsout = new JavaScriptSerializer();
        return jsout.Serialize(result);

    } catch (Exception ex) {
        Console.WriteLine(ex);
        return  "";
    }
}