授权会话始终在帮助程序

时间:2015-11-27 20:12:04

标签: objective-c authorization

我有一个带有特权帮助工具的Objective-C应用程序(https://github.com/NBICreator/NBICreator)。

我在一次构建期间需要执行一些不同的特权任务,我希望用户只进行一次身份验证才能执行这些任务。

授权有效,但我似乎无法在帮助程序中重用会话。即使我向安全管理服务器提供完全相同的权限并使用相同的 AuthenticationRef ,用户也必须对每个步骤进行身份验证。

我已经阅读了文档并首先在主应用程序中测试了预身份验证方法,并尝试打印出来(并在helper中保留auth会话)。但我尝试过的任何事情都没有成功。

我需要帮助找出安全管理服务器为什么需要重新进行身份验证。

Master Branch中GitHub上的代码是最新的,我通过来回改变来尝试和测试。使用该代码,即使我使用相同的身份验证,用户也必须在每次调用帮助程序函数时进行身份验证。

这是授权数据库中的权利:

class = rule;
    created = "470329367.933364";
    "default-prompt" =     {
        "" = "NBICreator is trying to start an Imagr workflow.";
    };
    identifier = "com.github.NBICreator";
    modified = "470329367.933364";
    requirement = "identifier \"com.github.NBICreator\" and anchor apple generic and certificate leaf[subject.CN] = \"Mac Developer: Erik Berglund (BXUF2UUW7E)\" and certificate 1[field.1.2.840.113635.100.6.2.1] /* exists */";
    rule =     (
        "authenticate-admin"
    );
    version = 0;

这是我定义权利的地方:https://github.com/NBICreator/NBICreator/blob/master/NBICreator/Helper/NBCHelperAuthorization.m#L36-L43

NSStringFromSelector(@selector(authorizeWorkflowImagr:withReply:)) : @{
                             kCommandKeyAuthRightName    : @"com.github.NBICreator.workflowImagr",
                             kCommandKeyAuthRightDefault : @kAuthorizationRuleAuthenticateAsAdmin,
                             kCommandKeyAuthRightDesc    : NSLocalizedString(
                                                                             @"NBICreator is trying to start an Imagr workflow.",
                                                                             @"prompt shown when user is required to authorize to add a user"
                                                                             )
                             },

这是我检查用户是否经过身份验证的地方: https://github.com/NBICreator/NBICreator/blob/master/NBICreator/Helper/NBCHelperAuthorization.m#L222-L253

+ (NSError *)checkAuthorization:(NSData *)authData command:(SEL)command authRef:(AuthorizationRef)authRef {
#pragma unused(authData)
NSError *           error;
OSStatus            err = 0;

AuthorizationItem   oneRight = { NULL, 0, NULL, 0 };
AuthorizationRights rights   = { 1, &oneRight };

oneRight.name = [@"com.github.NBICreator.workflowImagr" UTF8String];

err = AuthorizationCopyRights(
                              authRef,
                              &rights,
                              NULL,
                              kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed,
                              NULL
                              );

if ( err != errAuthorizationSuccess ) {
    NSString *message = CFBridgingRelease(SecCopyErrorMessageString(err, NULL));
    error = [NSError errorWithDomain:[[NSProcessInfo processInfo] processName] code:err userInfo:@{ NSLocalizedDescriptionKey : message }];
}

    return error;
}

正如您在那里看到的那样,我通过设置一个硬编码的正确名称来尝试将该权限恢复到安全管理服务器。

我现在很难过,似乎无法找到前进的方向。希望有人在这里可能会知道在哪里看。

1 个答案:

答案 0 :(得分:0)

我找到了如何在权限数据库中设置权限的答案。

我使用Apple示例EvenBetterAuthorizationExample中的默认代码来创建和使用权限。但是,只有this documentation指向不同的使用权。

这些都没有帮助我使用authenticatin一次,并且在我下次请求相同权限的身份验证时对帮助程序进行身份验证。

经过一些挖掘并查看授权数据库中的实际规则后,我找到了一种方法来复制AuthenticateWithPrivileges使用的规则,该规则通过验证5分钟直到需要重新验证。

所以,我改变了我的代码来创建自定义权利:

NSStringFromSelector(@selector(addUsersToVolumeAtPath:userShortName:userPassword:authorization:withReply:)) : @{
                             kCommandKeyAuthRightName    : NBCAuthorizationRightAddUsers,
                             kCommandKeyAuthRightDefault : @kAuthorizationRuleAuthenticateAsAdmin,
                             kCommandKeyAuthRightDesc    : NSLocalizedString(
                                                                             @"NBICreator is trying to add a user.",
                                                                             @"prompt shown when user is required to authorize to add a user"
                                                                             )
                             },

对此:

NSStringFromSelector(@selector(addUsersToVolumeAtPath:userShortName:userPassword:authorization:withReply:)) : @{
                             kCommandKeyAuthRightName    : NBCAuthorizationRightAddUsers,
                             kCommandKeyAuthRightDefault : @{
                                     @"class": @"user",
                                     @"group": @"admin",
                                     @"timeout": @(300),
                                     @"version": @(1),
                                     },
                             kCommandKeyAuthRightDesc    : NSLocalizedString(
                                                                             @"NBICreator is trying to add a user.",
                                                                             @"prompt shown when user is required to authorize to add a user"
                                                                             )
                             },

所以,我没有使用 @kAuthorizationRuleAuthenticateAsAdmin 作为RightDefault,而是传入了这个词:

@{
    @"class": @"user",
    @"group": @"admin",
    @"timeout": @(300),
    @"version": @(1),
},

之后,我的助手可以请求用户授权,然后我可以在5分钟内重新使用该会话,而无需再次询问用户。