-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
246 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// | ||
// ReturnUserMeasurement.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import BrowserServicesKit | ||
|
||
protocol ReturnUserMeasurement { | ||
|
||
var isReturningUser: Bool { get } | ||
func installCompletedWithATB(_ atb: Atb) | ||
func updateStoredATB(_ atb: Atb) | ||
|
||
} | ||
|
||
class KeychainReturnUserMeasurement: ReturnUserMeasurement { | ||
|
||
static let SecureATBKeychainName = "returning-user-atb" | ||
|
||
struct Measurement { | ||
|
||
let oldATB: String? | ||
let newATB: String | ||
|
||
} | ||
|
||
/// Called from the `VariantManager` to determine which variant to use | ||
var isReturningUser: Bool { | ||
return hasAnyKeychainItems() | ||
} | ||
|
||
func installCompletedWithATB(_ atb: Atb) { | ||
if let oldATB = readSecureATB() { | ||
sendReturnUserMeasurement(oldATB, atb.version) | ||
} | ||
writeSecureATB(atb.version) | ||
} | ||
|
||
/// Update the stored ATB with an even more generalised version of the ATB, if present. | ||
func updateStoredATB(_ atb: Atb) { | ||
guard let atb = atb.updateVersion else { return } | ||
writeSecureATB(atb) | ||
} | ||
|
||
private func writeSecureATB(_ atb: String) { | ||
let data = atb.data(using: .utf8)! | ||
|
||
var query: [String: Any] = [ | ||
kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: Self.SecureATBKeychainName, | ||
kSecValueData as String: data, | ||
|
||
// We expect to only need access when the app is in the foreground and we want it to be migrated to new devices. | ||
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked, | ||
|
||
// Just to be explicit that we don't want this stored in the cloud | ||
kSecAttrSynchronizable as String: false | ||
] | ||
|
||
var status = SecItemAdd(query as CFDictionary, nil) | ||
if status == errSecDuplicateItem { | ||
let attributesToUpdate: [String: Any] = [ | ||
kSecValueData as String: data | ||
] | ||
query.removeValue(forKey: kSecValueData as String) | ||
status = SecItemUpdate(query as CFDictionary, attributesToUpdate as CFDictionary) | ||
if status != errSecSuccess { | ||
fireDebugPixel(.debugReturnUserUpdateATB, errorCode: status) | ||
} | ||
} else if status != errSecSuccess { | ||
fireDebugPixel(.debugReturnUserAddATB, errorCode: status) | ||
} | ||
|
||
} | ||
|
||
private func readSecureATB() -> String? { | ||
let query: [String: Any] = [ | ||
kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: Self.SecureATBKeychainName, | ||
kSecReturnData as String: kCFBooleanTrue!, | ||
kSecMatchLimit as String: kSecMatchLimitOne | ||
] | ||
|
||
var dataTypeRef: AnyObject? | ||
let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef) | ||
if ![errSecSuccess, errSecItemNotFound].contains(status) { | ||
fireDebugPixel(.debugReturnUserReadATB, errorCode: status) | ||
} | ||
|
||
if let data = dataTypeRef as? Data { | ||
return String(data: data, encoding: .utf8) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private func sendReturnUserMeasurement(_ oldATB: String, _ newATB: String) { | ||
Pixel.fire(pixel: .returnUser, withAdditionalParameters: [ | ||
PixelParameters.returnUserOldATB: oldATB, | ||
PixelParameters.returnUserNewATB: newATB | ||
]) | ||
} | ||
|
||
private func fireDebugPixel(_ event: Pixel.Event, errorCode: OSStatus) { | ||
Pixel.fire(pixel: event, withAdditionalParameters: [ | ||
PixelParameters.returnUserErrorCode: "\(errorCode)" | ||
]) | ||
} | ||
|
||
/// Only check for keychain items created by *this* app. | ||
private func hasAnyKeychainItems() -> Bool { | ||
let possibleStorageClasses = [ | ||
kSecClassGenericPassword, | ||
kSecClassKey | ||
] | ||
return possibleStorageClasses.first(where: hasKeychainItemsInClass(_:)) != nil | ||
} | ||
|
||
private func hasKeychainItemsInClass(_ secClassCFString: CFString) -> Bool { | ||
let query: [String: Any] = [ | ||
kSecClass as String: secClassCFString, | ||
kSecMatchLimit as String: kSecMatchLimitOne, | ||
kSecReturnAttributes as String: true, // Needs to be true or returns nothing. | ||
kSecReturnRef as String: true, | ||
] | ||
var returnArrayRef: CFTypeRef? | ||
let status = SecItemCopyMatching(query as CFDictionary, &returnArrayRef) | ||
guard status == errSecSuccess, | ||
let returnArray = returnArrayRef as? [String: Any] else { | ||
return false | ||
} | ||
return returnArray.count > 0 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.