首页 > 解决方案 > 为什么更新钥匙串项目密码会导致重新提示应用程序访问?

问题描述

我正在使用 macOS 钥匙串服务存储通用密码。此密码应无需系统上已有的 Apple 应用程序提示即可使用。我最初使用如下代码创建它:

itemName="Some Item"
appPath="/Applications/Some Apple.app"
security add-generic-password -a "$username" -s "$itemName" -w "$password" -T "$appPath" "$keychain"
security set-generic-password-partition-list -a "$username" -s "$itemName" -S apple: -k "$password" "$keychain"

这工作正常,应用程序可以在不提示用户许可的情况下使用它。

但是,如果我稍后去更新这个密码,当另一个应用程序需要密码时,用户会得到一个一次性提示——即使该项目的 ACL 没有更改!

例如运行代码后

private func updateAppPassword(user: String = NSUserName(), password: String) throws {
    let query: [CFString:Any] = [
        kSecClass: kSecClassGenericPassword,
        kSecAttrServer: "Some Item",
        kSecAttrAccount: user
    ]
    let newInfo: [CFString:Any] = [
        kSecValueData: password.data(using: .utf8)!
    ]
    try SecItemUpdate(query as CFDictionary, newInfo as CFDictionary).check()
}

…当用户启动使用密码的 Apple 应用程序时,他们必须给予许可。

这并不 100% 令人惊讶,因为我确实更改了项目,但我不知道为什么更新它应该很重要,特别是因为我在创建权限时就保留了权限——并且在最初创建钥匙串项目之后没有提示!

特别奇怪的是,在我运行代码更新密码之前之后security dump-keychain -a,我从我的测试钥匙串中得到以下信息:

keychain: "/Users/me/Library/Keychains/dev-test.keychain-db"
version: 512
class: "genp"
attributes:
    0x00000007 <blob>="Some Item"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="vand065"
    "cdat"<timedate>=0x32303138303730353139303835385A00  "20180705190858Z\000"
    "crtr"<uint32>=<NULL>
    "cusi"<sint32>=<NULL>
    "desc"<blob>=<NULL>
    "gena"<blob>=<NULL>
    "icmt"<blob>=<NULL>
    "invi"<sint32>=<NULL>
    "mdat"<timedate>=0x32303138303730353139313035305A00  "20180705191050Z\000"
    "nega"<sint32>=<NULL>
    "prot"<blob>=<NULL>
    "scrp"<sint32>=<NULL>
    "svce"<blob>="Some Item"
    "type"<uint32>=<NULL>
access: 5 entries
    entry 0:
        authorizations (6): decrypt derive export_clear export_wrapped mac sign
        don't-require-password
        description: Some Item
        applications (1):
            0: /Applications/Some Apple.app (status -2147415734)
    entry 1:
        authorizations (1): encrypt
        don't-require-password
        description: Some Item
        applications: <null>
    entry 2:
        authorizations (1): integrity
        don't-require-password
        description: 53f29c48f37f1d8993800d34b13495e926a1e8f64121c2f7e7a6d23128d1bb73
        applications: <null>
    entry 3:
        authorizations (1): partition_id
        don't-require-password
        description: apple:
        applications: <null>
    entry 4:
        authorizations (1): change_acl
        don't-require-password
        description: Some Item
        applications (0):

即,密码更新似乎没有对钥匙串项或其 ACL 进行任何更改。那么为什么用户必须再次授予“/Applications/Some Apple.app”权限才能使用更新后的密码呢?

标签: macossecuritykeychain

解决方案


推荐阅读