Skip to content

Allows verification of Rainforest webhook messages using your private API key

License

Notifications You must be signed in to change notification settings

rainforestapp/rainforest-auth

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rainforest-auth

Allows verification of RainforestQA webhook signed messages using your API key, and advanced (aka long-initializing) webhooks with a callback URL. The documentation for using webhooks can be read here.

A Ruby version of this code is available at https://github.com/rainforestapp/auth

Installation

npm install rainforest-auth --save

Usage

This assumes the context is within an express app.

var http = require('http');
var RFA = require('rainforest-auth');
var auth = new RFA('YOUR_KEY_HERE');

app.post('/webhooks/rainforest', function (req, res, next) {
    if (req.body.callback_type != 'before_run') {
        return res.send(200);
    }

    if (!auth.verify(req.body.digest, req.body.callback_type, req.body.options)) {
        return res.send(403);
    }
    
    // Send our response right away, then continue on...
    res.send(202);

    var callback_url = auth.get_run_callback(req.body.run_id, req.body.callback_type);
    do_some_slow_initialization(function (err) {
        if (err) {
            // handle error
        }
        else {
            // Note the ruby example code suggests trying this up to 5 times
            // so you might want to use async.doWhilst() for that.
            http.request(callback_url, function (res) {
                // most of this is not required - just here for logging
                console.log('RAINFOREST STATUS: ' + res.statusCode);
                console.log('RAINFOREST HEADERS: ' + JSON.stringify(res.headers));
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
                  console.log('RAINFOREST BODY: ' + chunk);
                });
            })
        }
    });    
})

About

Allows verification of Rainforest webhook messages using your private API key

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%