Skip to content

Instantly share code, notes, and snippets.

@cadecairos
Created July 25, 2013 17:39
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/6082023 to your computer and use it in GitHub Desktop.
Save cadecairos/6082023 to your computer and use it in GitHub Desktop.
Generating the Hawk Authorization header using makeapi-client
/* The protected paths can only be accessed by Clients running this code in a Node Environment
* See line XX for the Hawk bits
*
* To set up the Make Client for hawk authorized requests, you would do this:
*
* new Make({
* apiURL: "https://makeapi.webmaker.org,
* hawk: {
* key: "key",
* id: "ID",
* algorithm: "sha256"
* }
* });
*
*/
function nodeStrategy( type, path, data, callback ) {
// Only use auth if provided
var authObj = ( user && pass ) ? {
username: user,
password: pass,
sendImmediately: true
} : undefined,
requestOptions = {
method: type,
uri: path,
json: data,
headers: {}
},
header;
if ( authObj ) {
requestOptions.auth = authObj;
} else if( credentials ) {
/*
* If hawk credentials were provided, generate the Authorization header using Hawk
*/
header = hawk.client.header( path, type, { credentials: credentials } );
requestOptions.headers.Authorization = header.field;
}
request( requestOptions, function( err, res, body ) {
// This is where the Client would verify the server's response to prevent server spoofing - It's a TO-DO right now :)
if ( err ) {
callback( err );
return;
}
if ( res.statusCode === 200 ) {
return callback( null, body );
}
// There was an error of some sort, and the body contains the reason why
callback( body.reason );
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment