forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.js
More file actions
316 lines (272 loc) · 9.54 KB
/
security.js
File metadata and controls
316 lines (272 loc) · 9.54 KB
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
'use strict';
var fs = require('fs');
var path = require('path');
var Q = require('q');
var expandTilde = require('expand-tilde');
var _ = require('underscore');
var extend = require('../utils/extend');
var IonicAppLib = require('ionic-app-lib');
var fail = IonicAppLib.utils.fail;
var Login = IonicAppLib.login;
var Security = IonicAppLib.security;
var log = IonicAppLib.logging.logger;
var LoginTask = require('./login');
var Prompt = require('../utils/prompt');
var Table = require('../utils/table');
var chalk = require('chalk');
var Project = IonicAppLib.project;
var settings = {
title: 'security',
name: 'security',
summary: 'Store your app\'s credentials for the Ionic Cloud',
args: {
'<command>': chalk.yellow('profiles list') + ', ' + chalk.yellow('profiles add "<name>"') + ', ' +
chalk.yellow('credentials android') + ', or ' + chalk.yellow('credentials ios'),
'[options]': ''
},
options: {
'--profile <tag>': '(' + chalk.yellow('credentials <platform>') +
') Specify the profile on which these credentials are saved',
'--keystore|-s <path>': '(' + chalk.yellow('credentials android') +
') Specify the location of your keystore file',
'--keystore-password|-p <password>': '(' + chalk.yellow('credentials android') +
') Specify your keystore password (exclude for prompt)',
'--key-alias|-k <alias>': '(' + chalk.yellow('credentials android') +
') Specify your key alias for this app',
'--key-password|-w <password>': '(' + chalk.yellow('credentials android') +
') Specify your key password for this app (exclude for prompt)',
'--cert|-c <path>': '(' + chalk.yellow('credentials ios') +
') Specify the location of your .p12 file',
'--cert-password|-p <password>': '(' + chalk.yellow('credentials ios') +
') Specify your certificate password (exclude for prompt)',
'--provisioning-profile|-r <path>': '(' + chalk.yellow('credentials ios') +
') Specify the location of your .mobileprovision file'
},
isProjectTask: true
};
function run(ionic, argv) {
var cmd;
var subcmd;
if (argv._.length < 2) {
cmd = 'profiles';
} else {
cmd = argv._[1];
}
var dir = null;
var project = null;
var appId = null;
try {
dir = process.cwd();
project = Project.load(dir);
appId = project.get('app_id');
if (!appId) {
throw new Error('Missing Ionic App ID.');
}
} catch (ex) {
return fail(ex, 'security');
}
switch (cmd) {
case 'profiles':
if (argv._.length < 3) {
subcmd = 'list';
} else {
subcmd = argv._[2];
}
switch (subcmd) {
case 'add':
return addSecurityProfile(ionic, argv, dir, appId);
case 'list':
return listSecurityProfiles(ionic, argv, dir, appId);
default:
return fail("Unknown subcommand 'profiles " + subcmd + "'.", 'security');
}
case 'credentials':
return addSecurityCredentials(ionic, argv, dir, appId);
}
return fail("Unknown subcommand '" + cmd + "'.", 'security');
}
function addSecurityProfile(ionic, argv, dir, appId) {
if (argv._.length < 4) {
return fail('Specify a name for this Security Profile. Example: ' +
"'ionic security profile add \"My New Profile\"'.", 'security');
}
var jar;
var name = argv._[3];
return Login.retrieveLogin()
.then(function(jar) {
if (!jar) {
log.info('No previous login existed. Attempting to log in now.');
return LoginTask.login(argv);
}
return jar;
})
.then(function(j) {
jar = j;
return Security.addProfile(appId, jar, name);
})
.then(function() {
console.success('Added "' + name + '".');
}, function(err) {
if (typeof err === 'object') {
if (err.type === 'ProfileExists') {
console.error(err.message);
} else {
return Q.reject(new Error(err.message));
}
}
})
.catch(function(ex) {
fail(ex, 'package');
});
}
function listSecurityProfiles(ionic, argv, dir, appId) {
var jar;
return Login.retrieveLogin()
.then(function(jar) {
if (!jar) {
log.info('No previous login existed. Attempting to log in now.');
return LoginTask.login(argv);
}
return jar;
})
.then(function(j) {
jar = j;
return Security.listProfiles(appId, jar);
})
.then(function(body) {
if (body.data.length === 0) {
log.info('You don\'t have any Security Profiles yet!');
log.info('Type ' + chalk.yellow('ionic help security') + ' to learn how to use Security Profiles.');
} else {
var table = new Table({ head: ['name', 'tag', 'android', 'ios'] });
_.each(body.data, function(profile) {
table.push([
profile.name,
profile.tag,
typeof profile.credentials.android === 'undefined' ? chalk.red.bold('✗') : chalk.green.bold('✓'),
typeof profile.credentials.ios === 'undefined' ? chalk.red.bold('✗') : chalk.green.bold('✓')
]);
});
log.info('');
log.info(table.toString());
log.info('');
}
})
.catch(function(ex) {
fail(ex, 'security');
});
}
function addSecurityCredentials(ionic, argv, dir, appId) {
if (argv._.length < 3) {
return fail('Specify a valid platform (android or ios).', 'security');
}
if (typeof argv.profile === 'undefined') {
return fail('Specify the Security Profile on which these credentials are saved ' +
'(--profile <tag>).', 'security');
}
var jar;
var platform = argv._[2];
var profile = argv.profile;
if (!_.contains(['android', 'ios'], platform)) {
return fail("Invalid platform '" + platform + "', please choose either 'android' or 'ios'.", 'security');
}
return Login.retrieveLogin()
.then(function(jar) {
if (!jar) {
log.info('No previous login existed. Attempting to log in now.');
return LoginTask.login(argv);
}
return jar;
})
.then(function(j) {
var q = Q.defer();
jar = j;
if (platform === 'android') {
if (typeof argv.s !== 'undefined') {
argv['keystore'] = argv.s;
}
if (typeof argv.p !== 'undefined') {
argv['keystore-password'] = argv.p;
}
if (typeof argv.k !== 'undefined') {
argv['key-alias'] = argv.k;
}
if (typeof argv.w !== 'undefined') {
argv['key-password'] = argv.w;
}
Prompt.prompt([
{ name: 'keystore', description: 'Keystore File:'.prompt, required: true },
{ name: 'keystore-password', description: 'Keystore Password:'.prompt, hidden: true, required: true },
{ name: 'key-alias', description: 'Key Alias:'.prompt, required: true },
{ name: 'key-password', description: 'Key Password:'.prompt, hidden: true, required: true }
], argv)
.then(function(result) {
q.resolve(result);
})
.catch(function(ex) {
q.reject(ex);
});
} else if (platform === 'ios') {
if (typeof argv.c !== 'undefined') {
argv['cert'] = argv.c;
}
if (typeof argv.p !== 'undefined') {
argv['cert-password'] = argv.p;
}
if (typeof argv.r !== 'undefined') {
argv['provisioning-profile'] = argv.r;
}
Prompt.prompt([
{ name: 'cert', description: 'Certificate File:'.prompt, required: true },
{ name: 'cert-password', description: 'Certificate Password:'.prompt, hidden: true, required: true },
{ name: 'provisioning-profile', description: 'Provisioning Profile:'.prompt, required: true }
], argv)
.then(function(result) {
q.resolve(result);
})
.catch(function(ex) {
q.reject(ex);
});
} else {
q.reject('Unrecognized platform: ' + platform);
}
return q.promise;
})
.then(function(result) {
if (platform === 'android') {
var keystoreFilePath = path.resolve(expandTilde(result['keystore']));
var keystorePassword = result['keystore-password'];
var keyAlias = result['key-alias'];
var keyPassword = result['key-password'];
var keystoreFileStream = fs.createReadStream(keystoreFilePath);
return Security.addAndroidCredentials(appId, jar, profile, keystoreFileStream,
keystorePassword, keyAlias, keyPassword);
} else if (platform === 'ios') {
var certificateFilePath = path.resolve(expandTilde(result['cert']));
var certificatePassword = result['cert-password'];
var provisioningProfileFilePath = path.resolve(expandTilde(result['provisioning-profile']));
var certificateFileStream = fs.createReadStream(certificateFilePath);
var provisioningProfileFileStream = fs.createReadStream(provisioningProfileFilePath);
return Security.addIOSCredentials(appId, jar, profile, certificateFileStream,
certificatePassword, provisioningProfileFileStream);
}
return Q.reject('Unrecognized platform.');
})
.then(function() {
console.success('Added ' + platform + ' credentials to your Security Profile.');
}, function(err) {
if (typeof err === 'object') {
if (err.type === 'CredentialsExist') {
console.error(err.message);
} else {
return Q.reject(new Error(err.message));
}
}
})
.catch(function(ex) {
fail(ex, 'package');
});
}
module.exports = extend(settings, {
run: run
});