0

当我们尝试连接 MacCatalyst 时,系统会在每次连接时要求输入密码,但在处理仅为 MacOS 创建的项目时,它会直接连接而无需输入密码。KeychainWrapper 类中“set”函数的输出对于两个项目是相同的,但是当我比较钥匙串访问中的两个键时,附件和我的代码中显示了差异

钥匙串截图

这是我的 IKEv2 连接代码:

public func connectIKEv2(config: Configuration, onError: @escaping (String)->Void) {
    let p = NEVPNProtocolIKEv2()

    p.authenticationMethod = NEVPNIKEAuthenticationMethod.none
    p.deadPeerDetectionRate = NEVPNIKEv2DeadPeerDetectionRate.medium
    p.disableRedirect = false
    p.enableRevocationCheck = false
    p.enablePFS = false
    p.useExtendedAuthentication = true
    p.remoteIdentifier = config.server
    p.useConfigurationAttributeInternalIPSubnet = false
    p.serverAddress = config.server
    p.username = config.account
    p.passwordReference = config.getPasswordRef()
    
    loadProfile { _ in
        self.manager.protocolConfiguration = p
        self.manager.onDemandRules = [NEOnDemandRuleConnect()]
        self.manager.isOnDemandEnabled = true

        self.manager.isEnabled = true
        self.saveProfile { success in
            if !success {
                onError("Unable to save vpn profile")
                return
            }
            else {
                print("Mayank: Profile saved")
            }
            self.loadProfile() { success in
                if !success {
                    onError("Unable to load profile")
                    return
                }
                let result = self.startVPNTunnel()
                if !result {
                    onError("Can't connect")
                }
                else {
                    print("Mayank: connecting with result")
                    print(result)

                }
            }
        }
    }
}

这是 KeychainWrapper 设置功能:

@discardableResult open func set(_ value: String, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool {
    if let data = value.data(using: .utf8) {
        return set(data, forKey: key, withAccessibility: accessibility)
    } else {
        return false
    }
}

@discardableResult open func set(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool {
    var keychainQueryDictionary: [String:Any] = setupKeychainQueryDictionary(forKey: key, withAccessibility: accessibility)
    keychainQueryDictionary[SecValueData] = value
    if let accessibility = accessibility {
        keychainQueryDictionary[SecAttrAccessible] = accessibility.keychainAttrValue
    } else {
        keychainQueryDictionary[SecAttrAccessible] = KeychainItemAccessibility.whenUnlocked.keychainAttrValue
    }
    let status: OSStatus = SecItemAdd(keychainQueryDictionary as CFDictionary, nil)
    if status == errSecSuccess {
        return true
    } else if status == errSecDuplicateItem {
        return update(value, forKey: key, withAccessibility: accessibility)
    } else {
        return false
    }
}

private func update(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool {
    var keychainQueryDictionary: [String:Any] = setupKeychainQueryDictionary(forKey: key, withAccessibility: accessibility)
    let updateDictionary = [SecValueData:value]
    if let accessibility = accessibility {
        keychainQueryDictionary[SecAttrAccessible] = accessibility.keychainAttrValue
    }
    let status: OSStatus = SecItemUpdate(keychainQueryDictionary as CFDictionary, updateDictionary as CFDictionary)
    if status == errSecSuccess {
        return true
    } else {
        return false
    }
}
4

0 回答 0