Skip to content

Instantly share code, notes, and snippets.

@cadecairos
Created July 25, 2013 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadecairos/6081426 to your computer and use it in GitHub Desktop.
Save cadecairos/6081426 to your computer and use it in GitHub Desktop.
Responding using Hawk
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
module.exports = function() {
var Hawk = require( "hawk" );
return {
Hawk: Hawk,
/*
* Code: the HTTP status code to use
* res: the response object generated by express
* creds: the credentials object to be used when calculating the MAC with Hawk
* artifacts: the parsed information from the original Authorization header,
* used by Hawk when generating the MAC for the response
* payload: the data to be sent with the response
* contentType: the contentType of the payload
*/
respond: function( code, res, creds, artifacts, payload, contentType ) {
// Set up the response headers, including contentType
var headers = { "Content-Type": contentType },
// Generate the content for the Server-Authorization header
header = Hawk.server.header( creds, artifacts, { payload: payload, contentType: contentType } );
headers[ "Server-Authorization" ] = header;
// Write the headers and payload to the response, send to the client
res.writeHead( code, headers );
res.end( payload );
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment