This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin_google.vfs.credentials.js
342 lines (295 loc) · 10.8 KB
/
plugin_google.vfs.credentials.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2016 Cloud9 IDE, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
define(function(require, exports, module) {
main.consumes = [
"Plugin", "c9", "ext",
"google.cloud",
];
main.provides = ["google.vfs.credentials"];
return main;
function main(options, imports, register) {
//
// Imports
//
var Plugin = imports["Plugin"];
var c9 = imports["c9"];
var ext = imports["ext"];
var googlecloud = imports["google.cloud"];
//
// Local variables
//
var remote;
//
// Plugin declaration
//
var plugin = new Plugin("Cloud9", main.consumes);
var emit = plugin.getEmitter();
function debug() {
if (1 || c9.debug) console.info.apply(console, arguments);
}
function noop() {
if (1 || c9.debug) console.info.apply(console, arguments);
}
function load() {
if (c9.connected) {
plugin.loadVfsExtension(function(err) {
if (err) console.error(err.stack || err);
plugin.loadCredentials();
});
}
c9.on("connect", function() {
plugin.loadVfsExtension(function(err) {
if (err) console.error(err.stack || err);
plugin.loadCredentials();
});
}, plugin);
c9.on("disconnect", function() {
// remote = null;
}, plugin);
}
function unload() {
remote = null;
}
plugin.on("load", load);
plugin.on("unload", unload);
//
// Helpers
//
/**
* Bind the given method and parameters to the connected VFS extension.
*
* The resulting wrapper function will forward calls to the VFS proxy
* object and back.
*
* The last arguments must be a callback.
*
* @param {String} methodName
* the name of the method to call on the VFS extension
*
* @param {String[]} argsDef
* a list of arguments required for the VFS extension
*
* @return {Function}
* the wrapper function
*/
function bindRemote(methodName, argsDef) {
if (argsDef == null) {
argsDef = ["callback"];
}
return function() {
if (argsDef.length > 1
&& arguments.length !== argsDef.length
&& arguments.length !== argsDef.length - 1
) {
throw new TypeError("Expected arguments: " + argsDef.join(", "));
}
var args = new Array(argsDef.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if (typeof args[args.length - 1] !== "function") {
args[args.length - 1] = noop;
}
var callback = args[args.length - 1];
if (!remote) {
var error = new Error("Extension not loaded");
return callback(error);
}
var callbackWrapper = function(err, meta) {
if (meta && meta.arguments !== undefined
&& Object.keys(meta).length === 1
) {
callback.apply(null, [err].concat(meta.arguments));
}
else {
callback(err, meta);
}
}
args[args.length - 1] = callbackWrapper;
remote[methodName].apply(remote, args);
};
}
//
// Public API declaration
//
/**
* Manages the VFS extension for the `gcloud` credential service.
*
* The credential service is based on the developer shell environment.
* It listens on a well known port and forwards email, project ID, and
* access token to the `gcloud` CLI utility when connected.
*
* To set the credentials manually, call `#setCredentials()`.
*
* Call `#loadCredentials()` to synchronize the credentials returned by
* `google.cloud#getCredentials()`.
*
* ```
* $ nc localhost 33939
* 2
* []
* 110
* ["[email protected]","other-project-1159","ya29.ZwJ000000000000000000000000000000000"]
* ```
*/
plugin.freezePublicAPI({
_events: [
],
/**
* Load the VFS extension and start listening.
*
* If the extension is already loaded, the previous instance is
* reused automatically.
*
* @param {Function} callback
* @param {Error=} callback.err
* @param {Object} callback.remote the remote API proxy object
*/
loadVfsExtension: function(callback) {
callback = callback || noop;
if (remote) {
return callback(null, remote);
}
ext.loadRemotePlugin("google.vfs.credentials", {
code: require("text!./ext_google.vfs.credentials.js"),
packageDir: options.packageDir,
}, function(err, remote_) {
remote = remote_;
if (!err) {
// start listening, if ext was freshly loaded
remote.listen(function(err) {
callback(err, remote);
});
}
else if (err.code === "EEXIST") {
// ignore "Extension API already defined for ext" warning
callback(null, remote);
}
else {
callback(err);
}
});
},
/**
* Unload the VFS extension and stop listening.
*
* @param {Function} callback
* @param {Error=} callback.err
*/
unloadVfsExtension: function(callback) {
callback = callback || noop;
if (!remote) {
var error = new Error("Extension not loaded");
return callback(error);
}
plugin.close(function(err) {
if (err) return callback(err);
ext.unloadRemotePlugin("google.vfs.credentials", {}, function(err) {
remote = null;
callback(err);
});
});
},
/**
* Fetch the credentials from `google.cloud#getCredentials()` and
* update the credential service.
*
* @param {Function} callback
* @param {Error=} callback.err
*/
loadCredentials: function(callback) {
callback = callback || noop;
googlecloud.getCredentials(function(err, credentials) {
if (err) {
return callback(err);
}
plugin.setCredentials(
credentials.email,
credentials.projectId,
credentials.accessToken,
callback
);
});
},
/**
* Start listening for incoming connections.
*
* @param {Function} callback
* @param {Error=} callback.err
* @param {Number} callback.port
* the port that the server is bound to.
* @param {String} callback.host
* the hostname or IP address that the server is bound to.
*/
listen: bindRemote("listen", [
"callback"
]),
/**
* Stop listening for incoming connections.
*
* @param {Function} callback
* @param {Error=} callback.err
*/
close: bindRemote("close", [
"callback"
]),
/**
* Set the credentials forwarded by the service.
*
* @param {String} userEmail
* the account email address
*
* @param {String} projectId
* the project ID
*
* @param {String} accessToken
* the OAuth 2.0 access token
*
* @param {Function} callback
* @param {Error=} callback.err
*/
setCredentials: bindRemote("setCredentials", [
"userEmail", "projectId", "accessToken", "callback"
]),
/**
* Check if the service has known credentials.
*
* @param {Function} callback
* @param {Error=} callback.err
* @param {boolean} callback.hasCredentials
*/
hasCredentials: bindRemote("hasCredentials", [
"callback"
]),
/**
* Clear credentials known by the service.
*
* @param {Function} callback
* @param {Error=} callback.err
*/
clearCredentials: bindRemote("clearCredentials", [
"callback"
]),
});
register(null, {
"google.vfs.credentials": plugin
});
}
});