Created
July 25, 2013 17:39
-
-
Save cadecairos/6082023 to your computer and use it in GitHub Desktop.
Generating the Hawk Authorization header using makeapi-client
This file contains hidden or 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
/* 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