-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
191 lines (191 loc) · 7.97 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
183
184
185
186
187
188
189
190
191
const request = require('request-promise-native');
const qs = require('querystring');
let token = null;
let bitrixUrl = '';
let credentials = {};
let scope = 'crm';
function isExpired(token){
return(new Date() > token.expiresAt);
}
function refreshToken(token){
if(token !== null)
{
const options = {
'url':credentials.auth.tokenHost+credentials.auth.tokenPath,
'qs':{
'refresh_token':token.refreshToken,
'client_id':credentials.client.id,
'client_secret':credentials.client.secret,
'grant_type':'refresh_token'
}
};
return new Promise(function(resolve,reject){
request(options)
.then(function(response){
try
{
let data = JSON.parse(response);
let time = new Date();
token = {
'accessToken':data.access_token,
'refreshToken':data.refresh_token,
'expiresAt':time.setSeconds(time.getSeconds() + data.expires_in)
};
resolve(token);
}
catch(err){
reject(err);
}
})
.catch(reject);
});
}
else throw new Error('Bitrix24 was not properly initialized. Token not found.');
}
exports.initialize = function(params){
return new Promise(function(resolve, reject){
if(params.url) bitrixUrl = params.url;
else return reject(new Error('Bitrix24 url not set.'));
if(params.credentials.client && params.credentials.client.id && params.credentials.client.secret &&
params.credentials.auth && params.credentials.auth.tokenHost && params.credentials.auth.tokenPath && params.credentials.auth.authorizePath)
{
credentials.client = {
'id': params.credentials.client.id,
'secret': params.credentials.client.secret
};
credentials.auth = {
'tokenHost': params.credentials.auth.tokenHost,
'tokenPath': params.credentials.auth.tokenPath,
'authorizePath': params.credentials.auth.authorizePath
};
}
else return reject('Bitrix24 credentials are not correct.');
if(params.scope) scope = params.scope;
if(params.credentials.user && params.credentials.user.login && params.credentials.user.password)
{
//Experimental - try to initialize authomatically
let authJar = request.jar();
request.post({
'url':'https://www.bitrix24.net/auth/',
'jar': authJar,
'form':{
'AUTH_FORM':'Y',
'TYPE':'AUTH',
'USER_LOGIN':params.credentials.user.login,
'USER_PASSWORD':params.credentials.user.password,
'USER_REMEMBER':'N'
},
'resolveWithFullResponse':true,
'simple':false
}).then(function(authResponse){
if(authResponse.statusCode == 302) //302 Moved is bitrix response code for successful auth, we got the PHPSESSID in the bag(or jar in this case)
{
request({
'url':bitrixUrl+'/oauth/authorize/?client_id='+credentials.client.id, //try to authorize client app, following redirect fails to get code from the main site
'jar':authJar,
'resolveWithFullResponse':true,
'simple':false,
'followRedirect':false
})
.then(function(redirectResponse){
return request({
'url':qs.parse(redirectResponse.headers.location).redirect_uri, //follow a redirect to main site manually to get auth data
'jar':authJar,
'resolveWithFullResponse':true,
'simple':false,
})
.then(function(authServiceResponse){
return request({
'url':decodeURIComponent(authServiceResponse.body.substring(30).slice(0,-10)), //auth with the site using auth data
'jar':authJar,
'resolveWithFullResponse':true,
'simple':false,
})
.then(function(){ //this is the custom user response and we don't really need it
return resolve(token); //token should be available now
});
});
})
.catch(reject);
}
else return reject(new Error('Bitrix24 authorization failed, check username/password.'));
})
.catch(reject);
}
else return resolve({'accessToken':null,'refreshToken':null,'expiresAt':null});
});
};
exports.manualAuthURL = function(req, res){
if (Object.keys(credentials).length === 0) throw new Error('Bitrix24 was not properly initialized before calling auth.');
else res.redirect(credentials.auth.tokenHost+credentials.auth.authorizePath+'?scope='+scope+'&response_type=code&client_id='+credentials.client.id);
};
exports.authenticate = function()
{
return function(req, res, next){
const options = {
'url':credentials.auth.tokenHost+credentials.auth.tokenPath,
'qs':{
'code':req.query.code,
'client_id':credentials.client.id,
'client_secret':credentials.client.secret,
'grant_type':'authorization_code'
}
};
request(options)
.then(function(response){
let data = JSON.parse(response);
let time = new Date();
token = {
'accessToken':data.access_token,
'refreshToken':data.refresh_token,
'expiresAt':time.setSeconds(time.getSeconds() + data.expires_in)
};
next();
})
.catch(function(err){
throw new Error(err);
});
};
};
exports.tokenCallback = exports.authenticate(); //reverse compatibility
exports.callMethod = function(method,params){
function prepareRequest(token)
{
if(params !== null && typeof params !== 'object') params = {};
const options = {
'url':bitrixUrl+'/rest/'+method+'?auth='+token.accessToken,
'form':params,
'resolveWithFullResponse':true,
'simple':false
};
return new Promise(function(resolve, reject){
request.post(options)
.then(function(result){
try
{
if(result.statusCode == 200)
{
let body = JSON.parse(result.body);
if(body.error) reject(new Error(body.error));
else resolve(body.result);
}
else reject(new Error(JSON.stringify({'statusCode':result.statusCode,'body':body})));
}
catch(err)
{
reject(err);
}
})
.catch(reject);
});
}
if(token === null) throw new Error('Bitrix24 was not properly initialized. Token not found.');
else if(isExpired(token))
return refreshToken(token)
.then(prepareRequest);
else return prepareRequest(token);
};
exports.getToken = function(){
if(isExpired(token)) return refreshToken(token);
else return Promise.resolve(token);
};