From 51bdf12198dad9b357ff9b8427fce5f57b7e3b40 Mon Sep 17 00:00:00 2001 From: Julius Zint Date: Sat, 22 Aug 2020 15:00:20 +0200 Subject: [PATCH] MPPasswordInputController completion callback refactoring Changed the completion callback definition to take a KPKCompositeKey pointer instead of a password string and keyfile URL. This is a intermedate step to support key files with TouchID unlock. The next step is to make KPKCompositeKey conform to the NSCoding protocol. The serialized data can then be stored instead of the password. --- MacPass/MPDocument.h | 6 +++--- MacPass/MPDocument.m | 4 ++-- MacPass/MPDocumentWindowController.m | 5 ++--- MacPass/MPPasswordInputController.h | 3 ++- MacPass/MPPasswordInputController.m | 13 ++++++------- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index ffbc6255..3930187d 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -108,13 +108,13 @@ FOUNDATION_EXPORT NSString *const MPDocumentGroupKey; /** * Decrypts the database with the given password and keyfile * - * @param password The password to unlock the db with, can be nil. This is not the same as an empty string @"" - * @param keyFileURL URL for the keyfile to use, can be nil + * @param compositeKey The CompositeKey to unlock the db. + * @param keyFileURL URL for the keyfile that was used to create the compositeKey. Can be nil. * @param error Pointer to an NSError pointer of error reporting. * * @return YES if the document was unlocked sucessfully, NO otherwise. Consult the error object for details */ -- (BOOL)unlockWithPassword:(NSString *)password keyFileURL:(NSURL *)keyFileURL error:(NSError *__autoreleasing*)error; +- (BOOL)unlockWithPassword:(KPKCompositeKey *)compositeKey keyFileURL:(NSURL *)keyFileURL error:(NSError *__autoreleasing*)error; /** * Changes the password of the database. Some sanity checks are applied and the change is aborted if the new values aren't valid * diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 602c88f0..aaf10707 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -429,7 +429,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou MPPasswordInputController *passwordInputController = [[MPPasswordInputController alloc] init]; [passwordInputController requestPasswordWithMessage:NSLocalizedString(@"EXTERN_CHANGE_OF_MASTERKEY", @"The master key was changed by an external program!") cancelLabel:NSLocalizedString(@"ABORT_MERGE_KEEP_MINE", @"Button label to abort a merge on a file with changed master key!") - completionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL didCancel, NSError *__autoreleasing *error) { + completionHandler:^BOOL(KPKCompositeKey *compositeKey, NSURL* keyURL, BOOL didCancel, NSError *__autoreleasing *error) { [self.windowForSheet endSheet:sheet returnCode:(didCancel ? NSModalResponseCancel : NSModalResponseOK)]; if(!didCancel) { NSData *keyFileData = keyURL ? [NSData dataWithContentsOfURL:keyURL] : nil; @@ -501,7 +501,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou } -- (BOOL)unlockWithPassword:(NSString *)password keyFileURL:(NSURL *)keyFileURL error:(NSError *__autoreleasing*)error{ +- (BOOL)unlockWithPassword:(KPKCompositeKey *)compositeKey keyFileURL:(NSURL *)keyFileURL error:(NSError *__autoreleasing*)error{ // TODO: Make this API asynchronous NSData *keyFileData = keyFileURL ? [NSData dataWithContentsOfURL:keyFileURL] : nil; diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index d7b2e2f4..82977db1 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -329,12 +329,11 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); if(self.document != nil) { fileURL = [self.document fileURL]; } - [self.passwordInputController requestPasswordWithMessage:message cancelLabel:nil completionHandler:^BOOL(NSString *password, NSURL *keyURL, BOOL didCancel, NSError *__autoreleasing *error) { + [self.passwordInputController requestPasswordWithMessage:message cancelLabel:nil completionHandler:^BOOL(KPKCompositeKey* compositeKey, NSURL* keyURL, BOOL didCancel, NSError *__autoreleasing *error) { if(didCancel) { return NO; } - return [((MPDocument *)self.document) unlockWithPassword:password keyFileURL:keyURL error:error]; - + return [((MPDocument *)self.document) unlockWithPassword:compositeKey keyFileURL:keyURL error:error ]; } forFile:fileURL]; } diff --git a/MacPass/MPPasswordInputController.h b/MacPass/MPPasswordInputController.h index a8e3ee62..d3f10046 100644 --- a/MacPass/MPPasswordInputController.h +++ b/MacPass/MPPasswordInputController.h @@ -21,12 +21,13 @@ // #import "MPViewController.h" +#import "KeePassKit/KeePassKit.h" @class KPKCompositeKey; @interface MPPasswordInputController : MPViewController -typedef BOOL (^passwordInputCompletionBlock)(NSString *password, NSURL *keyURL, BOOL didCancel, NSError *__autoreleasing*error); +typedef BOOL (^passwordInputCompletionBlock)(KPKCompositeKey *key, NSURL* keyFileURL, BOOL didCancel, NSError *__autoreleasing*error); - (void)requestPasswordWithMessage:(NSString *)message cancelLabel:(NSString *)cancelLabel completionHandler:(passwordInputCompletionBlock)completionHandler forFile:(NSURL*) fileURL; diff --git a/MacPass/MPPasswordInputController.m b/MacPass/MPPasswordInputController.m index d1b1b9f5..6630fe02 100644 --- a/MacPass/MPPasswordInputController.m +++ b/MacPass/MPPasswordInputController.m @@ -115,10 +115,6 @@ static NSMutableDictionary* touchIDSecuredPasswords; [self _reset]; } -- (void)requestPasswordWithCompletionHandler:(passwordInputCompletionBlock)completionHandler { - [self requestPasswordWithMessage:nil cancelLabel:nil completionHandler:completionHandler forFile:nil]; -} - #pragma mark Properties - (void)setEnablePassword:(BOOL)enablePassword { if(_enablePassword != enablePassword) { @@ -147,7 +143,10 @@ static NSMutableDictionary* touchIDSecuredPasswords; NSString *password = self.enablePassword ? self.passwordTextField.stringValue : nil; BOOL cancel = (sender == self.cancelButton); - BOOL result = self.completionHandler(password, self.keyPathControl.URL, cancel, &error); + NSURL* keyURL = self.keyPathControl.URL; + NSData *keyFileData = keyURL ? [NSData dataWithContentsOfURL:keyURL] : nil; + KPKCompositeKey *compositeKey = [[KPKCompositeKey alloc] initWithPassword:password keyFileData:keyFileData]; + BOOL result = self.completionHandler(compositeKey, keyURL, cancel, &error); if(cancel || result) { if(result && self.keyPathControl.URL == nil && self.touchIdEnabled.state) { [self _storePasswordForTouchIDUnlock:password forDatabase:self.absoluteURLString]; @@ -412,10 +411,10 @@ static NSMutableDictionary* touchIDSecuredPasswords; NSString* password = [self _loadPasswordForTochIDUnlock:self.absoluteURLString]; if(password != nil) { NSError* error; - self.completionHandler(password, nil, false, &error); + KPKCompositeKey *compositeKey = [[KPKCompositeKey alloc] initWithPassword:password keyFileData:nil]; + self.completionHandler(compositeKey, nil, false, &error); [self _showError:error]; } } - @end