Skip to content

Commit

Permalink
MPPasswordInputController completion callback refactoring
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Julius Zint committed Feb 14, 2021
1 parent 700dd43 commit 51bdf12
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
6 changes: 3 additions & 3 deletions MacPass/MPDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions MacPass/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ - (void)_mergeWithContentsFromURL:(NSURL *)url key:(KPKCompositeKey *)key option
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;
Expand Down Expand Up @@ -501,7 +501,7 @@ - (void)_lockDatabaseForDocument:(NSDocument *)document didSave:(BOOL)didSave co
}


- (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;

Expand Down
5 changes: 2 additions & 3 deletions MacPass/MPDocumentWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,11 @@ - (void)showPasswordInputWithMessage:(NSString *)message {
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];
}

Expand Down
3 changes: 2 additions & 1 deletion MacPass/MPPasswordInputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
//

#import "MPViewController.h"
#import "KeePassKit/KeePassKit.h"

@class KPKCompositeKey;

@interface MPPasswordInputController : MPViewController <NSTouchBarDelegate>

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;

Expand Down
13 changes: 6 additions & 7 deletions MacPass/MPPasswordInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ - (void)requestPasswordWithMessage:(NSString *)message cancelLabel:(NSString *)c
[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) {
Expand Down Expand Up @@ -147,7 +143,10 @@ - (IBAction)_submit:(id)sender {
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];
Expand Down Expand Up @@ -412,10 +411,10 @@ - (IBAction)unlockWithTouchID:(id)sender {
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

0 comments on commit 51bdf12

Please sign in to comment.