/
index.js
233 lines (184 loc) · 7.43 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Module's dependencies
*/
var SF = require('node-salesforce');
var Cache = require("mem-cache");
var uuid = require("node-uuid");
var request = require('request');
var jwtflow = require('./jwtflow.js');
var Salesforce = function(settings) {
// Initialize members
var self = this;
var config = settings;
var cacheTimeout = (!settings || !settings.timeout ) ? 15 * 60 * 1000 : settings.timeout; // 15 minutes in milliseconds
var isSandbox = (config && config.isSandbox);
var cacheOptions = {timeout: cacheTimeout};
Object.defineProperty(this, "cacheAuth", {
enumerable: false,
configurable: false,
writable: false,
value: new Cache(cacheOptions)
});
var cacheUser = new Cache(cacheOptions);
this.authenticate = function (credentials, cb) {
if (!cb && typeof credentials === 'function') {
cb = credentials;
credentials = config;
}
// defaults for cb
cb = cb || function(err) { if(err) throw err; };
if (typeof cb !== 'function') return cb(new Error("'cb' argument is invalid."));
if (!credentials || typeof credentials !== 'object') return cb(new Error("'credentials' argument is invalid."));
var account;
var secret;
var sfConnection;
if (isSandbox) {
sfConnection = new SF.Connection({ loginUrl: 'https://test.salesforce.com' });
} else {
sfConnection = new SF.Connection();
}
if (credentials.useOAuthJwtFlow) {
if (!credentials.privateKey) {
cb(new Error('credentials.privateKey is required when using OAuth JWT Bearer Flow.'));
return;
};
if (!credentials.clientId) {
cb(new Error('credentials.clientId is required when using OAuth JWT Bearer Flow.'));
return;
};
if (!credentials.actAsUsername) {
cb(new Error('credentials.username is required when using OAuth JWT Bearer Flow.'));
return;
};
jwtflow(credentials.clientId, credentials.privateKey, credentials.actAsUsername, isSandbox, function(err, accessToken) {
if (err) {
cb(err);
return;
}
sfConnection = new SF.Connection();
sfConnection.initialize({
instanceUrl: 'https://' + credentials.loginHost,
accessToken: accessToken
});
cb(null, sfConnection);
return;
});
return;
};
if (credentials.oauth2 && typeof credentials.oauth2 == 'object') {
if (typeof (credentials.oauth2.clientSecret) !== 'string') return cb(new Error("'oauth2.clientSecret' property is invalid."));
if (isSandbox) {
sfConnection = new SF.Connection({ oauth2: credentials.oauth2, loginUrl: 'https://test.salesforce.com' });
} else {
sfConnection = new SF.Connection(credentials.oauth2);
}
}
// Validates user credentials
if (!credentials.username || typeof (credentials.username) !== 'string') return cb(new Error("'username' property is missing or invalid."));
if (!credentials.password || typeof (credentials.password) !== 'string') return cb(new Error("'password' property is missing or invalid."));
account = credentials.username || config.username;
secret = credentials.password || config.password;
var auth = cacheUser.get(account);
if (auth) {
var item = self.cacheAuth.get(auth);
if (item && item.password === secret) return cb(null, item.connection);
}
sfConnection.login(account, secret, function(error, userInfo) {
if (error) {
return cb(error,null);
};
var auth = uuid.v4(); // Internal auth token
var item = {
username: account,
password: secret,
connection: sfConnection
};
self.cacheAuth.set(auth, item);
cacheUser.set(account, auth);
cb(null, sfConnection);
});
};
var getSFConnection = function (options, cb) {
// defaults for credentials
var credentials = options.credentials || config;
var metadata = options._kidozen || options.metadata;
if (metadata && metadata.userClaims) {
var claimType = credentials.userNameClaimType || 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress';
credentials.actAsUsername = (metadata.userClaims.filter(function(c) {
return c.type == claimType;
})[0] || {}).value;
if (!credentials.actAsUsername) {
cb(new Error('Claim type "' + claimType + '" was not found in user claims.'));
return;
};
}
self.authenticate(credentials, cb);
};
this.Query = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
if (options.SOSQL) {
item.query(options.SOSQL, function(err, result) {
return cb(err, result);
});
};
item.sobject(options.Entity)
.find(options.Conditions, options.Fields, options.Options, function(err, result) {
return cb(err, result);
});
});
};
this.Describe = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.sobject(options.objectClass).describe(function(err, result) {
return cb(err, result);
});
});
};
this.DescribeGlobal = function(cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.describeGlobal(function(err, result) {
return cb(err, result);
});
});
};
this.Create = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.sobject(options.Entity)
.create(options.Details, function(err, result) {
return cb(err, result);
});
});
};
this.Update = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.sobject(options.Entity)
.update(options.Details, function(err, result) {
return cb(err, result);
});
});
};
this.Upsert = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.sobject(options.Entity)
.upsert(options.Details, options.ExternalIdName, function(err, result) {
return cb(err, result);
});
});
};
this.Delete = function(options, cb) {
getSFConnection(options, function(err, item) {
if (err) { return cb(err); };
item.sobject(options.Entity)
.destroy(options.Details, function(err, result) {
return cb(err, result);
});
});
};
};
module.exports = Salesforce;