lib/authorizer/digest.js
/**
 * Calculates body hash with given algorithm and digestEncoding.
 *
 * @param {RequestBody} body Request body
 * @param {String} algorithm Hash algorithm to use
 * @param {String} digestEncoding Encoding of the hash
 * @param {Function} callback Callback function that will be called with body hash
 */
Calculates body hash with given algorithm and digestEncoding.
@param {RequestBody} body Request body
@param {String} algorithm Hash algorithm to use
@param {String} digestEncoding Encoding of the hash
@param {Function} callback Callback function that will be called with body hash
Calculates body hash with given algorithm and digestEncoding . @param { RequestBody } body Request body @param { String } algorithm Hash algorithm to use @param { String } digestEncoding Encoding of the hash @param { Function } callback Callback function that will be called with body hash
function computeBodyHash (body, algorithm, digestEncoding, callback) {
    if (!(algorithm && digestEncoding)) { return callback(); }
    var hash = crypto.createHash(algorithm),
        originalReadStream,
        rawBody,
        graphqlBody,
        urlencodedBody;
    if (!body || body.isEmpty()) {
        return callback(hash.digest(digestEncoding));
    }
    if (body.mode === RequestBody.MODES.raw) {
        rawBody = bodyBuilder.raw(body.raw).body;
        hash.update(rawBody);
        return callback(hash.digest(digestEncoding));
    }
    if (body.mode === RequestBody.MODES.urlencoded) {
        urlencodedBody = bodyBuilder.urlencoded(body.urlencoded).form;
        urlencodedBody = urlEncoder.encodeQueryString(urlencodedBody);
        hash.update(urlencodedBody);
        return callback(hash.digest(digestEncoding));
    }
    if (body.mode === RequestBody.MODES.file) {
        originalReadStream = _.get(body, 'file.content');
        if (!originalReadStream) {
            return callback();
        }
        return originalReadStream.cloneReadStream(function (err, clonedStream) {
            if (err) { return callback(); }
            clonedStream.on('data', function (chunk) {
                hash.update(chunk);
            });
            clonedStream.on('end', function () {
                callback(hash.digest(digestEncoding));
            });
        });
    }
    if (body.mode === RequestBody.MODES.graphql) {
        graphqlBody = bodyBuilder.graphql(body.graphql).body;
        hash.update(graphqlBody);
        return callback(hash.digest(digestEncoding));
    }
    return callback();
}
function computeBodyHash ( body , algorithm , digestEncoding , callback ) { if ( ! ( algorithm && digestEncoding ) ) { return callback ( ) ; } var hash = crypto . createHash ( algorithm ) , originalReadStream , rawBody , graphqlBody , urlencodedBody ; if ( ! body || body . isEmpty ( ) ) { return callback ( hash . digest ( digestEncoding ) ) ; } if ( body . mode === RequestBody . MODES . raw ) { rawBody = bodyBuilder . raw ( body . raw ) . body ; hash . update ( rawBody ) ; return callback ( hash . digest ( digestEncoding ) ) ; } if ( body . mode === RequestBody . MODES . urlencoded ) { urlencodedBody = bodyBuilder . urlencoded ( body . urlencoded ) . form ; urlencodedBody = urlEncoder . encodeQueryString ( urlencodedBody ) ; hash . update ( urlencodedBody ) ; return callback ( hash . digest ( digestEncoding ) ) ; } if ( body . mode === RequestBody . MODES . file ) { originalReadStream = _ . get ( body , 'file.content' ) ; if ( ! originalReadStream ) { return callback ( ) ; } return originalReadStream . cloneReadStream ( function ( err , clonedStream ) { if ( err ) { return callback ( ) ; } clonedStream . on ( 'data' , function ( chunk ) { hash . update ( chunk ) ; } ) ; clonedStream . on ( 'end' , function ( ) { callback ( hash . digest ( digestEncoding ) ) ; } ) ; } ) ; } if ( body . mode === RequestBody . MODES . graphql ) { graphqlBody = bodyBuilder . graphql ( body . graphql ) . body ; hash . update ( graphqlBody ) ; return callback ( hash . digest ( digestEncoding ) ) ; } return callback ( ) ; }
Calculates body hash with given algorithm and digestEncoding.
Calculates body hash with given algorithm and digestEncoding .
// if body is not available, return hash of empty string // @todo: Figure out a way to calculate hash for formdata body type. // ensure that callback is called if body.mode doesn't match with any of the above modes