@capgo/cli 4.3.6 → 5.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/key.ts CHANGED
@@ -21,14 +21,15 @@ export async function saveKey(options: saveOptions, log = true) {
21
21
  const config = await getConfig()
22
22
  const { extConfig } = config.app
23
23
 
24
- const keyPath = options.key || baseKey
24
+ // const keyPath = options.key || baseKey
25
+ const keyPath = options.key || baseKeyPub
25
26
  // check if publicKey exist
26
27
 
27
- let privateKey = options.keyData || ''
28
+ let publicKey = options.keyData || ''
28
29
 
29
- if (!existsSync(keyPath) && !privateKey) {
30
+ if (!existsSync(keyPath) && !publicKey) {
30
31
  if (log) {
31
- p.log.error(`Cannot find public key ${keyPath} or as keyData option or in ${config.app.extConfigFilePath}`)
32
+ p.log.error(`Cannot find a public key at ${keyPath} or as keyData option or in ${config.app.extConfigFilePath}`)
32
33
  program.error('')
33
34
  }
34
35
  else {
@@ -38,7 +39,20 @@ export async function saveKey(options: saveOptions, log = true) {
38
39
  else if (existsSync(keyPath)) {
39
40
  // open with fs publicKey path
40
41
  const keyFile = readFileSync(keyPath)
41
- privateKey = keyFile.toString()
42
+ publicKey = keyFile.toString()
43
+ }
44
+
45
+ // let's doublecheck and make sure the key we are saving is the right type based on the decryption strategy
46
+ if (publicKey) {
47
+ if (!publicKey.startsWith('-----BEGIN RSA PUBLIC KEY-----')) {
48
+ if (log) {
49
+ p.log.error(`the public key provided is not a valid RSA Public key`)
50
+ program.error('')
51
+ }
52
+ else {
53
+ return false
54
+ }
55
+ }
42
56
  }
43
57
 
44
58
  if (extConfig) {
@@ -51,12 +65,16 @@ export async function saveKey(options: saveOptions, log = true) {
51
65
  if (!extConfig.plugins.CapacitorUpdater)
52
66
  extConfig.plugins.CapacitorUpdater = {}
53
67
 
54
- extConfig.plugins.CapacitorUpdater.privateKey = privateKey
68
+ // TODO: this might be a breaking change if user has other code looking at the specific value in the config file
69
+ if (extConfig.plugins.CapacitorUpdater.privateKey)
70
+ delete extConfig.plugins.CapacitorUpdater.privateKey
71
+ extConfig.plugins.CapacitorUpdater.publicKey = publicKey
72
+
55
73
  // console.log('extConfig', extConfig)
56
74
  writeConfig(extConfig, config.app.extConfigFilePath)
57
75
  }
58
76
  if (log) {
59
- p.log.success(`private key saved into ${config.app.extConfigFilePath} file in local directory`)
77
+ p.log.success(`public key saved into ${config.app.extConfigFilePath} file in local directory`)
60
78
  p.log.success(`your app will decode the zip archive with this key`)
61
79
  }
62
80
  return true
@@ -98,6 +116,7 @@ export async function createKey(options: Options, log = true) {
98
116
 
99
117
  const config = await getConfig()
100
118
  const { extConfig } = config.app
119
+
101
120
  if (extConfig) {
102
121
  if (!extConfig.plugins) {
103
122
  extConfig.plugins = {
@@ -105,21 +124,29 @@ export async function createKey(options: Options, log = true) {
105
124
  CapacitorUpdater: {},
106
125
  }
107
126
  }
108
- extConfig.plugins.CapacitorUpdater.privateKey = privateKey
127
+
128
+ if (!extConfig.plugins.CapacitorUpdater)
129
+ extConfig.plugins.CapacitorUpdater = {}
130
+
131
+ // TODO: this might be a breaking change if user has other code looking at the specific value in the config file
132
+ if (extConfig.plugins.CapacitorUpdater.privateKey)
133
+ delete extConfig.plugins.CapacitorUpdater.privateKey
134
+ extConfig.plugins.CapacitorUpdater.publicKey = publicKey
135
+
109
136
  // console.log('extConfig', extConfig)
110
137
  writeConfig(extConfig, config.app.extConfigFilePath)
111
138
  }
112
139
 
113
140
  if (log) {
114
141
  p.log.success('Your RSA key has been generated')
115
- p.log.success(`Public key saved in ${baseKeyPub}`)
142
+ p.log.success(`Private key saved in ${baseKey}`)
116
143
  p.log.success('This key will be use to encrypt your bundle before sending it to Capgo')
117
144
  p.log.success('Keep it safe')
118
145
  p.log.success('Than make it unreadable by Capgo and unmodifiable by anyone')
119
- p.log.success(`Private key saved in ${config.app.extConfigFilePath}`)
146
+ p.log.success(`Public key saved in ${config.app.extConfigFilePath}`)
120
147
  p.log.success('Your app will be the only one having it')
121
148
  p.log.success('Only your users can decrypt your update')
122
- p.log.success('Only you can send them an update')
149
+ p.log.success('Only your key can send them an update')
123
150
  p.outro(`Done ✅`)
124
151
  }
125
152
  return true