forked from baudehlo/rainforest-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (37 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
var crypto = require('crypto');
function RainForestAuth (key, keyHash) {
this.key = key;
if (!this.key) {
this.key_hash = keyHash;
} else {
const hash = crypto.createHash('sha256');
hash.update(key);
this.key_hash = hash.digest('hex');
}
}
RainForestAuth.prototype.get_run_callback = function (run_id, callback_type) {
var digest = this.sign(callback_type, {run_id: run_id});
return "https://app.rainforestqa.com/api/1/callback/run/" + run_id + "/" + callback_type + "/" + digest;
}
RainForestAuth.prototype.sign_old = function (callback_type, options = null) {
var hmac = crypto.createHmac('sha1', this.key);
hmac.update(merge_data(callback_type, options));
return hmac.digest('hex');
}
RainForestAuth.prototype.sign = function (callback_type, options = null) {
var hmac = crypto.createHmac('sha1', this.key_hash);
hmac.update(merge_data(callback_type, options));
return hmac.digest('hex');
}
// Verify a digest vs callback_type and options
RainForestAuth.prototype.verify = function (digest, callback_type, options) {
if (!this.key) {
return digest === this.sign(callback_type, options);
}
return digest === this.sign(callback_type, options) || digest === this.sign_old(callback_type, options);
}
function merge_data (callback_type, options) {
return JSON.stringify({callback_type: callback_type, options: options});
}
module.exports = RainForestAuth;