@capgo/capacitor-updater 9.0.0 → 9.0.2

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.
Files changed (109) hide show
  1. package/CapgoCapacitorUpdater.podspec +3 -6
  2. package/Package.swift +30 -0
  3. package/README.md +2406 -291
  4. package/android/build.gradle +37 -17
  5. package/android/consumer-rules.pro +48 -0
  6. package/android/proguard-rules.pro +23 -0
  7. package/android/src/main/AndroidManifest.xml +0 -1
  8. package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
  11. package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7927 -1243
  14. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5785 -0
  15. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
  16. package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
  17. package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
  18. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
  19. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
  20. package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
  21. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +2041 -118
  22. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +301 -0
  23. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +569 -0
  24. package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +436 -0
  25. package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +604 -0
  26. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
  27. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
  28. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  29. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
  30. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +963 -0
  31. package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
  32. package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
  33. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
  34. package/dist/docs.json +4431 -840
  35. package/dist/esm/definitions.d.ts +2641 -225
  36. package/dist/esm/definitions.js +103 -1
  37. package/dist/esm/definitions.js.map +1 -1
  38. package/dist/esm/history.d.ts +1 -0
  39. package/dist/esm/history.js +283 -0
  40. package/dist/esm/history.js.map +1 -0
  41. package/dist/esm/index.d.ts +3 -2
  42. package/dist/esm/index.js +5 -4
  43. package/dist/esm/index.js.map +1 -1
  44. package/dist/esm/web.d.ts +53 -42
  45. package/dist/esm/web.js +180 -37
  46. package/dist/esm/web.js.map +1 -1
  47. package/dist/plugin.cjs.js +570 -37
  48. package/dist/plugin.cjs.js.map +1 -1
  49. package/dist/plugin.js +570 -37
  50. package/dist/plugin.js.map +1 -1
  51. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
  52. package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
  53. package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
  54. package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
  55. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +6813 -0
  56. package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
  57. package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +200 -0
  58. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +5099 -0
  59. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +387 -0
  60. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
  61. package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
  62. package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
  63. package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
  64. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  65. package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
  66. package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
  67. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +876 -0
  68. package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
  69. package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
  70. package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
  71. package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
  72. package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
  73. package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
  74. package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
  75. package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +6533 -0
  76. package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
  77. package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +156 -0
  78. package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +314 -0
  79. package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +653 -0
  80. package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
  81. package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
  82. package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
  83. package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
  84. package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1109 -0
  85. package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
  86. package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
  87. package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
  88. package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +196 -0
  89. package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
  90. package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +470 -0
  91. package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
  92. package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
  93. package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
  94. package/package.json +80 -43
  95. package/scripts/builtin-hashes-lib.mjs +148 -0
  96. package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
  97. package/scripts/resolve-real-path.mjs +25 -0
  98. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
  99. package/ios/Plugin/BundleInfo.swift +0 -113
  100. package/ios/Plugin/BundleStatus.swift +0 -48
  101. package/ios/Plugin/CapacitorUpdater.swift +0 -858
  102. package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
  103. package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
  104. package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
  105. package/ios/Plugin/CryptoCipher.swift +0 -240
  106. /package/{LICENCE → LICENSE} +0 -0
  107. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
  108. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
  109. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
@@ -0,0 +1,230 @@
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ package ee.forgr.capacitor_updater;
8
+
9
+ import android.content.Context;
10
+ import android.content.SharedPreferences;
11
+ import android.security.keystore.KeyGenParameterSpec;
12
+ import android.security.keystore.KeyProperties;
13
+ import java.io.IOException;
14
+ import java.nio.charset.StandardCharsets;
15
+ import java.security.KeyStore;
16
+ import java.security.KeyStoreException;
17
+ import java.security.NoSuchAlgorithmException;
18
+ import java.security.NoSuchProviderException;
19
+ import java.security.UnrecoverableEntryException;
20
+ import java.security.cert.CertificateException;
21
+ import java.util.UUID;
22
+ import javax.crypto.Cipher;
23
+ import javax.crypto.KeyGenerator;
24
+ import javax.crypto.SecretKey;
25
+ import javax.crypto.spec.GCMParameterSpec;
26
+
27
+ /**
28
+ * Helper class to manage device ID persistence across app installations.
29
+ * Uses Android Keystore-backed storage and backup-restorable SharedPreferences.
30
+ *
31
+ * The device ID is a random UUID stored in encrypted preferences and mirrored
32
+ * to appUUID so Android backup/restore can keep it across reinstalls.
33
+ */
34
+ public class DeviceIdHelper {
35
+
36
+ private static final String KEYSTORE_ALIAS = "capgo_device_id_key";
37
+ private static final String ANDROID_KEYSTORE = "AndroidKeyStore";
38
+ private static final String LEGACY_PREFS_KEY = "appUUID";
39
+ private static final String DEVICE_ID_PREFS = "capgo_device_id";
40
+ private static final String DEVICE_ID_KEY = "deviceId";
41
+ private static final String IV_KEY = "iv";
42
+ private static final int GCM_TAG_LENGTH = 128;
43
+
44
+ /**
45
+ * Gets or creates a device ID that persists across reinstalls.
46
+ *
47
+ * This method:
48
+ * 1. First checks for an existing ID in Keystore-encrypted storage
49
+ * 2. Falls back to legacy SharedPreferences restored by Android backup
50
+ * 3. Generates a new UUID if neither exists
51
+ * 4. Stores the ID synchronously in both stores for future use
52
+ *
53
+ * @param context Application context
54
+ * @param legacyPrefs Legacy SharedPreferences (for migration)
55
+ * @return Device ID as a lowercase UUID string
56
+ */
57
+ public static String getOrCreateDeviceId(Context context, SharedPreferences legacyPrefs) {
58
+ try {
59
+ // Try to get device ID from Keystore storage
60
+ String deviceId = getDeviceIdFromKeystore(context);
61
+
62
+ if (deviceId != null && !deviceId.isEmpty()) {
63
+ deviceId = deviceId.toLowerCase();
64
+ saveLegacyDeviceId(legacyPrefs, deviceId);
65
+ return deviceId;
66
+ }
67
+
68
+ // Migration: Check legacy SharedPreferences for existing device ID
69
+ deviceId = legacyPrefs.getString(LEGACY_PREFS_KEY, null);
70
+
71
+ if (deviceId == null || deviceId.isEmpty()) {
72
+ // Generate new device ID if none exists
73
+ deviceId = UUID.randomUUID().toString();
74
+ }
75
+
76
+ // Ensure lowercase for consistency
77
+ deviceId = deviceId.toLowerCase();
78
+
79
+ // Save to backup-restorable preferences and Keystore storage
80
+ saveLegacyDeviceId(legacyPrefs, deviceId);
81
+ saveDeviceIdToKeystore(context, deviceId);
82
+
83
+ return deviceId;
84
+ } catch (Exception e) {
85
+ // Fallback to legacy method if Keystore fails
86
+ return getFallbackDeviceId(legacyPrefs);
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Retrieves the device ID from Keystore-encrypted storage.
92
+ *
93
+ * @param context Application context
94
+ * @return Device ID string or null if not found
95
+ */
96
+ private static String getDeviceIdFromKeystore(Context context) {
97
+ try {
98
+ SharedPreferences prefs = context.getSharedPreferences(DEVICE_ID_PREFS, Context.MODE_PRIVATE);
99
+ String encryptedDeviceId = prefs.getString(DEVICE_ID_KEY, null);
100
+ String ivString = prefs.getString(IV_KEY, null);
101
+
102
+ if (encryptedDeviceId == null || ivString == null) {
103
+ return null;
104
+ }
105
+
106
+ // Get the encryption key from Keystore
107
+ SecretKey key = getOrCreateKey();
108
+ if (key == null) {
109
+ return null;
110
+ }
111
+
112
+ // Decrypt the device ID
113
+ byte[] encryptedBytes = android.util.Base64.decode(encryptedDeviceId, android.util.Base64.DEFAULT);
114
+ byte[] iv = android.util.Base64.decode(ivString, android.util.Base64.DEFAULT);
115
+
116
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
117
+ GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
118
+ cipher.init(Cipher.DECRYPT_MODE, key, spec);
119
+
120
+ byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
121
+ return new String(decryptedBytes, StandardCharsets.UTF_8);
122
+ } catch (Exception e) {
123
+ return null;
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Saves the device ID to Keystore-encrypted storage.
129
+ *
130
+ * The device ID is encrypted using AES/GCM with a key stored in Android Keystore.
131
+ * The Keystore key persists across reinstalls on Android 6.0+ (API 23+).
132
+ *
133
+ * @param context Application context
134
+ * @param deviceId The device ID to save
135
+ */
136
+ private static void saveDeviceIdToKeystore(Context context, String deviceId) throws Exception {
137
+ // Get or create encryption key in Keystore
138
+ SecretKey key = getOrCreateKey();
139
+ if (key == null) {
140
+ throw new Exception("Failed to get encryption key");
141
+ }
142
+
143
+ // Encrypt the device ID
144
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
145
+ cipher.init(Cipher.ENCRYPT_MODE, key);
146
+
147
+ byte[] iv = cipher.getIV();
148
+ byte[] encryptedBytes = cipher.doFinal(deviceId.getBytes(StandardCharsets.UTF_8));
149
+
150
+ // Store encrypted device ID and IV in SharedPreferences
151
+ SharedPreferences prefs = context.getSharedPreferences(DEVICE_ID_PREFS, Context.MODE_PRIVATE);
152
+ prefs
153
+ .edit()
154
+ .putString(DEVICE_ID_KEY, android.util.Base64.encodeToString(encryptedBytes, android.util.Base64.DEFAULT))
155
+ .putString(IV_KEY, android.util.Base64.encodeToString(iv, android.util.Base64.DEFAULT))
156
+ .commit();
157
+ }
158
+
159
+ /**
160
+ * Gets or creates the encryption key in Android Keystore.
161
+ *
162
+ * The key is configured to persist across reinstalls and not require user authentication.
163
+ *
164
+ * @return SecretKey from Keystore or null if failed
165
+ */
166
+ private static SecretKey getOrCreateKey() {
167
+ try {
168
+ KeyStore keyStore = KeyStore.getInstance(ANDROID_KEYSTORE);
169
+ keyStore.load(null);
170
+
171
+ // Check if key already exists
172
+ if (keyStore.containsAlias(KEYSTORE_ALIAS)) {
173
+ KeyStore.SecretKeyEntry entry = (KeyStore.SecretKeyEntry) keyStore.getEntry(KEYSTORE_ALIAS, null);
174
+ return entry.getSecretKey();
175
+ }
176
+
177
+ // Create new key
178
+ KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE);
179
+
180
+ KeyGenParameterSpec keySpec = new KeyGenParameterSpec.Builder(
181
+ KEYSTORE_ALIAS,
182
+ KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
183
+ )
184
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
185
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
186
+ .setKeySize(256)
187
+ .setRandomizedEncryptionRequired(true)
188
+ .build();
189
+
190
+ keyGenerator.init(keySpec);
191
+ return keyGenerator.generateKey();
192
+ } catch (
193
+ KeyStoreException
194
+ | CertificateException
195
+ | NoSuchAlgorithmException
196
+ | IOException
197
+ | NoSuchProviderException
198
+ | UnrecoverableEntryException e
199
+ ) {
200
+ return null;
201
+ } catch (Exception e) {
202
+ return null;
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Fallback method using legacy SharedPreferences if Keystore fails or API < 23.
208
+ *
209
+ * @param legacyPrefs Legacy SharedPreferences
210
+ * @return Device ID string
211
+ */
212
+ private static String getFallbackDeviceId(SharedPreferences legacyPrefs) {
213
+ String deviceId = legacyPrefs.getString(LEGACY_PREFS_KEY, null);
214
+
215
+ if (deviceId == null || deviceId.isEmpty()) {
216
+ deviceId = UUID.randomUUID().toString();
217
+ saveLegacyDeviceId(legacyPrefs, deviceId);
218
+ }
219
+
220
+ return deviceId.toLowerCase();
221
+ }
222
+
223
+ private static void saveLegacyDeviceId(SharedPreferences legacyPrefs, String deviceId) {
224
+ if (legacyPrefs == null || deviceId == null || deviceId.isEmpty()) {
225
+ return;
226
+ }
227
+
228
+ legacyPrefs.edit().putString(LEGACY_PREFS_KEY, deviceId).commit();
229
+ }
230
+ }