-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
134 lines (113 loc) · 2.75 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
var request = require('request'),
q = require('yapl');
module.exports = EXPA;
function EXPA(username, password, enforceSSL){
"use strict";
var r = request.defaults({
rejectUnauthorized: typeof enforceSSL !== 'boolean' ? true : enforceSSL,
jar: true,
followAllRedirects: true,
qsStringifyOptions: {arrayFormat: 'brackets'}
});
var _ = {},
_baseUrl = 'https://gis-api.aiesec.org/v2',
_token;
var tokenRequest = function(){
var deferred = q();
r.get('https://experience.aiesec.org/auth', (error, response, body) => {
var match = body.match('<meta.*content="(.*)".*name="csrf-token"');
r.post({
url: 'https://auth.aiesec.org/users/sign_in',
form: {
"user[email]": username,
"user[password]": password,
"authenticity_token": match[1],
"commit": 'Sign in'
}
}, function(error, response, body){
if(error) {
deferred.reject(error);
} else {
if(body.indexOf('<h2>Invalid email or password.') > -1){
deferred.reject('Invalid email or password');
} else {
response.body = body;
deferred.resolve(response);
}
}
});
});
return deferred;
};
_.getNewToken = function() {
return tokenRequest().then((response) => {
var cookie = response.req._headers.cookie;
var token = cookie.match('expa_token=(.*)')[1].replace(/;.*/, '');
_token = token;
return token;
});
};
_.getToken = function(){
if(_token) {
return q().resolve(_token);
} else {
return _.getNewToken();
}
};
_.request = function(uri, options){
var deferred = q();
var params = {};
if (typeof options === 'object') {
Object.assign(params, options, {uri: uri});
} else if (typeof uri === 'string') {
Object.assign(params, {uri: uri});
} else {
Object.assign(params, uri);
}
if(params.uri.indexOf('http') < 0) params.baseUrl = _baseUrl;
params.callback = function(err, resp, body){
if(err) {
deferred.reject(err);
} else {
try{
var json = JSON.parse(body);
if(json.status && json.status.code == 401) {
//if token expired, get new one and retry
_.getNewToken().then(function(){
_.request(uri, options)
.then(deferred.resolve)
.catch(deferred.reject);
});
} else {
deferred.resolve(json);
}
} catch(e) {
deferred.resolve(body);
}
}
};
_.getToken().then(function(token){
params.uri += `?access_token=${token}`;
r(params);
}, deferred.reject);
return deferred;
};
_.get = function(url, data){
return _.request(url, {
form: data
});
};
_.post = function(url, data){
return _.request(url, {
method: "POST",
form: data
});
};
_.patch = function(url, data){
return _.request(url, {
method: "PATCH",
form: data
});
};
return _;
}