-
-
Save abstractj/c0b930da58b3b191d653 to your computer and use it in GitHub Desktop.
Hawk client request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "hawk-client-demo", | |
"version": "0.1.0", | |
"dependencies": { | |
"hawk": ">= 0.0.0", | |
"request": ">= 0.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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