@capacitor-community/sqlite 3.4.0-1 → 3.4.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.
@@ -34,6 +34,7 @@ class Database {
34
34
  var mode: String
35
35
  var vUpgDict: [Int: [String: Any]]
36
36
  var databaseLocation: String
37
+ var account: String
37
38
  var path: String = ""
38
39
  var mDb: OpaquePointer?
39
40
  let globalData: GlobalSQLite = GlobalSQLite()
@@ -43,10 +44,11 @@ class Database {
43
44
 
44
45
  // MARK: - Init
45
46
  init(databaseLocation: String, databaseName: String, encrypted: Bool,
46
- mode: String, version: Int,
47
+ account: String, mode: String, version: Int,
47
48
  vUpgDict: [Int: [String: Any]] = [:]) throws {
48
49
  self.dbVersion = version
49
50
  self.encrypted = encrypted
51
+ self.account = account
50
52
  self.dbName = databaseName
51
53
  self.mode = mode
52
54
  self.vUpgDict = vUpgDict
@@ -94,7 +96,7 @@ class Database {
94
96
  func open () throws {
95
97
  var password: String = ""
96
98
  if encrypted && (mode == "secret" || mode == "encryption") {
97
- password = UtilsSecret.getPassphrase()
99
+ password = UtilsSecret.getPassphrase(account: account)
98
100
  }
99
101
  if mode == "encryption" {
100
102
  do {
@@ -7,6 +7,10 @@
7
7
  //
8
8
  import Foundation
9
9
  extension NSNotification.Name {
10
- static var importJsonProgress: Notification.Name {return .init(rawValue: "importJsonProgress")}
11
- static var exportJsonProgress: Notification.Name {return .init(rawValue: "exportJsonProgress")}
10
+ static var importJsonProgress: Notification.Name {
11
+ return .init(rawValue: "importJsonProgress")}
12
+ static var exportJsonProgress: Notification.Name {
13
+ return .init(rawValue: "exportJsonProgress")}
14
+ static var biometricEvent: Notification.Name {
15
+ return .init(rawValue: "biometricEvent")}
12
16
  }
@@ -52,7 +52,6 @@ class KeychainWrapper {
52
52
  print("Error converting value to data.")
53
53
  throw KeychainWrapperError(type: .badData)
54
54
  }
55
-
56
55
  // 1
57
56
  let query: [String: Any] = [
58
57
  // 2
@@ -91,6 +90,7 @@ class KeychainWrapper {
91
90
  // 2
92
91
  kSecMatchLimit as String: kSecMatchLimitOne,
93
92
  kSecReturnAttributes as String: true,
93
+
94
94
  // 3
95
95
  kSecReturnData as String: true
96
96
  ]
@@ -7,4 +7,7 @@
7
7
 
8
8
  public struct SqliteConfig {
9
9
  var iosDatabaseLocation: String?
10
+ var biometricAuth: Int?
11
+ var biometricTitle: String?
12
+ var iosKeychainPrefix: String?
10
13
  }
@@ -44,7 +44,8 @@ enum State: String {
44
44
  class UtilsSQLCipher {
45
45
 
46
46
  class func getDatabaseState(databaseLocation: String,
47
- databaseName: String) -> State {
47
+ databaseName: String,
48
+ account: String) -> State {
48
49
  do {
49
50
  let path: String = try UtilsFile
50
51
  .getFilePath(databaseLocation: databaseLocation,
@@ -56,7 +57,7 @@ class UtilsSQLCipher {
56
57
  } catch UtilsSQLCipherError.openDBNoPassword(let message) {
57
58
  if message == "Open" {
58
59
  do {
59
- try openDBStoredPassword(dBPath: path)
60
+ try openDBStoredPassword(dBPath: path, account: account)
60
61
  return State.ENCRYPTEDSECRET
61
62
  } catch UtilsSQLCipherError.openDBStoredPassword(let message) {
62
63
  if message == "Open" {
@@ -100,9 +101,9 @@ class UtilsSQLCipher {
100
101
  }
101
102
 
102
103
  }
103
- class func openDBStoredPassword(dBPath: String) throws {
104
+ class func openDBStoredPassword(dBPath: String, account: String) throws {
104
105
  do {
105
- let password: String = UtilsSecret.getPassphrase()
106
+ let password: String = UtilsSecret.getPassphrase(account: account)
106
107
  let oDb: OpaquePointer? = try openOrCreateDatabase(
107
108
  filename: dBPath, password: password, readonly: true)
108
109
  try close(oDB: oDb)
@@ -9,30 +9,42 @@
9
9
  import Foundation
10
10
 
11
11
  enum UtilsSecretError: Error {
12
+ case prefixPassphrase(message: String)
12
13
  case setPassphrase(message: String)
13
14
  case changePassphrase(message: String)
14
15
  case setEncryptionSecret(message: String)
15
16
  case changeEncryptionSecret(message: String)
16
17
  }
17
18
 
19
+ let oldAccount: String = "CapacitorSQLitePlugin"
20
+
18
21
  class UtilsSecret {
19
22
 
20
23
  // MARK: - IsPassphrase
21
24
 
22
- class func isPassphrase() -> Bool {
23
- var ret: Bool = false
24
- if getPassphrase() != "" {
25
- ret = true
25
+ class func isPassphrase(account: String) throws -> Bool {
26
+
27
+ if !getPassphrase(account: account).isEmpty {
28
+ return true
26
29
  }
27
- return ret
30
+ if !getPassphrase(account: oldAccount).isEmpty {
31
+ let passphrase = getPassphrase(account: oldAccount)
32
+ do {
33
+ try setPassphrase(account: account, passphrase: passphrase)
34
+ return true
35
+ } catch UtilsSecretError.prefixPassphrase(let message) {
36
+ throw UtilsSecretError.changePassphrase(message: message)
37
+ }
38
+ }
39
+ return false
28
40
  }
29
41
 
30
42
  // MARK: - GetPassphrase
31
43
 
32
- class func getPassphrase() -> String {
44
+ class func getPassphrase(account: String) -> String {
33
45
  let kcw = KeychainWrapper()
34
46
  if let password = try? kcw.getGenericPasswordFor(
35
- account: "CapacitorSQLitePlugin",
47
+ account: account,
36
48
  service: "unlockSecret") {
37
49
  return password
38
50
  }
@@ -41,11 +53,11 @@ class UtilsSecret {
41
53
 
42
54
  // MARK: - SetPassphrase
43
55
 
44
- class func setPassphrase(passphrase: String) throws {
56
+ class func setPassphrase(account: String, passphrase: String) throws {
45
57
  let kcw = KeychainWrapper()
46
58
  do {
47
59
  try kcw.storeGenericPasswordFor(
48
- account: "CapacitorSQLitePlugin",
60
+ account: account,
49
61
  service: "unlockSecret",
50
62
  password: passphrase)
51
63
  } catch let error as KeychainWrapperError {
@@ -62,9 +74,9 @@ class UtilsSecret {
62
74
 
63
75
  // MARK: - ValidatePassphrase
64
76
 
65
- class func validatePassphrase(_ passphrase: String) -> Bool {
77
+ class func validatePassphrase(account: String, passphrase: String) -> Bool {
66
78
  var ret: Bool = false
67
- let currentPassphrase = getPassphrase()
79
+ let currentPassphrase = getPassphrase(account: account)
68
80
  if passphrase == currentPassphrase {
69
81
  ret = true
70
82
  }
@@ -73,10 +85,12 @@ class UtilsSecret {
73
85
 
74
86
  // MARK: - ChangePassphrase
75
87
 
76
- class func changePassphrase(oldPassphrase: String, passphrase: String) throws -> Bool {
77
- guard validatePassphrase(oldPassphrase) == true else { return false }
88
+ class func changePassphrase(account: String, oldPassphrase: String,
89
+ passphrase: String) throws -> Bool {
90
+ guard validatePassphrase(account: account,
91
+ passphrase: oldPassphrase) == true else { return false }
78
92
  do {
79
- try setPassphrase(passphrase: passphrase)
93
+ try setPassphrase(account: account, passphrase: passphrase)
80
94
  return true
81
95
  } catch UtilsSecretError.setPassphrase(let message) {
82
96
  throw UtilsSecretError.changePassphrase(message: message)
@@ -86,15 +100,25 @@ class UtilsSecret {
86
100
  // MARK: - SetEncryptionSecret
87
101
 
88
102
  // swiftlint:disable function_body_length
89
- class func setEncryptionSecret(passphrase: String,
103
+ // swiftlint:disable cyclomatic_complexity
104
+ class func setEncryptionSecret(prefix: String, passphrase: String,
90
105
  databaseLocation: String) throws {
91
106
  do {
107
+ if prefix.isEmpty {
108
+ let msg: String = "keychain prefix must not be empty"
109
+ throw UtilsSecretError.setEncryptionSecret(message: msg)
110
+ }
92
111
  if passphrase.isEmpty {
93
112
  let msg: String = "passphrase must not be empty"
94
113
  throw UtilsSecretError.setEncryptionSecret(message: msg)
95
114
  }
96
115
  // store encrypted passphrase
97
- try setPassphrase(passphrase: passphrase)
116
+ let account = "\(prefix)_\(oldAccount)"
117
+ if !getPassphrase(account: account).isEmpty {
118
+ let msg: String = "passphrase already stored in keychain"
119
+ throw UtilsSecretError.setEncryptionSecret(message: msg)
120
+ }
121
+ try setPassphrase(account: account, passphrase: passphrase)
98
122
 
99
123
  // get the list of databases
100
124
  let databaseURL: URL = try UtilsFile.getDatabasesUrl().absoluteURL
@@ -106,7 +130,7 @@ class UtilsSecret {
106
130
  for file: String in dbList {
107
131
  let state: State = UtilsSQLCipher
108
132
  .getDatabaseState(databaseLocation: databaseLocation,
109
- databaseName: file)
133
+ databaseName: file, account: account)
110
134
  if state.rawValue == "ENCRYPTEDGLOBALSECRET" {
111
135
  let globalData: GlobalSQLite = GlobalSQLite()
112
136
  let password: String = globalData.secret
@@ -140,25 +164,33 @@ class UtilsSecret {
140
164
  }
141
165
 
142
166
  }
167
+ // swiftlint:enable cyclomatic_complexity
143
168
  // swiftlint:enable function_body_length
144
169
 
145
170
  // MARK: - ChangeEncryptionSecret
146
171
 
147
172
  // swiftlint:disable function_body_length
148
- class func changeEncryptionSecret(passphrase: String,
173
+ class func changeEncryptionSecret(prefix: String, passphrase: String,
149
174
  oldPassphrase: String,
150
175
  databaseLocation: String) throws {
151
176
  do {
177
+ if prefix.isEmpty {
178
+ let msg: String = "Keychain prefix must not " +
179
+ "be empty"
180
+ throw UtilsSecretError.changeEncryptionSecret(message: msg)
181
+ }
152
182
  if passphrase.isEmpty || oldPassphrase.isEmpty {
153
183
  let msg: String = "Passphrase and/or oldpassphrase must not " +
154
184
  "be empty"
155
185
  throw UtilsSecretError.changeEncryptionSecret(message: msg)
156
186
  }
157
- guard isPassphrase() == true else {
187
+ let account = "\(prefix)_\(oldAccount)"
188
+ guard try isPassphrase(account: account) == true else {
158
189
  let msg: String = "Encryption secret has not been set"
159
190
  throw UtilsSecretError.changeEncryptionSecret(message: msg)
160
191
  }
161
- guard validatePassphrase(oldPassphrase) == true else {
192
+ guard validatePassphrase(account: account,
193
+ passphrase: oldPassphrase) == true else {
162
194
  let msg: String = "Given oldpassphrase is wrong"
163
195
  throw UtilsSecretError.changeEncryptionSecret(message: msg)
164
196
  }
@@ -173,7 +205,7 @@ class UtilsSecret {
173
205
  for file: String in dbList {
174
206
  let state: State = UtilsSQLCipher
175
207
  .getDatabaseState(databaseLocation: databaseLocation,
176
- databaseName: file)
208
+ databaseName: file, account: account)
177
209
  if state.rawValue == "ENCRYPTEDSECRET" {
178
210
  let dbPath: String = try UtilsFile
179
211
  .getFilePath(databaseLocation: databaseLocation,
@@ -199,7 +231,7 @@ class UtilsSecret {
199
231
  }
200
232
 
201
233
  // store encrypted passphrase
202
- try setPassphrase(passphrase: passphrase)
234
+ try setPassphrase(account: account, passphrase: passphrase)
203
235
 
204
236
  } catch UtilsSecretError.setPassphrase(let message) {
205
237
  throw UtilsSecretError.setEncryptionSecret(message: message)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/sqlite",
3
- "version": "3.4.0-1",
3
+ "version": "3.4.0-2",
4
4
  "description": "Community plugin for native & electron SQLite databases",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",