Created
November 20, 2010 04:28
-
-
Save bsstoner/707615 to your computer and use it in GitHub Desktop.
Node.js implementation of Tokbox's OpenTokSDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Node.js implementation of Tokbox's OpenTokSDK | |
* | |
* by Brian Stoner | |
* | |
* var tokbox = require('./tokbox') | |
* | |
* var t = new tokbox.OpenTokSDK(API_KEY,API_SECRET) | |
* | |
* t.createSession('127.0.0.1', {}, function(session){ | |
* var token = t.generateToken(session.sessionId) | |
* }) | |
* | |
*/ | |
// OpenTok Constants: | |
var TOKEN_SENTINEL = "T1==", | |
SDK_VERSION = "tbpy-0.90.0", | |
TOKBOX_HOST = "staging.tokbox.com", | |
SESSION_API_ENDPOINT = "/hl/session/create"; | |
var http = require('http'), | |
querystring = require('querystring'), | |
crypto = require('crypto'), | |
httpClient = http.createClient(443, TOKBOX_HOST, true); | |
// Session Properties definition: | |
var SessionProperties = exports.SessionProperties = function(){ | |
return { | |
'echoSuppression.enabled' : null, | |
'multiplexer.numOutputStreams' : null, | |
'multiplexer.switchType' : null, | |
'multiplexer.switchTimeout' : null | |
} | |
} | |
// Just the session | |
var OpenTokSession = function(sessionId){ | |
this.sessionId = sessionId | |
} | |
// The SDK | |
var OpenTokSDK = exports.OpenTokSDK = function(partnerId, partnerSecret){ | |
this.partnerId = partnerId | |
this.partnerSecret = partnerSecret | |
} | |
OpenTokSDK.prototype.generateToken = function(sessionId, permissions, expireTime){ | |
this.sessionId = sessionId || '' | |
var createTime = OpenTokSDK.prototype._getUTCDate(), | |
sig, | |
tokenString, | |
tokenParams, | |
tokenBuffer, | |
dataString, | |
dataParams = { | |
session_id:this.sessionId, | |
create_time: createTime, | |
nonce: Math.floor(Math.random()*999999) | |
}; | |
if(permissions){ | |
dataParams['permissions'] = permissions | |
} | |
if(expireTime){ | |
dataParams['expire_time'] = expireTime | |
} | |
dataString = querystring.stringify(dataParams) | |
sig = this._signString(dataString, this.partnerSecret) | |
tokenParams = ["partner_id=",this.partnerId,"&sdk_version=",SDK_VERSION,"&sig=",sig,":",dataString].join("") | |
tokenBuffer = new Buffer(tokenParams,"utf8"); | |
return TOKEN_SENTINEL + tokenBuffer.toString('base64'); | |
} | |
OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){ | |
var sessionId, | |
params = { | |
partner_id: this.partnerId, | |
location_hint: ipPassthru | |
} | |
for(var p in properties){ | |
params[p] = properties[p] | |
} | |
sessionId = this._doRequest(SESSION_API_ENDPOINT,params, function(sessionId){ | |
callback(new OpenTokSession(sessionId)) | |
}) | |
} | |
OpenTokSDK.prototype._signString = function(string, secret){ | |
var hmac = crypto.createHmac('sha1',secret) | |
hmac.update(string) | |
return hmac.digest(encoding='hex') | |
} | |
OpenTokSDK.prototype._doRequest = function(url, params, callback){ | |
var dataString = querystring.stringify(params), | |
headers = { | |
'host':TOKBOX_HOST, | |
'Content-Type': 'application-xml', | |
'Content-Length': dataString.length, | |
'X-TB-PARTNER-AUTH': this.partnerId + ":" + this.partnerSecret | |
} | |
req = httpClient.request("POST",url,headers) | |
req.write(dataString) | |
req.end() | |
req.on('response', function(res){ | |
var chunks = '' | |
res.setEncoding('utf8') | |
res.on('data', function(chunk){ | |
chunks += chunk | |
}) | |
res.on('end', function(){ | |
var start = chunks.match('<session_id>'), | |
end = chunks.match('</session_id>') | |
if(start && end){ | |
var session = chunks.substr(start.index + 12, (end.index - start.index - 12)) | |
callback(session) | |
} else { | |
callback('') | |
} | |
}) | |
}) | |
} | |
OpenTokSDK.prototype._getUTCDate = function(){ | |
var D= new Date(); | |
return Date.UTC(D.getUTCFullYear(), D.getUTCMonth(), D.getUTCDate(), D.getUTCHours(), | |
D.getUTCMinutes(), D.getUTCSeconds()).toString().substr(0,10) | |
} |
Thanks for the heads up. This was written before the newer https module that got added to node around v0.3.
I created a new version, that works with newer versions of node.js and the latest opentok API. It can be found here: https://github.com/bsstoner/opentok
It's also packaged in npm, just do: npm install opentok
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By the way, thank you very much for coding this!