SubmarinerApp / Submariner

A Subsonic client for macOS
https://submarinerapp.com
BSD 3-Clause "New" or "Revised" License
123 stars 3 forks source link

Update Keychain item when URL and such changes #61

Closed NattyNarwhal closed 1 year ago

NattyNarwhal commented 1 year ago

Do need to override setters.

Rough draft of the common func to handle this:

- (void)updateKeychainWithNewURL: (NSURL*)newURL username: (NSString*)newUsername {
    NSURL *anUrl = [NSURL URLWithString:self.url];

    NSLog(@"update internet keychain");

    // get internet keychain
    NSMutableDictionary *attribs = [NSMutableDictionary dictionary];
    attribs[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
    attribs[(__bridge id)kSecAttrServer] = anUrl.host;
    attribs[(__bridge id)kSecAttrAccount] = self.username;
    attribs[(__bridge id)kSecAttrPath] = @"/";
    attribs[(__bridge id)kSecAttrPort] = [anUrl portWithHTTPFallback];
    attribs[(__bridge id)kSecAttrProtocol] = [anUrl keychainProtocol];

    NSMutableDictionary *newAttributes = [NSMutableDictionary dictionary];
    newAttributes[(__bridge id)kSecAttrServer] = newURL.host;
    newAttributes[(__bridge id)kSecAttrAccount] = newUsername ?: self.username;
    newAttributes[(__bridge id)kSecAttrPort] = [newURL portWithHTTPFallback];
    newAttributes[(__bridge id)kSecAttrProtocol] = [newURL keychainProtocol];

    OSStatus ret = SecItemUpdate((__bridge CFDictionaryRef)attribs, (__bridge CFDictionaryRef)newAttributes);
    if (ret != errSecSuccess) {
        NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:ret userInfo: nil];
        [NSApp performSelectorOnMainThread:@selector(presentError:) withObject:error waitUntilDone:NO];
    }
}
NattyNarwhal commented 1 year ago

I suspect the best option is to 1. cache 2. apply changes outside of setter (i.e. only on "Save" for edit server).

Or we could apply a label of some sort?

We should also delete the keychain entry (do we?) when the server object is deleted. And worry about those objects that get created upon creating a new server.

NattyNarwhal commented 1 year ago

It might also be worth switching from internet passwords to generic, and use the server OID for caching.

NattyNarwhal commented 1 year ago

Resolved, since edit server controller for now is the bottleneck.