-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
182 lines (170 loc) · 5.26 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*!
* hackerone (https://github.com/xc0d3rz/hackerone)
* Copyright 2016-2017 xc0d3rz(x.c0d3rz000@gmail.com)
* Licensed under the MIT license
*/
(function () {
/**
*
* @param user
* @param password
* @constructor
*/
function Hackerone(user, password) {
this.authentication(user, password);
this.reports = this.list('reports');
this.programs = this.list('programs');
this.me = this.list('me');
}
/**
*
* @type {string}
*/
Hackerone.prototype.endpoint = 'https://api.hackerone.com/v1/';
/**
*
* @param user
* @param password
*/
Hackerone.prototype.authentication = function (user, password) {
this._authentication = {
user: user,
password: password
};
};
/**
*
* @param obj
* @param prefix
* @returns {string}
* @copyright https://stackoverflow.com/a/1714899
*/
Hackerone.prototype.serialize = function (obj, prefix) {
var str = [], p;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + '[' + ((isNaN(p)) ? p : '') + ']' : p, v = obj[p];
str.push((v !== null && typeof v === 'object') ?
this.serialize(v, k) :
encodeURIComponent(k) + '=' + encodeURIComponent(v));
}
}
return str.join('&');
};
/**
*
* @param path
* @param param
* @returns {string}
*/
Hackerone.prototype.url = function (path, param) {
return this.endpoint + path + '?' + this.serialize(param || {});
};
/**
*
* @param path
* @param param
* @param callback
*/
Hackerone.prototype.get = function (path, param, callback) {
var _this = this, request = require('request');
request(
{
url: _this.url(path, param),
auth: _this._authentication,
json: true
}, function (err, response, body) {
if (err && typeof callback == 'function') callback(err, null);
if (body && 'errors' in body && typeof callback == 'function') {
callback(body, null);
} else {
callback(null, body);
}
}
);
};
/**
*
* @param path
* @param param
* @param callback
*/
Hackerone.prototype.post = function (path, param, callback) {
var _this = this, request = require('request');
request.post(
{
url: _this.url(path, {}),
auth: _this._authentication,
form: param,
json: true
}, function (err, response, body) {
if (err && typeof callback == 'function') callback(err, null);
if (body && 'errors' in body && typeof callback == 'function') {
callback(body, null);
} else {
callback(null, body);
}
}
);
};
/**
*
* @param path
* @param param
* @param callback
*/
Hackerone.prototype.put = function (path, param, callback) {
var _this = this, request = require('request');
request.put(
{
url: _this.url(path, {}),
auth: _this._authentication,
form: param,
json: true
}, function (err, response, body) {
if (err && typeof callback == 'function') callback(err, null);
if (body && 'errors' in body && typeof callback == 'function') {
callback(body, null);
} else {
callback(null, body);
}
}
);
};
/**
*
* @param t
* @returns {*}
*/
Hackerone.prototype.list = function (t) {
var fs = require('fs'),
path = require('path'),
_list = {},
_this = this;
fs.readdirSync(path.join(__dirname, 'list', t)).forEach(function (file) {
if (fs.statSync(path.join(__dirname, 'list', t, file)).isDirectory()) {
var group = file;
_list[group] = {};
fs.readdirSync(path.join(__dirname, 'list', t, file)).forEach(function (_file) {
var name = _file.replace(new RegExp(path.extname(_file), 'g'), '');
var controller = require(path.join(__dirname, 'list', t, file, _file));
if (typeof controller == 'function') {
_list[group][name] = function () {
controller.apply(_this, arguments);
};
}
});
} else {
var name = file.replace(new RegExp(path.extname(file), 'g'), '');
var controller = require(path.join(__dirname, 'list', t, file));
if (typeof controller == 'function') {
_list[name] = function () {
controller.apply(_this, arguments);
};
}
}
});
return _list;
};
module.exports = Hackerone;
}.call(module.exports));