@capacitor-community/sqlite 4.6.2-1 → 4.6.2-3

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.
@@ -606,6 +606,34 @@ enum CapacitorSQLiteError: Error {
606
606
  }
607
607
  }
608
608
 
609
+ // MARK: - IsDatabaseEncrypted
610
+
611
+ @objc public func isDatabaseEncrypted(_ dbName: String) throws -> NSNumber {
612
+ if isInit {
613
+ let mDbName = CapacitorSQLite.getDatabaseName(dbName: dbName)
614
+ let isFileExists: Bool = UtilsFile
615
+ .isFileExist(databaseLocation: databaseLocation,
616
+ fileName: mDbName + "SQLite.db")
617
+ if isFileExists {
618
+ let state: State = UtilsSQLCipher
619
+ .getDatabaseState(databaseLocation: databaseLocation,
620
+ databaseName: mDbName + "SQLite.db",
621
+ account: account)
622
+ if state.rawValue == "ENCRYPTEDGLOBALSECRET" || state.rawValue == "ENCRYPTEDSECRET" {
623
+ return 1
624
+ }
625
+ if state.rawValue == "UNENCRYPTED" {
626
+ return 0
627
+ }
628
+ throw CapacitorSQLiteError.failed(message: "Database unknown")
629
+ } else {
630
+ throw CapacitorSQLiteError.failed(message: "Database does not exist")
631
+ }
632
+ } else {
633
+ throw CapacitorSQLiteError.failed(message: initMessage)
634
+ }
635
+ }
636
+
609
637
  // MARK: - IsNCDatabase
610
638
 
611
639
  @objc public func isNCDatabase(_ databasePath: String) throws -> NSNumber {
@@ -46,4 +46,7 @@ CAP_PLUGIN(CapacitorSQLitePlugin, "CapacitorSQLite",
46
46
  CAP_PLUGIN_METHOD(clearEncryptionSecret, CAPPluginReturnPromise);
47
47
  CAP_PLUGIN_METHOD(getFromHTTPRequest, CAPPluginReturnPromise);
48
48
  CAP_PLUGIN_METHOD(checkEncryptionSecret, CAPPluginReturnPromise);
49
+ CAP_PLUGIN_METHOD(isInConfigEncryption, CAPPluginReturnPromise);
50
+ CAP_PLUGIN_METHOD(isInConfigBiometricAuth, CAPPluginReturnPromise);
51
+ CAP_PLUGIN_METHOD(isDatabaseEncrypted, CAPPluginReturnPromise);
49
52
  )
@@ -41,6 +41,56 @@ public class CapacitorSQLitePlugin: CAPPlugin {
41
41
  }
42
42
  }
43
43
 
44
+ // MARK: - IsInConfigEncryption
45
+
46
+ @objc func isInConfigEncryption(_ call: CAPPluginCall) {
47
+ var bRes: Bool = false
48
+ if self.config?.iosIsEncryption == 1 {
49
+ bRes = true
50
+ }
51
+
52
+ retHandler.rResult(call: call, ret: bRes)
53
+ }
54
+
55
+ // MARK: - IsInConfigBiometricAuth
56
+
57
+ @objc func isInConfigBiometricAuth(_ call: CAPPluginCall) {
58
+ var bRes: Bool = false
59
+ if self.config?.biometricAuth == 1 {
60
+ bRes = true
61
+ }
62
+
63
+ retHandler.rResult(call: call, ret: bRes)
64
+ }
65
+ // MARK: - IsDatabaseEncrypted
66
+
67
+ @objc func isDatabaseEncrypted(_ call: CAPPluginCall) {
68
+ guard let dbName = call.options["database"] as? String else {
69
+ retHandler.rResult(
70
+ call: call, ret: false,
71
+ message: "isDatabaseEncrypted: Must provide a database name")
72
+ return
73
+ }
74
+ do {
75
+ let res = try implementation?.isDatabaseEncrypted(dbName)
76
+ var bRes: Bool = false
77
+ if res == 1 {
78
+ bRes = true
79
+ }
80
+ retHandler.rResult(call: call, ret: bRes)
81
+ } catch CapacitorSQLiteError.failed(let message) {
82
+ let msg = "isDatabaseEncrypted: \(message)"
83
+ retHandler.rResult(call: call, message: msg)
84
+ return
85
+ } catch let error {
86
+ retHandler.rResult(
87
+ call: call,
88
+ message: "isDatabaseEncrypted: \(error)")
89
+ return
90
+ }
91
+
92
+ }
93
+
44
94
  // MARK: - IsSecretStored
45
95
 
46
96
  @objc func isSecretStored(_ call: CAPPluginCall) {
@@ -103,6 +103,11 @@ class Database {
103
103
  func open () throws {
104
104
  var password: String = ""
105
105
  if isEncryption && encrypted && (mode == "secret" || mode == "encryption") {
106
+ let isPassphrase = try UtilsSecret.isPassphrase(account: account)
107
+ if !isPassphrase {
108
+ let msg: String = "No Passphrase stored"
109
+ throw DatabaseError.open(message: msg)
110
+ }
106
111
  password = UtilsSecret.getPassphrase(account: account)
107
112
  }
108
113
  if mode == "encryption" {
@@ -110,7 +115,8 @@ class Database {
110
115
  do {
111
116
  let ret: Bool = try UtilsEncryption
112
117
  .encryptDatabase(databaseLocation: databaseLocation,
113
- filePath: path, password: password)
118
+ filePath: path, password: password,
119
+ version: dbVersion)
114
120
  if !ret {
115
121
  let msg: String = "Failed in encryption"
116
122
  throw DatabaseError.open(message: msg)
@@ -18,9 +18,10 @@ class UtilsEncryption {
18
18
 
19
19
  // swiftlint:disable function_body_length
20
20
  class func encryptDatabase(databaseLocation: String, filePath: String,
21
- password: String) throws -> Bool {
21
+ password: String, version: Int) throws -> Bool {
22
22
  var ret: Bool = false
23
23
  var oDB: OpaquePointer?
24
+ var eDB: OpaquePointer?
24
25
  do {
25
26
  if UtilsFile.isFileExist(filePath: filePath) {
26
27
  do {
@@ -34,7 +35,7 @@ class UtilsEncryption {
34
35
  .openOrCreateDatabase(filename: tempPath,
35
36
  password: "",
36
37
  readonly: false)
37
- try _ = UtilsSQLCipher
38
+ eDB = try UtilsSQLCipher
38
39
  .openOrCreateDatabase(filename: filePath,
39
40
  password: password,
40
41
  readonly: false)
@@ -48,6 +49,13 @@ class UtilsEncryption {
48
49
  try _ = UtilsFile
49
50
  .deleteFile(fileName: "temp.db",
50
51
  databaseLocation: databaseLocation)
52
+ // set the version
53
+ let sqltr: String = "PRAGMA user_version = \(version);"
54
+ if sqlite3_exec(eDB, sqltr, nil, nil, nil) != SQLITE_OK {
55
+ throw UtilsEncryptionError
56
+ .encryptionFailed(message: "set version to \(version) failed")
57
+ }
58
+
51
59
  ret = true
52
60
  }
53
61
  // close the db
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "4.6.2-1",
3
+ "version": "4.6.2-3",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -158,6 +158,25 @@ export interface CapacitorSQLitePlugin {
158
158
  * @since 3.0.0-beta.5
159
159
  */
160
160
  isDBOpen(options: capSQLiteOptions): Promise<capSQLiteResult>;
161
+ /**
162
+ * Check if a SQLite database is encrypted
163
+ * @param options: capSQLiteOptions
164
+ * @returns Promise<capSQLiteResult>
165
+ * @since 4.6.2-2
166
+ */
167
+ isDatabaseEncrypted(options: capSQLiteOptions): Promise<capSQLiteResult>;
168
+ /**
169
+ * Check encryption value in capacitor.config
170
+ * @returns Promise<capSQLiteResult>
171
+ * @since 4.6.2-2
172
+ */
173
+ isInConfigEncryption(): Promise<capSQLiteResult>;
174
+ /**
175
+ * Check encryption value in capacitor.config
176
+ * @returns Promise<capSQLiteResult>
177
+ * @since 4.6.2-2
178
+ */
179
+ isInConfigBiometricAuth(): Promise<capSQLiteResult>;
161
180
  /**
162
181
  * Check if a SQLite database exists without connection
163
182
  * @param options: capSQLiteOptions
@@ -1054,6 +1073,25 @@ export interface ISQLiteConnection {
1054
1073
  * @since 4.1.1
1055
1074
  */
1056
1075
  getFromHTTPRequest(url?: string, overwrite?: boolean): Promise<void>;
1076
+ /**
1077
+ * Check if a SQLite database is encrypted
1078
+ * @param options: capSQLiteOptions
1079
+ * @returns Promise<capSQLiteResult>
1080
+ * @since 4.6.2-2
1081
+ */
1082
+ isDatabaseEncrypted(database: string): Promise<capSQLiteResult>;
1083
+ /**
1084
+ * Check encryption value in capacitor.config
1085
+ * @returns Promise<capSQLiteResult>
1086
+ * @since 4.6.2-2
1087
+ */
1088
+ isInConfigEncryption(): Promise<capSQLiteResult>;
1089
+ /**
1090
+ * Check encryption value in capacitor.config
1091
+ * @returns Promise<capSQLiteResult>
1092
+ * @since 4.6.2-2
1093
+ */
1094
+ isInConfigBiometricAuth(): Promise<capSQLiteResult>;
1057
1095
  /**
1058
1096
  * Check if a database exists
1059
1097
  * @param database
@@ -1418,6 +1456,31 @@ export class SQLiteConnection implements ISQLiteConnection {
1418
1456
  return Promise.reject(err);
1419
1457
  }
1420
1458
  }
1459
+ async isDatabaseEncrypted(database: string): Promise<capSQLiteResult> {
1460
+ if (database.endsWith('.db')) database = database.slice(0, -3);
1461
+ try {
1462
+ const res = await this.sqlite.isDatabaseEncrypted({ database: database });
1463
+ return Promise.resolve(res);
1464
+ } catch (err) {
1465
+ return Promise.reject(err);
1466
+ }
1467
+ }
1468
+ async isInConfigEncryption(): Promise<capSQLiteResult> {
1469
+ try {
1470
+ const res = await this.sqlite.isInConfigEncryption();
1471
+ return Promise.resolve(res);
1472
+ } catch (err) {
1473
+ return Promise.reject(err);
1474
+ }
1475
+ }
1476
+ async isInConfigBiometricAuth(): Promise<capSQLiteResult> {
1477
+ try {
1478
+ const res = await this.sqlite.isInConfigBiometricAuth();
1479
+ return Promise.resolve(res);
1480
+ } catch (err) {
1481
+ return Promise.reject(err);
1482
+ }
1483
+ }
1421
1484
  async isDatabase(database: string): Promise<capSQLiteResult> {
1422
1485
  if (database.endsWith('.db')) database = database.slice(0, -3);
1423
1486
  try {
package/src/web.ts CHANGED
@@ -536,4 +536,19 @@ export class CapacitorSQLiteWeb
536
536
  console.log('isNCDatabase', options);
537
537
  throw this.unimplemented('Not implemented on web.');
538
538
  }
539
+
540
+ async isDatabaseEncrypted(
541
+ options: capSQLiteOptions,
542
+ ): Promise<capSQLiteResult> {
543
+ console.log('isDatabaseEncrypted', options);
544
+ throw this.unimplemented('Not implemented on web.');
545
+ }
546
+
547
+ async isInConfigEncryption(): Promise<capSQLiteResult> {
548
+ throw this.unimplemented('Not implemented on web.');
549
+ }
550
+
551
+ async isInConfigBiometricAuth(): Promise<capSQLiteResult> {
552
+ throw this.unimplemented('Not implemented on web.');
553
+ }
539
554
  }