UNPKG

1.59 kBJavaScriptView Raw
1/**
2 * @module util-identities
3 */
4
5'use strict'
6
7const util = require('./util')
8const debuglog = util.debuglog
9const flatList = util.flatList
10const execFileAsync = util.execFileAsync
11
12/**
13 * @constructor
14 * @param {string} name - Name of the signing identity.
15 * @param {String} hash - SHA-1 hash of the identity.
16 */
17var Identity = module.exports.Identity = function (name, hash) {
18 this.name = name
19 this.hash = hash
20}
21
22/**
23 * This function returns a promise checking the indentity proposed and updates the identity option to a exact finding from results.
24 * @function
25 * @param {Object} opts - Options.
26 * @param {string} identity - The proposed identity.
27 * @returns {Promise} Promise.
28 */
29module.exports.findIdentitiesAsync = function (opts, identity) {
30 // Only to look for valid identities, excluding those flagged with
31 // CSSMERR_TP_CERT_EXPIRED or CSSMERR_TP_NOT_TRUSTED. Fixes #9
32
33 var args = [
34 'find-identity',
35 '-v'
36 ]
37 if (opts.keychain) {
38 args.push(opts.keychain)
39 }
40
41 return execFileAsync('security', args)
42 .then(function (result) {
43 return result.split('\n').map(function (line) {
44 if (line.indexOf(identity) >= 0) {
45 var identityFound = line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'))
46 var identityHashFound = line.substring(line.indexOf(')') + 2, line.indexOf('"') - 1)
47 debuglog('Identity:', '\n',
48 '> Name:', identityFound, '\n',
49 '> Hash:', identityHashFound)
50 return new Identity(identityFound, identityHashFound)
51 }
52 })
53 })
54 .then(flatList)
55}