Skip to content

Instantly share code, notes, and snippets.

@arsdehnel
Last active May 17, 2017 03:46
Show Gist options
  • Save arsdehnel/c23ef48b0cbc93c584b7fda536297541 to your computer and use it in GitHub Desktop.
Save arsdehnel/c23ef48b0cbc93c584b7fda536297541 to your computer and use it in GitHub Desktop.
trying to work through a kong hmac implementation
const crypto = require( 'crypto' );
const hostname = 'localhost';
const proxyPort = '8000';
const http = require( 'http' );
const requestPath = '/v1.0/recognition/public-wall';
const hmacHeaders = ( username, secret ) => {
const date = new Date().toUTCString();
const stringToSign = 'date: ' + date;
const encodedSignature = crypto.createHmac( 'sha1', secret ).update( stringToSign ).digest( 'base64' );
const hmacAuth = 'hmac username="' + username + '",algorithm="hmac-sha1",headers="date",signature="' + encodedSignature + '"';
const headers = {
'date': date,
'Authorization': hmacAuth
};
return headers;
};
const headers = hmacHeaders( 'bob', 'secret456' );
const options = {
host: hostname,
port: proxyPort,
path: requestPath,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'date': headers.date,
'Authorization': headers.Authorization
}
};
const req = http.request( options, function( res ) {
console.log( res.statusCode );
res.setEncoding( 'utf8' );
res.on( 'data', function( d ) {
console.log( d );
} );
} );
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment