util-identities.js 1.6 KB

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