Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Add "generate" route which returns JSON
Browse files Browse the repository at this point in the history
This will be used once we have a fancy JS front-end.
  • Loading branch information
neftaly authored and Francois Marier committed Feb 20, 2015
1 parent 276d73b commit 2079187
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
38 changes: 35 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ server.views({
path: Path.join(__dirname, 'templates')
});

/**
* Serve index.js
*/
server.route({
method: 'GET',
path: '/',
Expand All @@ -26,6 +29,9 @@ server.route({
}
});

/**
* Serve public files
*/
server.route({
method: 'GET',
path: '/{param*}',
Expand All @@ -37,16 +43,42 @@ server.route({
}
});

/**
* Return SRI lookup in JSON format
*/
server.route({
method: 'POST',
path: '/hash',
path: '/generate',
handler: function (request, reply) {
helpers.generateElement(request.payload.url, 'sha-256', function (result) {
reply(result).type('text/plain');
var options = {
url: request.payload.url,
algorithms: request.payload.algorithms
};
helpers.generate(options, function (result) {
reply(JSON.stringify(result)).type('application/json');
});
}
});


/**
* Return SRI lookup in HTML format.
* Deprecated, pending move to isomorphic app.
*/
server.route({
method: 'POST',
path: '/hash',
handler: function (request, reply) {
helpers.generateElement(
request.payload.url,
request.payload.algorithms,
function (result) {
reply(result).type('text/plain');
}
);
}
});

server.start(function () {
console.log('Server running at:', server.info.uri);
});
46 changes: 41 additions & 5 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,60 @@ var processResource = function (resourceUrl, cb) {
});
};

var generateElement = function (resourceUrl, algorithm, cb) {
fetchResource(resourceUrl, function (resource) {

/**
* Generate SRI data for a resource
*
* @param {Array} options.algorithms - List of desired hash algorithms
* @param {string} options.url - Resource URL
* @return {Object.XMLHttpRequest} fetchResource request
*/
var generate = function (options, cb) {
if (typeof options.algorithms === 'string') {
options.algorithms = [options.algorithms];
}

return fetchResource(options.url, function (resource) {
if (!resource) {
return cb('');
return cb({
'status': false,
'response': 0
});
}

var integrity = sriToolbox.generate({
algorithms: [algorithm],
algorithms: options.algorithms,
parameters: {
'ct': resource.ct
}
}, resource.data);
return cb({
'status': true,
'url': resource.url,
'content-type': resource.ct,
'integrity': integrity,
'eligibility': resource.eligibility
});
});
};

/**
* Wrap SRI data for resourceUrl in a script tag
*
* @deprecated pending move to isomorphic app
*/
var generateElement = function (resourceUrl, algorithms, cb) {
var options = {
url: resourceUrl,
algorithms: algorithms
};
return generate(options, function (resource) {
cb('<script src="' + resource.url
+ '" integrity="' + integrity + '"></script>');
+ '" integrity="' + resource.integrity + '"></script>');
});
};

exports.generateElement = generateElement;
exports.generate = generate;
exports.upgradeToHttps = upgradeToHttps;
exports.eligibility = eligibility;
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <h1>{{title}}</h1>
<h2>Generate a third-party resource</h2>
<p>Enter the URL of the sub-resource you wish to include on your site:</p>
<form action="/hash" method="POST">
<input name="algorithms" type="hidden" value="sha-256">

This comment has been minimized.

Copy link
@neftaly

neftaly Feb 25, 2015

Author Contributor

Removing the option to generate sha-512, for ease-of-use.

<input name="url" type="text" placeholder="https://example.com/script-1.0.1.js" size="50">
<input value="Hash!" type="submit">
</form>
Expand Down

0 comments on commit 2079187

Please sign in to comment.