Swift 如何(正确)使用cocoa安全框架

Swift 如何(正确)使用cocoa安全框架,swift,security,cocoa,Swift,Security,Cocoa,我正在尝试使用苹果提供的安全框架来授权我的应用程序的用户。我使用以下规则创建了三个权限。右侧1的超时值为0,其他1的超时值为600 [ kAuthorizationRightRule: kAuthorizationRuleAuthenticateAsSessionUser, "key": "my.right.name", "shared": false, "timeout": 0 // Either 0 or 600 ] 第一次通过SFAuthorization

我正在尝试使用苹果提供的安全框架来授权我的应用程序的用户。我使用以下规则创建了三个权限。右侧1的超时值为0,其他1的超时值为600

[
    kAuthorizationRightRule: kAuthorizationRuleAuthenticateAsSessionUser,
    "key": "my.right.name",
    "shared": false,
    "timeout": 0 // Either 0 or 600
]
第一次通过SFAuthorization.get获得权限1时,将显示一个提示。当我再次尝试获取密码时,我希望再次被提示输入密码,但它会立即被授予。即使我试图获得权限2或3,在授予权限1后也不会再显示任何提示

我试图通过谷歌找到关于这个框架的信息,因为苹果的文档并没有真正的帮助,但没有成功

感谢您的帮助

编辑

我的代码(简化):

和正确的枚举:

enum Right {
        case right1
        case right2
        case right3

        static let all: [Right] = [.right1, .right2, .right3]

        private var data: (name: String, timeout: Int) {
            switch self {
            case .right1:
                return ("right.name.one", 0)
            case .right2:
                return ("right.name.two", 600)
            case .right3:
                return ("right.name.three", 600)
            }
        }

        fileprivate var name: UnsafePointer<Int8> {
            return (self.data.name as NSString).utf8String!
        }

        fileprivate var description: CFString {
            let description: String
            switch self {
            case .right1:
                description = "First right description"
            case .right2:
                description = "Second right description"
            case .right3:
                description = "Third right description"
            }
            return description as CFString
        }

        fileprivate var rule: CFDictionary {
            return [
                kAuthorizationRightRule: kAuthorizationRuleAuthenticateAsSessionUser,
                "key": self.data.name,
                "shared": false,
                "timeout": self.data.timeout
            ] as CFDictionary
        }

        fileprivate var authItem: AuthorizationItem {
            return AuthorizationItem(name: self.name, valueLength: 0, value: nil, flags: 0)
        }

    }
enum Right{
案例权利1
案例权利2
案例权利3
静态let all:[正确]=[.right1、.right2、.right3]
私有变量数据:(名称:字符串,超时:Int){
切换自身{
案例1:
return(“right.name.one”,0)
案例2:
return(“right.name.two”,600)
案例3:
return(“right.name.three”,600)
}
}
fileprivate变量名称:UnsafePointer{
返回(self.data.name为NSString).utf8String!
}
fileprivate变量说明:CFString{
let description:字符串
切换自身{
案例1:
description=“右一描述”
案例2:
description=“右二描述”
案例3:
description=“右三描述”
}
将描述返回为CFString
}
fileprivate var规则:CFDictionary{
返回[
kAuthorizationRightRule:KauthorizationRuleAuthenticateSessionUser,
“key”:self.data.name,
“共享”:错误,
“超时”:self.data.timeout
]作为词典
}
fileprivate var authItem:AuthorizationItem{
返回授权项(名称:self.name,valueLength:0,值:nil,标志:0)
}
}

事实证明,设置
kAuthorizationRightRule
属性会导致忽略其他属性。经过进一步研究,我发现使用以下字典作为规则会得到与
kauthorizationruleauthenticatessessionuser
相同的结果,但设置了超时:

[
  "key": "my.right.name",
  "allow-root": false,
  "authenticate-user": true,
  "class": "user",
  "session-owner": true,
  "shared": false,
  "timeout": 0
]

此外,为了防止其他权限不显示提示,应使用多个SFAuthorization对象。

结果表明,设置
kAuthorizationRightRule
属性会导致忽略其他属性。经过进一步研究,我发现使用以下字典作为规则会得到与
kauthorizationruleauthenticatessessionuser
相同的结果,但设置了超时:

[
  "key": "my.right.name",
  "allow-root": false,
  "authenticate-user": true,
  "class": "user",
  "session-owner": true,
  "shared": false,
  "timeout": 0
]

此外,为了防止其他权限不显示提示,应使用多个SFAuthorization对象。

我认为这就是授权的工作方式。否则,您可以考虑API文档中建议的其他框架。@多米尼克布切尔,基于文档的状态,我认为至少0的超时将导致每次提示。为了获得用户每次都必须进行身份验证或同意的最大安全性,请将超时设置为0。我从未使用过此API,但您是否每次都创建新的securitySession?似乎在做任何事情之前,创建一个新的是至关重要的。。。你能提供你实现的算法吗?@DominikBucher我已经用我的implementation@DominikBucher创建新的安全会话是什么意思?我在文件中找不到这一点。我想这就是授权的工作方式。否则,您可以考虑API文档中建议的其他框架。@多米尼克布切尔,基于文档的状态,我认为至少0的超时将导致每次提示。为了获得用户每次都必须进行身份验证或同意的最大安全性,请将超时设置为0。我从未使用过此API,但您是否每次都创建新的securitySession?似乎在做任何事情之前,创建一个新的是至关重要的。。。你能提供你实现的算法吗?@DominikBucher我已经用我的implementation@DominikBucher创建新的安全会话是什么意思?我在文件里找不到。。