@dinoboff/ims-lti
Version:
Module for building an LTI Tool Provider and accept LTI launch requests
112 lines (96 loc) • 3.19 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var HMAC_SHA1, _clean_request_body, crypto, exports, url, utils;
crypto = require('crypto');
url = require('url');
utils = require('./utils');
_clean_request_body = function(body, query) {
var cleanParams, encodeParam, out;
out = [];
encodeParam = function(key, val) {
return key + "=" + (utils.special_encode(val));
};
cleanParams = function(params) {
var i, key, len, val, vals;
if (typeof params !== 'object') {
return;
}
for (key in params) {
vals = params[key];
if (key === 'oauth_signature') {
continue;
}
if (Array.isArray(vals) === true) {
for (i = 0, len = vals.length; i < len; i++) {
val = vals[i];
out.push(encodeParam(key, val));
}
} else {
out.push(encodeParam(key, vals));
}
}
};
cleanParams(body);
cleanParams(query);
return utils.special_encode(out.sort().join('&'));
};
HMAC_SHA1 = (function() {
function HMAC_SHA1(options) {
this.trustProxy = (options && options.trustProxy) || false;
}
HMAC_SHA1.prototype.toString = function() {
return 'HMAC_SHA1';
};
HMAC_SHA1.prototype.build_signature_raw = function(req_url, parsed_url, method, params, consumer_secret, token) {
var sig;
sig = [method.toUpperCase(), utils.special_encode(req_url), _clean_request_body(params, parsed_url.query)];
return this.sign_string(sig.join('&'), consumer_secret, token);
};
HMAC_SHA1.prototype.host = function(req) {
if (!this.trustProxy) {
return req.headers.host;
}
return req.headers['x-forwarded-host'] || req.headers.host;
};
HMAC_SHA1.prototype.protocol = function(req) {
var xprotocol;
xprotocol = req.headers['x-forwarded-proto'];
if (this.trustProxy && xprotocol) {
return xprotocol;
}
if (req.protocol) {
return req.protocol;
}
if (req.connection.encrypted) {
return 'https';
} else {
return 'http';
}
};
HMAC_SHA1.prototype.build_signature = function(req, body, consumer_secret, token) {
var hapiRawReq, hitUrl, host, originalUrl, parsedUrl, protocol;
hapiRawReq = req.raw && req.raw.req;
if (hapiRawReq) {
req = hapiRawReq;
}
originalUrl = req.originalUrl || req.url;
host = this.host(req);
protocol = this.protocol(req);
if (body.tool_consumer_info_product_family_code === 'canvas') {
originalUrl = url.parse(originalUrl).pathname;
}
parsedUrl = url.parse(originalUrl, true);
hitUrl = protocol + '://' + host + parsedUrl.pathname;
return this.build_signature_raw(hitUrl, parsedUrl, req.method, body, consumer_secret, token);
};
HMAC_SHA1.prototype.sign_string = function(str, key, token) {
key = key + "&";
if (token) {
key += token;
}
return crypto.createHmac('sha1', key).update(str).digest('base64');
};
return HMAC_SHA1;
})();
exports = module.exports = HMAC_SHA1;
}).call(this);