Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.