Skip to content

Commit

Permalink
tmp: debug
Browse files Browse the repository at this point in the history
  • Loading branch information
issammani committed Jan 21, 2025
1 parent 239fb83 commit dc81b9f
Showing 1 changed file with 83 additions and 7 deletions.
90 changes: 83 additions & 7 deletions firefox-ios/Storage/Rust/RustLogins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@ public class RustLoginEncryptionKeys {
let canary = try createCanary(text: canaryPhrase, encryptionKey: secret)

DispatchQueue.global(qos: .background).sync {
self.keychain.set(secret,
forKey: self.loginPerFieldKeychainKey,
withAccessibility: MZKeychainItemAccessibility.afterFirstUnlock)
self.keychain.set(canary,
forKey: self.canaryPhraseKey,
withAccessibility: MZKeychainItemAccessibility.afterFirstUnlock)
let result = self.keychain.set(secret,
forKey: self.loginPerFieldKeychainKey,
withAccessibility: MZKeychainItemAccessibility.afterFirstUnlock)
self.logger.log("[issam] 0 createAndStoreKey set result",
level: .warning,
category: .storage,
description: "\(result)")
let result2 = self.keychain.set(canary,
forKey: self.canaryPhraseKey,
withAccessibility: MZKeychainItemAccessibility.afterFirstUnlock)
self.logger.log("[issam] 00 createAndStoreKey set result",
level: .warning,
category: .storage,
description: "\(result2)")
}

let key = self.keychain.string(forKey: self.loginPerFieldKeychainKey)
self.logger.log("[issam] 1 createAndStoreKey",
level: .warning,
category: .storage,
description: "\(key ?? "empty")")
return secret
} catch let err as NSError {
if let loginsStoreError = err as? LoginsStoreError {
Expand Down Expand Up @@ -605,9 +619,17 @@ public class RustLogins: LoginsProtocol, KeyManager {
public func addLogin(login: LoginEntry) -> Deferred<Maybe<String>> {
let deferred = Deferred<Maybe<String>>()

self.logger.log("[issam] 0 Adding login",
level: .warning,
category: .storage,
description: "xxxx")
queue.async {
guard self.isOpen else {
let error = LoginsStoreError.UnexpectedLoginsApiError(reason: "Database is closed")
self.logger.log("[issam] 1 Adding login",
level: .warning,
category: .storage,
description: error.descriptionValue)

deferred.fill(Maybe(failure: error as MaybeErrorType))
return
Expand All @@ -620,9 +642,17 @@ public class RustLogins: LoginsProtocol, KeyManager {
let id = try self.storage?.add(login: login).id
deferred.fill(Maybe(success: id!))
} catch let err as NSError {
self.logger.log("[issam] 2 Error adding login",
level: .warning,
category: .storage,
description: err.localizedDescription)
deferred.fill(Maybe(failure: err))
}
case .failure(let err):
self.logger.log("[issam] 3 Error adding login",
level: .warning,
category: .storage,
description: err.localizedDescription)
deferred.fill(Maybe(failure: err))
}
}
Expand Down Expand Up @@ -853,12 +883,25 @@ public class RustLogins: LoginsProtocol, KeyManager {
private func getKeychainData(rustKeys: RustLoginEncryptionKeys) -> (String?, String?) {
var keychainData: (String?, String?) = (nil, nil)

self.logger.log("[issam] 1 getKeychainData",
level: .warning,
category: .storage,
description: "")

DispatchQueue.global(qos: .background).sync {
let key = rustKeys.keychain.string(forKey: rustKeys.loginPerFieldKeychainKey)
let encryptedCanaryPhrase = rustKeys.keychain.string(forKey: rustKeys.canaryPhraseKey)
keychainData = (key, encryptedCanaryPhrase)
self.logger.log("[issam] 2 getKeychainData",
level: .warning,
category: .storage,
description: "\(keychainData.0 ?? "empty")")
}

self.logger.log("[issam] 3 getKeychainData",
level: .warning,
category: .storage,
description: "\(keychainData.0 ?? "empty")")
return keychainData
}

Expand Down Expand Up @@ -937,27 +980,52 @@ public class RustLogins: LoginsProtocol, KeyManager {
// call or the key data has been cleared from the keychain.

self.hasSyncedLogins().upon { result in
self.logger.log("[issam] 0 getStoredKey",
level: .warning,
category: .storage,
description: "")
guard result.failureValue == nil else {
completion(.failure(result.failureValue! as NSError))
return
}
self.logger.log("[issam] 1 getStoredKey",
level: .warning,
category: .storage,
description: "")

guard let hasLogins = result.successValue else {
let msg = "Failed to verify logins count before attempting to reset key"
completion(.failure(LoginEncryptionKeyError.dbRecordCountVerificationError(msg) as NSError))
return
}

self.logger.log("[issam] 2 getStoredKey",
level: .warning,
category: .storage,
description: "")

if hasLogins {
// Since the key data isn't present and we have login records in
// the database, we both clear the database and reset the key.
GleanMetrics.LoginsStoreKeyRegeneration.keychainDataLost.record()
self.logger.log("[issam] 3 getStoredKey",
level: .warning,
category: .storage,
description: "")
self.resetLoginsAndKey(rustKeys: rustKeys, completion: completion)
} else {
// There are no records in the database so we don't need to wipe any
// existing login records. We just need to create a new key.
do {
self.logger.log("[issam] 4 getStoredKey",
level: .warning,
category: .storage,
description: "")
let key = try rustKeys.createAndStoreKey()
self.logger.log("[issam] 5 getStoredKey",
level: .warning,
category: .storage,
description: "\(key)")
completion(.success(key))
} catch let error as NSError {
completion(.failure(error))
Expand Down Expand Up @@ -998,8 +1066,16 @@ public class RustLogins: LoginsProtocol, KeyManager {
*/
public func getKey() throws -> Data {
let rustKeys = RustLoginEncryptionKeys()
let key1 = rustKeys.keychain.string(forKey: rustKeys.loginPerFieldKeychainKey)
self.logger.log("[issam] 0 getKey",
level: .warning,
category: .storage,
description: "\(key1 ?? "empty")")
let (key, _) = getKeychainData(rustKeys: rustKeys)

self.logger.log("[issam] 1 getKey",
level: .warning,
category: .storage,
description: "\(key ?? "empty")")
guard let keyData = key?.data(using: .utf8) else {
throw LoginsStoreError.MissingKey
}
Expand Down

0 comments on commit dc81b9f

Please sign in to comment.