Created
February 4, 2016 10:09
-
-
Save kuu/0e69466ac1f331d9dbe1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var sha256 = require('sha256'), | |
config = require('config'); | |
function serialize(params, delimiter) { | |
return Object.keys(params).sort() | |
.map(function (key) { | |
return key + '=' + (params[key] !== void 0 ? params[key] : options[key]); | |
}).join(delimiter); | |
} | |
function getPcode(apiKey) { | |
var idx = apiKey.lastIndexOf('.'); | |
if (idx === -1) { | |
return ''; | |
} | |
return apiKey.substring(0, idx); | |
} | |
exports.getPlayerTokenRequest = function(embedCode, accountId) { | |
//console.log('getPlayerTokenRequest(embedCode="' + embedCode + '", accountId="' + accountId + '")'); | |
var apiKey = config.apiKey, | |
secret = config.secret, | |
pcode = getPcode(config.apiKey), | |
path = '/sas/embed_token/' + pcode + '/' + embedCode, | |
expires = Date.now() + 86400000, | |
params = accountId === void 0 ? { | |
api_key: apiKey, | |
expires: expires | |
} : { | |
api_key: apiKey, | |
expires: expires, | |
account_id: accountId | |
}, | |
method = 'GET', hashStr, signature, token; | |
hashStr = sha256(secret + method + path + serialize(params, '')); | |
signature = new Buffer(hashStr, 'hex').toString('base64'); | |
// Remove any trailing '=' sign | |
signature = signature.replace(/=+$/, ''); | |
token = [ | |
'http://player.ooyala.com' + path, | |
[ | |
serialize(params, '&'), | |
['signature', encodeURIComponent(signature)].join('=') | |
].join('&').replace(/^&+/, '') | |
].join('?'); | |
//console.log('\ttoken=', token); | |
return token; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment