Skip to content

Commit

Permalink
More resilient connection logic #2 (#31)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1204024374356703/f

* Fixes a line of code that was outdated

* Improved error handling

* Updates BSK

* Fixes a unit test

* Updates BSK

* Rolls back some unintentional changes and fixes another unit test

* Updates BSK

* Updates BSK

* Shows an error message in debug builds when there's an assertion failure in the system extension

* Fixes the merge from network-protection to no longer use BSK.
  • Loading branch information
diegoreymendez authored Mar 13, 2023
1 parent 52c9bd0 commit 48e5f3c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11814,7 +11814,7 @@
repositoryURL = "https://github.com/duckduckgo/TrackerRadarKit.git";
requirement = {
kind = revision;
revision = 2fe5b26fd356e3b81fb967ae8e600fd062ca7655;
revision = b01176bccceed114feabd58e753e62ef325e19e8;
};
};
3706FA6E293F65D500E42796 /* XCRemoteSwiftPackageReference "lottie-ios" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,39 @@ public class NetworkProtectionKeychainStore: NetworkProtectionKeyStore {

struct Constants {
static let defaultServiceName = "DuckDuckGo Network Protection Private Key"
static let privateKeyName = "DuckDuckGo Network Protection Private Key"
}

enum UserDefaultKeys {
static let currentPublicKey = "com.duckduckgo.network-protection.NetworkProtectionKeychainStore.UserDefaultKeys.currentPublicKeyBase64"
}

private let serviceName: String
private let useSystemKeychain: Bool
private let userDefaults: UserDefaults
private let errorEvents: EventMapping<NetworkProtectionError>?

public convenience init(useSystemKeychain: Bool, errorEvents: EventMapping<NetworkProtectionError>?) {
self.init(serviceName: Constants.defaultServiceName, useSystemKeychain: useSystemKeychain, errorEvents: errorEvents)
}
public init(serviceName: String? = nil,
useSystemKeychain: Bool,
userDefaults: UserDefaults = .standard,
errorEvents: EventMapping<NetworkProtectionError>?) {

init(serviceName: String, useSystemKeychain: Bool, errorEvents: EventMapping<NetworkProtectionError>?) {
self.serviceName = serviceName
self.serviceName = serviceName ?? Constants.defaultServiceName
self.useSystemKeychain = useSystemKeychain
self.userDefaults = userDefaults
self.errorEvents = errorEvents
}

// MARK: - NetworkProtectionKeyStore

/// Retrieves the stored private key without generating one if it doesn't exist.
///
public func storedPrivateKey() throws -> PrivateKey? {
public func storedPrivateKey() throws -> PrivateKey? {
guard let currentPublicKey = currentPublicKey else {
return nil
}

do {
guard let data = try readData(named: Constants.privateKeyName) else {
guard let data = try readData(named: currentPublicKey) else {
return nil
}

Expand All @@ -103,11 +112,13 @@ public class NetworkProtectionKeychainStore: NetworkProtectionKeyStore {
handle(error)
// Intentionally not re-throwing
}

let generatedKey = PrivateKey()
let base64PublicKey = generatedKey.publicKey.base64Key
currentPublicKey = base64PublicKey

do {
try writeData(generatedKey.rawValue, named: Constants.privateKeyName)
try writeData(generatedKey.rawValue, named: base64PublicKey)
} catch {
handle(error)
// Intentionally not re-throwing
Expand All @@ -120,7 +131,12 @@ public class NetworkProtectionKeychainStore: NetworkProtectionKeyStore {
///
public func resetCurrentKey() {
do {
try deleteEntry(named: Constants.privateKeyName)
guard let currentPublicKey = currentPublicKey else {
return
}

try deleteEntry(named: currentPublicKey)
self.currentPublicKey = nil
} catch {
handle(error)
// Intentionally not re-throwing
Expand Down Expand Up @@ -184,6 +200,26 @@ public class NetworkProtectionKeychainStore: NetworkProtectionKeyStore {
] as [String: Any]
}

// MARK: - UserDefaults

/// The currently used public key.
///
/// The key is stored in base64 representation.
///
private var currentPublicKey: String? {
get {
guard let base64Key = userDefaults.string(forKey: UserDefaultKeys.currentPublicKey) else {
return nil
}

return PublicKey(base64Key: base64Key)?.base64Key
}

set {
userDefaults.set(newValue, forKey: UserDefaultKeys.currentPublicKey)
}
}

// MARK: - EventMapping

private func handle(_ error: Error) {
Expand Down

0 comments on commit 48e5f3c

Please sign in to comment.