UNPKG

skyriver

Version:

SkyRiver Streaming Cloud server-side library for NodeJS

39 lines (30 loc) 975 B
'use strict'; var util = require('./util'), config = require('./config'); function Credentials() { this.appKey =config.APP_KEY; this.appSecret = config.APP_SECRET; } Credentials.prototype.generateAccessToken = function(options, data) { var nonce = Date.now() / 1000; var sign = this._signRequest(options, data, nonce); var token = nonce + ':' + this.appKey + ':' + sign; return token; } Credentials.prototype._signRequest = function(options, body, nonce) { var contentType = options.headers['Content-Type']; var data = options.method + ' ' + options.path; data += '\nHost: ' + options.host; if (contentType) { data += '\nContent-Type: ' + contentType; } data += '\n\n'; if (body && contentType && contentType != 'application/octet-stream') { data += body; } data += nonce; var digest = util.hmacSha1(data, this.appSecret); var sageDigest = util.base64ToUrlSafe(digest); return sageDigest; } module.exports = Credentials;