Skip to content

Commit e169cf8

Browse files
committed
refactor: Update KeychainManager methods for improved query handling and modify module name in nitro.json
1 parent e8406b9 commit e169cf8

2 files changed

Lines changed: 39 additions & 30 deletions

File tree

ios/SensitiveInfo.swift

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,19 @@ class KeychainManager {
121121
return level != .standard // Always allow fallback unless already at standard
122122
}
123123

124-
/// Build keychain query for the specified security level
125-
static func buildQuery(for key: String, securityLevel: SecurityLevel, options: StorageOptions?) -> [String: Any] {
124+
/// Build keychain query without specifying an account (for all items)
125+
static func buildQuery(securityLevel: SecurityLevel, options: StorageOptions?) -> [String: Any] {
126+
return buildQuery(for: nil, securityLevel: securityLevel, options: options)
127+
}
128+
/// Build keychain query for the specified security level and account key
129+
static func buildQuery(for key: String?, securityLevel: SecurityLevel, options: StorageOptions?) -> [String: Any] {
126130
var query: [String: Any] = [
127131
kSecClass as String: kSecClassGenericPassword,
128132
kSecAttrService as String: "ReactNativeSensitiveInfo",
129133
]
130134

131-
// Only add account if key is not empty (for specific item operations)
132-
if !key.isEmpty {
135+
// Only add account if key is provided and not empty (for specific item operations)
136+
if let key = key, !key.isEmpty {
133137
query[kSecAttrAccount as String] = key
134138
}
135139

@@ -182,10 +186,10 @@ class KeychainManager {
182186

183187
// MARK: - Main Implementation
184188

185-
@available(iOS 11.0, macOS 10.13, *)
186-
public class SensitiveInfoCore {
189+
fileprivate class SensitiveInfoCore {
187190

188-
public init() {}
191+
/// Core implementation initializer
192+
init() {}
189193

190194
// MARK: - Core Operations
191195

@@ -234,7 +238,8 @@ public class SensitiveInfoCore {
234238
if status == errSecSuccess {
235239
if let data = result as? Data,
236240
let string = String(data: data, encoding: .utf8) {
237-
return .success(string)
241+
// Wrap in String? to unify generic as String?
242+
return .success(string as String?)
238243
} else {
239244
return .failure(SensitiveInfoError.operationFailed("Invalid data format"))
240245
}
@@ -280,9 +285,8 @@ public class SensitiveInfoCore {
280285
let levels: [SecurityLevel] = [.standard, .biometric, .strongbox]
281286

282287
for level in levels {
283-
let query = KeychainManager.buildQuery(for: "", securityLevel: level, options: nil)
288+
let query = KeychainManager.buildQuery(securityLevel: level, options: nil)
284289
var mutableQuery = query
285-
mutableQuery.removeValue(forKey: kSecAttrAccount as String) // Remove specific account to get all
286290
mutableQuery[kSecReturnData as String] = true
287291
mutableQuery[kSecReturnAttributes as String] = true
288292
mutableQuery[kSecMatchLimit as String] = kSecMatchLimitAll
@@ -311,9 +315,8 @@ public class SensitiveInfoCore {
311315
let levels: [SecurityLevel] = [.standard, .biometric, .strongbox]
312316

313317
for level in levels {
314-
let query = KeychainManager.buildQuery(for: "", securityLevel: level, options: nil)
318+
let query = KeychainManager.buildQuery(securityLevel: level, options: nil)
315319
var mutableQuery = query
316-
mutableQuery.removeValue(forKey: kSecAttrAccount as String) // Remove specific account to clear all
317320

318321
SecItemDelete(mutableQuery as CFDictionary) // Ignore errors, some levels might not have items
319322
}
@@ -345,59 +348,65 @@ public class SensitiveInfoCore {
345348
}
346349
}
347350

351+
348352
// MARK: - Nitro Interface
349353

350-
@available(iOS 11.0, macOS 10.13, *)
351-
public class SensitiveInfoImpl: HybridSensitiveInfoSpec {
354+
public final class RNSensitiveInfo: HybridSensitiveInfoSpec_base, HybridSensitiveInfoSpec_protocol {
352355
private let implementation = SensitiveInfoCore()
353356

357+
/// Module initializer
358+
public override init() {
359+
super.init()
360+
}
361+
362+
/// Initialize module
354363
public var memorySize: Int {
355-
return MemoryLayout<SensitiveInfoImpl>.size
364+
return MemoryLayout<RNSensitiveInfo>.size
356365
}
357366

358367
// MARK: - Core Operations
359368

360369
public func getItem(key: String, options: StorageOptions?) throws -> Promise<String?> {
361-
return Promise.async { [weak self] in
362-
try self?.implementation.getItem(key: key, options: options)
370+
return Promise.async { [unowned self] in
371+
try self.implementation.getItem(key: key, options: options)
363372
}
364373
}
365374

366375
public func setItem(key: String, value: String, options: StorageOptions?) throws -> Promise<Void> {
367-
return Promise.async { [weak self] in
368-
try self?.implementation.setItem(key: key, value: value, options: options)
376+
return Promise.async { [unowned self] in
377+
try self.implementation.setItem(key: key, value: value, options: options)
369378
}
370379
}
371380

372381
public func removeItem(key: String, options: StorageOptions?) throws -> Promise<Void> {
373-
return Promise.async { [weak self] in
374-
try self?.implementation.removeItem(key: key, options: options)
382+
return Promise.async { [unowned self] in
383+
try self.implementation.removeItem(key: key, options: options)
375384
}
376385
}
377386

378387
public func getAllItems(options: StorageOptions?) throws -> Promise<Dictionary<String, String>> {
379-
return Promise.async { [weak self] in
380-
try self?.implementation.getAllItems(options: options) ?? [:]
388+
return Promise.async { [unowned self] in
389+
try self.implementation.getAllItems(options: options)
381390
}
382391
}
383392

384393
public func clear(options: StorageOptions?) throws -> Promise<Void> {
385-
return Promise.async { [weak self] in
386-
try self?.implementation.clear(options: options)
394+
return Promise.async { [unowned self] in
395+
try self.implementation.clear(options: options)
387396
}
388397
}
389398

390399
// MARK: - Capability Detection
391400

392401
public func isBiometricAvailable() throws -> Promise<Bool> {
393-
return Promise.async { [weak self] in
394-
self?.implementation.isBiometricAvailable() ?? false
402+
return Promise.async { [unowned self] in
403+
self.implementation.isBiometricAvailable()
395404
}
396405
}
397406

398407
public func isStrongBoxAvailable() throws -> Promise<Bool> {
399-
return Promise.async { [weak self] in
400-
self?.implementation.isStrongBoxAvailable() ?? false
408+
return Promise.async { [unowned self] in
409+
self.implementation.isStrongBoxAvailable()
401410
}
402411
}
403412
}

nitro.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"autolinking": {
1111
"SensitiveInfo": {
12-
"swift": "SensitiveInfo",
12+
"swift": "RNSensitiveInfo",
1313
"kotlin": "SensitiveInfo"
1414
}
1515
},

0 commit comments

Comments
 (0)