Skip to content

Instantly share code, notes, and snippets.

@abstractj
Created July 4, 2013 00:33
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 abstractj/c0b930da58b3b191d653 to your computer and use it in GitHub Desktop.
Save abstractj/c0b930da58b3b191d653 to your computer and use it in GitHub Desktop.
Hawk client request
{
"name": "hawk-client-demo",
"version": "0.1.0",
"dependencies": {
"hawk": ">= 0.0.0",
"request": ">= 0.0.0"
}
}
var Http = require('http');
var Request = require('request');
var Hawk = require('hawk');
// Declare internals
var internals = {
credentials: {
dh37fgj492je: {
id: 'dh37fgj492je', // Required by Hawk.client.header
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
}
};
var server = "http://localhost:8080/aerogear-controller-demo/autobots";
var credentialsFunc = function (id, callback) {
return callback(null, internals.credentials[id]);
};
// Send unauthenticated request
Request(server, function (error, response, body) {
console.log("============================ You can't connect here, you're lame =================================");
console.log("Status code: \n" + response.statusCode);
console.log("Body: \n" + body);
});
// Send authenticated request
credentialsFunc(internals.credentials.dh37fgj492je.id, function (err, credentials) {
var header = Hawk.client.header(server, 'GET', { credentials: credentials });
console.log("Header: " + header.field);
var options = {
uri: server,
method: 'GET',
headers: {
authorization: header.field
}
};
Request(options, function (error, response, body) {
console.log("============================ Welcome home bro =================================");
console.log("Status code: \n" + response.statusCode);
console.log("Body: \n" + body);
process.exit(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment