util-entitlements.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @module util-entitlements
  3. */
  4. 'use strict'
  5. const os = require('os')
  6. const path = require('path')
  7. const plist = require('plist')
  8. const util = require('./util')
  9. const debuglog = util.debuglog
  10. const getAppContentsPath = util.getAppContentsPath
  11. const readFileAsync = util.readFileAsync
  12. const writeFileAsync = util.writeFileAsync
  13. let tmpFileCounter = 0
  14. /**
  15. * This function returns a promise completing the entitlements automation: The process includes checking in `Info.plist` for `ElectronTeamID` or setting parsed value from identity, and checking in entitlements file for `com.apple.security.application-groups` or inserting new into array. A temporary entitlements file may be created to replace the input for any changes introduced.
  16. * @function
  17. * @param {Object} opts - Options.
  18. * @returns {Promise} Promise.
  19. */
  20. module.exports.preAutoEntitlements = function (opts) {
  21. // If entitlements file not provided, default will be used. Fixes #41
  22. var appInfoPath = path.join(getAppContentsPath(opts), 'Info.plist')
  23. var appInfo
  24. var entitlements
  25. debuglog('Automating entitlement app group...', '\n',
  26. '> Info.plist:', appInfoPath, '\n',
  27. '> Entitlements:', opts.entitlements)
  28. return readFileAsync(opts.entitlements, 'utf8')
  29. .then(function (result) {
  30. entitlements = plist.parse(result)
  31. if (!entitlements['com.apple.security.app-sandbox']) {
  32. // Only automate when app sandbox enabled by user
  33. return
  34. }
  35. return readFileAsync(appInfoPath, 'utf8')
  36. .then(function (result) {
  37. appInfo = plist.parse(result)
  38. // Use ElectronTeamID in Info.plist if already specified
  39. if (appInfo.ElectronTeamID) {
  40. debuglog('`ElectronTeamID` found in `Info.plist`: ' + appInfo.ElectronTeamID)
  41. } else {
  42. // The team identifier in signing identity should not be trusted
  43. if (opts['provisioning-profile']) {
  44. appInfo.ElectronTeamID = opts['provisioning-profile'].message.Entitlements['com.apple.developer.team-identifier']
  45. debuglog('`ElectronTeamID` not found in `Info.plist`, use parsed from provisioning profile: ' + appInfo.ElectronTeamID)
  46. } else {
  47. appInfo.ElectronTeamID = opts.identity.name.substring(opts.identity.name.indexOf('(') + 1, opts.identity.name.lastIndexOf(')'))
  48. debuglog('`ElectronTeamID` not found in `Info.plist`, use parsed from signing identity: ' + appInfo.ElectronTeamID)
  49. }
  50. return writeFileAsync(appInfoPath, plist.build(appInfo), 'utf8')
  51. .then(function () {
  52. debuglog('`Info.plist` updated:', '\n',
  53. '> Info.plist:', appInfoPath)
  54. })
  55. }
  56. })
  57. .then(function () {
  58. var appIdentifier = appInfo.ElectronTeamID + '.' + appInfo.CFBundleIdentifier
  59. // Insert application identifier if not exists
  60. if (entitlements['com.apple.application-identifier']) {
  61. debuglog('`com.apple.application-identifier` found in entitlements file: ' + entitlements['com.apple.application-identifier'])
  62. } else {
  63. debuglog('`com.apple.application-identifier` not found in entitlements file, new inserted: ' + appIdentifier)
  64. entitlements['com.apple.application-identifier'] = appIdentifier
  65. }
  66. // Insert developer team identifier if not exists
  67. if (entitlements['com.apple.developer.team-identifier']) {
  68. debuglog('`com.apple.developer.team-identifier` found in entitlements file: ' + entitlements['com.apple.developer.team-identifier'])
  69. } else {
  70. debuglog('`com.apple.developer.team-identifier` not found in entitlements file, new inserted: ' + appInfo.ElectronTeamID)
  71. entitlements['com.apple.developer.team-identifier'] = appInfo.ElectronTeamID
  72. }
  73. // Init entitlements app group key to array if not exists
  74. if (!entitlements['com.apple.security.application-groups']) {
  75. entitlements['com.apple.security.application-groups'] = []
  76. }
  77. // Insert app group if not exists
  78. if (Array.isArray(entitlements['com.apple.security.application-groups']) && entitlements['com.apple.security.application-groups'].indexOf(appIdentifier) === -1) {
  79. debuglog('`com.apple.security.application-groups` not found in entitlements file, new inserted: ' + appIdentifier)
  80. entitlements['com.apple.security.application-groups'].push(appIdentifier)
  81. } else {
  82. debuglog('`com.apple.security.application-groups` found in entitlements file: ' + appIdentifier)
  83. }
  84. // Create temporary entitlements file
  85. const entitlementsPath = path.join(os.tmpdir(), `tmp-entitlements-${process.pid.toString(16)}-${(tmpFileCounter++).toString(16)}.plist`)
  86. opts.entitlements = entitlementsPath
  87. return writeFileAsync(entitlementsPath, plist.build(entitlements), 'utf8')
  88. .then(function () {
  89. debuglog('Entitlements file updated:', '\n',
  90. '> Entitlements:', entitlementsPath)
  91. })
  92. })
  93. })
  94. }