diff --git a/MacPass/Base.lproj/GeneralSettings.xib b/MacPass/Base.lproj/GeneralSettings.xib index 1b5b7173..c364ea59 100644 --- a/MacPass/Base.lproj/GeneralSettings.xib +++ b/MacPass/Base.lproj/GeneralSettings.xib @@ -1,5 +1,5 @@ - + @@ -22,11 +22,11 @@ - + - + @@ -50,14 +50,14 @@ - + - + - + @@ -69,7 +69,7 @@ - + @@ -90,7 +90,7 @@ - + @@ -119,7 +119,7 @@ - + @@ -136,7 +136,7 @@ - + - Enabling this compromises security. If enabled, your preferences will contain mappings from database to keyfile. Once disabled, all values will be removed from the preferences. Key locations for databases without a password will not be saved. + Enabling this compromises security. If enabled, your preferences will contain mappings from database to keyfile. Key locations for databases without a password will not be saved. + @@ -169,16 +180,19 @@ + + + - + @@ -225,7 +239,7 @@ - + diff --git a/MacPass/MPAppDelegate.h b/MacPass/MPAppDelegate.h index fcbab992..f5f87874 100644 --- a/MacPass/MPAppDelegate.h +++ b/MacPass/MPAppDelegate.h @@ -22,16 +22,25 @@ #import +extern NSString *const MPDidChangeStoredKeyFilesSettings; + @interface MPAppDelegate : NSObject @property (strong) IBOutlet NSWindow *passwordCreatorWindow; @property (strong) IBOutlet NSWindow *welcomeWindow; @property (weak) IBOutlet NSMenuItem *saveMenuItem; +@property (nonatomic, assign) BOOL isAllowedToStoreKeyFile; - (IBAction)showPreferences:(id)sender; - (IBAction)showPasswordCreator:(id)sender; - (IBAction)createNewDatabase:(id)sender; - (IBAction)openDatabase:(id)sender; +/** + * Clears the stored key files for any documents. + * @param sender sender of this action + */ +- (IBAction)clearRememberdKeyFiles:(id)sender; + - (NSString *)applicationName; - (void)lockAllDocuments; diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index b53527ca..a2e65eb9 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -36,6 +36,8 @@ #import "MPDocument.h" #import "KPKCompositeKey.h" +NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDidChangeStoredKeyFilesSettings"; + @interface MPAppDelegate () { @private MPServerDaemon *serverDaemon; @@ -63,10 +65,34 @@ [[NSNotificationCenter defaultCenter] removeObserver:self]; } -- (void)awakeFromNib { - [[self.saveMenuItem menu] setDelegate:self]; +#pragma mark Properties +- (void)setIsAllowedToStoreKeyFile:(BOOL)isAllowedToStoreKeyFile { + if(_isAllowedToStoreKeyFile != isAllowedToStoreKeyFile) { + _isAllowedToStoreKeyFile = isAllowedToStoreKeyFile; + /* cleanup on disable */ + if(!self.isAllowedToStoreKeyFile) { + [self clearRememberdKeyFiles:nil]; + } + /* Inform anyone that might be interested that we can now no longer/ or can use keyfiles */ + [[NSNotificationCenter defaultCenter] postNotificationName:MPDidChangeStoredKeyFilesSettings object:self]; + } } +- (void)awakeFromNib { + _isAllowedToStoreKeyFile = NO; + /* Update the … at the save menu */ + [[self.saveMenuItem menu] setDelegate:self]; + + /* We want to inform anyone about the changes to keyFile remmebering */ + [self bind:@"isAllowedToStoreKeyFile" + toObject:[NSUserDefaultsController sharedUserDefaultsController] + withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyRememberKeyFilesForDatabases] + options:nil]; +} + +#pragma mark - +#pragma mark NSApplicationDelegate + - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag { if(!flag) { BOOL reopen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch]; @@ -123,6 +149,18 @@ return [[NSBundle mainBundle] infoDictionary][@"CFBundleName"]; } +#pragma mark - +#pragma mark NSMenuDelegate +- (void)menuNeedsUpdate:(NSMenu *)menu { + if([self.saveMenuItem menu] != menu) { + return; // wrong menu + } + MPDocument *document = [[NSDocumentController sharedDocumentController] currentDocument]; + BOOL displayDots = (document.fileURL == nil || !document.compositeKey.hasPasswordOrKeyFile); + NSString *saveTitle = displayDots ? NSLocalizedString(@"SAVE_WITH_DOTS", "") : NSLocalizedString(@"SAVE", ""); + [self.saveMenuItem setTitle:saveTitle]; +} + #pragma mark - #pragma mark Actions - (void)showPreferences:(id)sender { @@ -164,6 +202,12 @@ } } +- (void)clearRememberdKeyFiles:(id)sender { + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kMPSettingsKeyRememeberdKeysForDatabases]; +} + +#pragma mark - +#pragma mark Private Helper - (void)_applicationDidFinishRestoringWindows:(NSNotification *)notification { NSDocumentController *documentController = [NSDocumentController sharedDocumentController]; NSArray *documents = [documentController documents]; @@ -206,14 +250,4 @@ return isFileURL; } -#pragma mark NSMenuDelegate -- (void)menuNeedsUpdate:(NSMenu *)menu { - if([self.saveMenuItem menu] != menu) { - return; // wrong menu - } - MPDocument *document = [[NSDocumentController sharedDocumentController] currentDocument]; - BOOL displayDots = (document.fileURL == nil || !document.compositeKey.hasPasswordOrKeyFile); - NSString *saveTitle = displayDots ? NSLocalizedString(@"SAVE_WITH_DOTS", "") : NSLocalizedString(@"SAVE", ""); - [self.saveMenuItem setTitle:saveTitle]; -} @end diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 30b5aec7..9cfc7ba1 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -23,8 +23,6 @@ #import #import "KPKVersion.h" -APPKIT_EXTERN NSString *const MPDocumentDidChangeStoredKeyFilesSettings; - APPKIT_EXTERN NSString *const MPDocumentDidAddGroupNotification; APPKIT_EXTERN NSString *const MPDocumentDidRevertNotifiation; @@ -135,8 +133,19 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; - (void)deleteGroup:(KPKGroup *)group; - (void)deleteEntry:(KPKEntry *)entry; +#pragma mark Actions +/** + * Empties the Trash group. Removing all Groups and Entries inside. This aciton is not undoable + * @param sender sender + */ - (IBAction)emptyTrash:(id)sender; +/** + * Creates an Entry for a template. We assume the sender is an item, that contains a UUID as representedObject. + * + * @param sender sender, that should respond to representedObject and return an NSUUID for the template to use + */ - (IBAction)createEntryFromTemplate:(id)sender; + @end @interface MPDocument (Attachments) diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index b08dc029..dddbf246 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -21,6 +21,7 @@ // #import "MPDocument.h" +#import "MPAppDelegate.h" #import "MPDocumentWindowController.h" #import "MPDatabaseVersion.h" #import "MPIconHelper.h" @@ -41,8 +42,6 @@ #import "KPKTimeInfo.h" #import "KPKAttribute.h" -NSString *const MPDocumentDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDocumentDidChangeStoredKeyFilesSettings"; - NSString *const MPDocumentDidAddGroupNotification = @"com.hicknhack.macpass.MPDocumentDidAddGroupNotification"; NSString *const MPDocumentDidRevertNotifiation = @"com.hicknhack.macpass.MPDocumentDidRevertNotifiation"; @@ -52,11 +51,6 @@ NSString *const MPDocumentDidUnlockDatabaseNotification = @"com.hicknhack.macp NSString *const MPDocumentEntryKey = @"MPDocumentEntryKey"; NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; -typedef NS_ENUM(NSUInteger, MPAlertType) { - MPAlertTypeEmptryTrash, - MPAlertTypeDeleteTrashed -}; - @interface MPDocument () { @private BOOL _didLockFile; @@ -71,7 +65,6 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { @property (assign) BOOL readOnly; @property (strong) NSURL *lockFileURL; -@property (nonatomic, assign) BOOL isAllowedToStoreKeyFile; @property (strong) IBOutlet NSView *warningView; @property (weak) IBOutlet NSImageView *warningViewImage; @@ -118,12 +111,7 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { _encryptedData = nil; _didLockFile = NO; _readOnly = NO; - _isAllowedToStoreKeyFile = NO; self.tree = [KPKTree templateTree]; - [self bind:@"isAllowedToStoreKeyFile" - toObject:[NSUserDefaultsController sharedUserDefaultsController] - withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyRememberKeyFilesForDatabases] - options:nil]; } return self; } @@ -261,7 +249,8 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { if(isUnlocked) { [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self]; /* Make sure to only store */ - if(self.compositeKey.hasKeyFile && self.compositeKey.hasPassword && self.isAllowedToStoreKeyFile) { + MPAppDelegate *delegate = [NSApp delegate]; + if(self.compositeKey.hasKeyFile && self.compositeKey.hasPassword && delegate.isAllowedToStoreKeyFile) { [self _storeKeyURL:keyFileURL]; } } @@ -288,7 +277,8 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { } - (NSURL *)suggestedKeyURL { - if(!self.isAllowedToStoreKeyFile) { + MPAppDelegate *delegate = [NSApp delegate]; + if(!delegate.isAllowedToStoreKeyFile) { return nil; } NSDictionary *keysForFiles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases]; @@ -370,17 +360,6 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { } } -- (void)setIsAllowedToStoreKeyFile:(BOOL)isAllowedToStoreKeyFile { - if(_isAllowedToStoreKeyFile != isAllowedToStoreKeyFile) { - _isAllowedToStoreKeyFile = isAllowedToStoreKeyFile; - if(!self.isAllowedToStoreKeyFile) { - [[NSUserDefaults standardUserDefaults] removeObjectForKey:kMPSettingsKeyRememeberdKeysForDatabases]; - } - /* Inform anyone that might be interested that we can now no longer/ or can use keyfiles */ - [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidChangeStoredKeyFilesSettings object:self]; - } -} - #pragma mark Data Accesors - (KPKEntry *)findEntry:(NSUUID *)uuid { @@ -518,18 +497,24 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { [[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]]; NSWindow *window = [[self windowControllers][0] window]; - [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; + [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(_emptyTrashAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; } -- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { +- (void)_emptyTrashAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { if(returnCode == NSAlertFirstButtonReturn) { [self _emptyTrash]; } } - (void)createEntryFromTemplate:(id)sender { - NSMenuItem *item = sender; - NSUUID *entryUUID = [item representedObject]; + if(![sender respondsToSelector:@selector(representedObject)]) { + return; // sender cannot provide usefull data + } + id obj = [sender representedObject]; + if([obj isKindOfClass:[NSUUID class]]) { + return; // sender cannot provide NSUUID + } + NSUUID *entryUUID = [sender representedObject]; if(entryUUID) { KPKEntry *templateEntry = [self findEntry:entryUUID]; if(templateEntry && self.selectedGroup) { @@ -538,7 +523,6 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { [self.selectedGroup.undoManager setActionName:NSLocalizedString(@"ADD_TREMPLATE_ENTRY", "")]; } } - return; } - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { @@ -585,7 +569,8 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { if(nil == keyURL) { return; // no URL to store in the first place } - NSAssert(self.isAllowedToStoreKeyFile, @"We can only store if we are allowed to do so!"); + MPAppDelegate *delegate = [NSApp delegate]; + NSAssert(delegate.isAllowedToStoreKeyFile, @"We can only store if we are allowed to do so!"); NSMutableDictionary *keysForFiles = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases] mutableCopy]; if(nil == keysForFiles) { keysForFiles = [[NSMutableDictionary alloc] initWithCapacity:1]; diff --git a/MacPass/MPPasswordInputController.m b/MacPass/MPPasswordInputController.m index bbc50146..0e5aafc8 100644 --- a/MacPass/MPPasswordInputController.m +++ b/MacPass/MPPasswordInputController.m @@ -7,6 +7,7 @@ // #import "MPPasswordInputController.h" +#import "MPAppDelegate.h" #import "MPDocumentWindowController.h" #import "MPDocument.h" #import "MPSettingsHelper.h" @@ -41,7 +42,7 @@ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if(self) { _enablePassword = YES; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_selectKeyURL) name:MPDocumentDidChangeStoredKeyFilesSettings object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_selectKeyURL) name:MPDidChangeStoredKeyFilesSettings object:nil]; } return self; }