-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
194 lines (135 loc) · 5.35 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
192
193
194
var request = require('request');
var url = require('url');
var Q = require('q');
var debug = require('debug')('node-auth-sdk');
var ClientToken = require('./lib/clientToken');
var UserToken = require('./lib/userToken');
var FtvenNodeAuthSdk = function(apiUrl, clientId, userId, userPwd) {
'use strict';
var clientToken = new ClientToken(apiUrl, clientId);
var userToken = new UserToken(apiUrl, clientToken, userId, userPwd);
// Make an API call
this.call = function(verb, uri, data, headers, options) {
return mergeOptions(verb, uri, data, headers, options)
.then(addClientTokenHeader)
.then(addUserTokenHeader)
.then(sendRequest);
};
// Retrieve a Client Token for your application
this.getClientToken = function() {
var deferred = Q.defer();
debug('Getting client token');
if (!clientToken.hasToken() || !clientToken.isExpiredToken()) {
debug('Client token null or expired');
clientToken.requestToken().then(function() {
debug('New client token seems good');
deferred.resolve(clientToken.getObject());
}).fail(function(err) {
debug('Error while retrieving client token');
debug(err);
deferred.reject(err);
});
} else {
debug('Client token is already good');
deferred.resolve(clientToken.getObject());
}
return deferred.promise;
};
// Retrive a User token for an user of your application
this.getUserToken = function() {
var deferred = Q.defer();
debug('Getting user token');
this.getClientToken()
.then(function() {
if (!userToken.hasToken() || !userToken.isExpiredToken()) {
debug('User token null or expired');
userToken.requestToken().then(function() {
debug('New user token seems good');
deferred.resolve(userToken.getObject());
}).fail(function(err) {
debug('Error while retrieving user token');
debug(err);
deferred.reject(err);
});
} else {
debug('User token is already good');
deferred.resolve(userToken.getObject());
}
});
return deferred.promise;
};
function mergeOptions(verb, uri, data, headers, options) {
var deferred = Q.defer();
debug('Merging options');
try {
options = options || {};
options.url = url.resolve(apiUrl, uri);
options.method = verb;
if (options.method === 'POST' || options.method === 'PATCH' || options.method === 'PUT') {
options.body = (data) ? JSON.stringify(data) : data;
}
options.headers = headers || {};
debug('Merged options:');
debug(options);
deferred.resolve(options);
} catch(err) {
debug('Error while merging options');
debug(err);
deferred.reject(err);
}
return deferred.promise;
}
function addClientTokenHeader(options) {
debug('Checking client token');
if (!clientToken.hasToken() || !clientToken.isExpiredToken()) {
debug('Client token null or expired');
return clientToken.requestToken().then(function() {
debug('New client token seems good');
options.headers[clientToken.getKey()] = clientToken.getValue();
return options;
});
} else {
var deferred = Q.defer();
options.headers[clientToken.getKey()] = clientToken.getValue();
deferred.resolve(options);
return deferred.promise;
}
}
function addUserTokenHeader(options) {
if (!userToken.hasToken() || !userToken.isExpiredToken()) {
return userToken.requestToken().then(function() {
options.headers[userToken.getKey()] = userToken.getValue();
return options;
});
} else {
var deferred = Q.defer();
options.headers[userToken.getKey()] = userToken.getValue();
deferred.resolve(options);
return deferred.promise;
}
}
function sendRequest(options) {
var deferred = Q.defer();
debug('Sending the request');
debug(options);
request(options, function(err, httpResponse, body) {
if (err) {
debug('Request failed');
debug(err);
deferred.reject(err);
} else {
try {
debug('Success');
debug(body);
deferred.resolve(JSON.parse(body));
} catch(e) {
debug('Request failed');
debug(e);
deferred.reject('Error request response is not a valid JSON format');
}
}
});
return deferred.promise;
}
};
module.exports = FtvenNodeAuthSdk;