在Mac OS X上创建“Web Form Password”类型的Keychain Item

时间:2011-10-20 16:01:56

标签: objective-c macos keychain

我正在开发一个应用程序,当用户选择菜单项时,他们会被带到网页。该网页需要身份验证,并且为了让用户更加简单,我希望将他们的身份验证信息传递给存储在我的应用程序中的Safari / Firefox / Chrome。

我尝试过创建通用和互联网钥匙串项目,这些项目在Keychain Access中显示得非常漂亮,但没有网络浏览器可以接收它们。

我注意到为浏览器存储的钥匙串项目具有“Web表单密码”类型。

当我尝试使用“kSecAuthenticationTypeHTMLForm”类型创建一个钥匙串项时,它在Keychain Access中显示为“互联网密码”。我修改了EMKeychain类中的一些代码:

+ (EMInternetKeychainItem *)addInternetKeychainItemForServer:(NSString *)server
                                            withUsername:(NSString *)username
                                                password:(NSString *)password
                                                    path:(NSString *)path
                                                    port:(NSInteger)port
                                                protocol:(SecProtocolType)protocol
{
if (!username || !server || !password)
    return nil;

const char *serverCString = [server UTF8String];
const char *usernameCString = [username UTF8String];
const char *passwordCString = [password UTF8String];
const char *pathCString = [path UTF8String];

if (!path || [path length] == 0)
    pathCString = "";

SecKeychainItemRef item = nil;
OSStatus returnStatus = SecKeychainAddInternetPassword(NULL, strlen(serverCString), serverCString, 0, NULL, strlen(usernameCString), usernameCString, strlen(pathCString), pathCString, port, protocol, kSecAuthenticationTypeHTMLForm, strlen(passwordCString), (void *)passwordCString, &item);

if (returnStatus != noErr || !item)
{
    if (_logsErrors)
        NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
    return nil;
}
return [EMInternetKeychainItem _internetKeychainItemWithCoreKeychainItem:item forServer:server username:username password:password path:path port:port protocol:protocol];
}

1 个答案:

答案 0 :(得分:10)

最可能的问题是钥匙串条目是使用ACL创建的,该ACL不允许访问Safari的内容。 (我认为Chrome和Firefox使用自己的专有密码数据库而不是密钥链,因此修改密钥链不会影响这些浏览器。)

尝试使用SecKeychainItemSetAccess允许访问所有应用程序。我使用以下代码创建这样一个允许的SecAccess对象:

// Create an access object.
SecAccessRef access;
status = SecAccessCreate(CFSTR("item description"), 
                         NULL, // Only this app has access (this'll get changed in a moment)
                         &access);
if (status) { ... }

// Override access control to provide full access to all applications.
NSArray *aclList = nil;
status = SecAccessCopyACLList(access, (CFArrayRef *)&aclList);
if (status) { ... }
for (id object in aclList) { // will do just one iteration
    SecACLRef acl = (SecACLRef)object;

    CFArrayRef applist = NULL;
    CFStringRef desc = NULL;
    CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR cakps;

    status = SecACLCopySimpleContents(acl, &applist, &desc, &cakps);
    if (status)  { ... }

    status = SecACLSetSimpleContents(acl, 
                                     NULL, // All applications.
                                     desc,
                                     &cakps);
    if (status) { ... }

    if (applist != NULL)
        CFRelease(applist);
    if (desc != NULL)
        CFRelease(desc);
}