This repository was archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
200 lines (177 loc) · 4.56 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
/* eslint no-console:0 */
var fs = require('fs');
var path = require('path');
var del = require('del');
var glob = require('glob');
var async = require('async');
var chalk = require('chalk');
var figures = require('figures');
var debug = require('debug')('node-codesign');
var execFile = require('child_process').execFile;
function checkAppExists(opts, fn) {
debug('checking appPath `%s` exists...', opts.appPath);
fs.exists(opts.appPath, function(exists) {
if (!exists) {
debug('appPath `%s` does not exist!', opts.appPath);
return fn(new Error(opts.appPath + ' does not exist.'));
}
debug('appPath exists');
fn();
});
}
// Clean up ".cstemp" files from previous attempts
function cleanup(opts, fn) {
debug('running cleanup');
del([opts.appPath + '/*.cstemp']).then(function() {
fn();
});
}
function runCodesign(src, opts, fn) {
var args = [
'-s',
opts.identity,
'-vvv',
'--force',
'--options',
'runtime'
];
if (opts.entitlements) {
args.push('--entitlements', opts.entitlements);
}
args.push(src);
execFile('codesign', args, function(err, output) {
debug(output);
if (err) {
fn(new Error('codesign failed ' + path.basename(src)
+ '. See output above for more details. Original Error: '
+ err.message));
return;
}
fn(null, src);
});
}
function _signAll(files, opts, fn) {
async.parallel(files.map(function(src) {
return function(cb) {
debug('signing %s...', path.basename(src));
runCodesign(src, opts, cb);
};
}), function(_err, _files) {
if (_err) {
return fn(_err);
}
debug('%d files signed successfully!', _files.length);
fn(null, _files);
});
}
function _filterFiles(files, fn) {
async.parallel(files.map(function(file) {
return function(cb) {
fs.lstat(file, function(err, stat) {
if (err) {
return cb(err);
}
if (!stat.isFile()) {
return cb(null, null);
}
cb(null, file);
});
};
}), function(_err, _files) {
if (_err) {
return fn(_err);
}
fn(null, _files.filter(function(f) {
return f !== null;
}));
});
}
function _collectFiles(pattern, opts, fn) {
glob.glob(pattern, function(err, files) {
if (err) {
return fn(err);
}
if (files.length === 0) {
return fn(new Error('No files found for '
+ opts.appPath + '/' + pattern));
}
fn(null, files);
});
}
function codesign(pattern, opts, fn) {
async.waterfall([
function(cb) {
_collectFiles(pattern, opts, cb);
},
function(files, cb) {
_filterFiles(files, cb);
},
function(files, cb) {
_signAll(files, opts, cb);
}
], fn);
}
function verify(src, fn) {
debug('verifying signature on `%s`...', src);
var args = [
'--verify',
'-vvv',
src
];
execFile('codesign', args, function(err) {
if (err) {
return fn(err);
}
fn(null, src);
});
}
/**
* @param {String} commonName
* @param {Function} fn - Callback.
*/
function isIdentityAvailable(commonName, fn) {
execFile('certtool', ['y'], function(err, output) {
if (err) {
debug('Failed to list certificates.');
fn(null, false);
return;
}
if (output.indexOf(commonName) === -1) {
debug('Signing identity `%s` not detected.',
commonName);
fn(null, false);
return;
}
debug('The signing identity `%s` is available!', commonName);
fn(null, true);
});
}
module.exports = function(opts, done) {
async.series([
checkAppExists.bind(null, opts),
cleanup.bind(null, opts),
codesign.bind(null, opts.appPath, opts),
verify.bind(null, opts.appPath)
], done);
};
module.exports.isIdentityAvailable = isIdentityAvailable;
module.exports.codesign = codesign;
module.exports.verify = verify;
module.exports.printWarning = function() {
console.error(chalk.yellow.bold(figures.warning),
' User confusion ahead!');
console.error(chalk.gray(
' The default preferences for OSX Gatekeeper will not',
'allow users to run unsigned applications.'));
console.error(chalk.gray(
' However, we\'re going to continue building',
'the app and an installer because you\'re most likely'));
console.error(chalk.gray(
' a developer trying to test',
'the app\'s installation process.'));
console.error(chalk.gray(
' For more information on OSX Gatekeeper and how to change your',
'system preferences to run unsigned applications,'));
console.error(chalk.gray(' please see',
'https://support.apple.com/en-us/HT202491'));
};