Giter Site home page Giter Site logo

How to test RtcTokenBuilder? about tools HOT 5 CLOSED

agoraio avatar agoraio commented on July 16, 2024
How to test RtcTokenBuilder?

from tools.

Comments (5)

plutoless avatar plutoless commented on July 16, 2024

unlike jwt token. you cannot parse a token back to readable information. is there any reason you want to do that?

from tools.

nilsreichardt avatar nilsreichardt commented on July 16, 2024

Yes, I want to write a test similar I showed in the description. I want to call a method with my parameters and this method returns a token. After that I want to check if in this token are my passed parameters, like uid, role, channelName, etc.

from tools.

plutoless avatar plutoless commented on July 16, 2024

@nilsreichardt unfortunately you can't do that. we compare your token by generating a token using same algorithm and parameters and compare them. we don't convert it back to raw info either. As long as you are using our original algorithm and correct parameter, the token generated should be correct.

from tools.

0xdevalias avatar 0xdevalias commented on July 16, 2024

@nilsreichardt I haven't verified this when running, but looking at the source..

We can see the source for buildTokenWithAccount here:

  • static buildTokenWithAccount(appID, appCertificate, channelName, account, role, privilegeExpiredTs) {
    this.key = new AccessToken(appID, appCertificate, channelName, account)
    this.key.addPriviledge(Priviledges.kJoinChannel, privilegeExpiredTs)
    if (role == Role.ATTENDEE ||
    role == Role.PUBLISHER ||
    role == Role.ADMIN) {
    this.key.addPriviledge(Priviledges.kPublishAudioStream, privilegeExpiredTs)
    this.key.addPriviledge(Priviledges.kPublishVideoStream, privilegeExpiredTs)
    this.key.addPriviledge(Priviledges.kPublishDataStream, privilegeExpiredTs)
    }
    return this.key.build();
    }

It uses AccessToken, which is defined at:

You can see the build function within this, that returns version + token.appID + content.toString('base64'):

  • this.build = function () {
    var m = Message({
    salt: token.salt
    , ts: token.ts
    , messages: token.messages
    }).pack();
    var toSign = Buffer.concat(
    [Buffer.from(token.appID, 'utf8'),
    Buffer.from(token.channelName, 'utf8'),
    Buffer.from(token.uid, 'utf8'),
    m]);
    var signature = encodeHMac(token.appCertificate, toSign);
    var crc_channel = UINT32(crc32.str(token.channelName)).and(UINT32(0xffffffff)).toNumber();
    var crc_uid = UINT32(crc32.str(token.uid)).and(UINT32(0xffffffff)).toNumber();
    var content = AccessTokenContent({
    signature: signature,
    crc_channel: crc_channel,
    crc_uid: crc_uid,
    m: m
    }).pack();
    return (version + token.appID + content.toString('base64'));
    }

There is also the fromString function defined in that same file:

  • this.fromString = function (originToken) {
    try {
    originVersion = originToken.substr(0, VERSION_LENGTH);
    if (originVersion != version) {
    return false;
    }
    var originAppID = originToken.substr(VERSION_LENGTH, (VERSION_LENGTH + APP_ID_LENGTH));
    var originContent = originToken.substr((VERSION_LENGTH + APP_ID_LENGTH));
    var originContentDecodedBuf = Buffer.from(originContent, 'base64');
    var content = unPackContent(originContentDecodedBuf);
    this.signature = content.signature;
    this.crc_channel_name = content.crc_channel_name;
    this.crc_uid = content.crc_uid;
    this.m = content.m;
    var msgs = unPackMessages(this.m);
    this.salt = msgs.salt;
    this.ts = msgs.ts;
    this.messages = msgs.messages;
    } catch (err) {
    console.log(err);
    return false;
    }
    return true;
    };

Based on that, it looks like you should be able to recover some, if not most/all of the parameters you want, and thus be able to write some tests for them.

Let me know how you go with this, would be interested to hear!

from tools.

 avatar commented on July 16, 2024

@plutoless is there documentation or can you clarify the details on the tokenbuilder. I am specifically looking for the difference between the below two methods. C# example..

What is this property does this._salt = salt;?

 public AccessToken(string appId, string appCertificate, string channelName, string uid)
        {
            _appId = appId;
            _appCertificate = appCertificate;
            _channelName = channelName;
            _uid = uid;
        }

        public AccessToken(string appId, string appCertificate, string channelName, string uid, uint ts, uint salt)
        {
            this._appId = appId;
            this._appCertificate = appCertificate;
            this._channelName = channelName;
            this._uid = uid;
            this._ts = ts;
            this._salt = salt;
        }

from tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.