From 4beb950c5b782433645400c7702c6bb24a6f811f Mon Sep 17 00:00:00 2001 From: georgesnow Date: Fri, 4 Oct 2019 10:09:28 -0400 Subject: [PATCH] fixed password input controller for unlocking the database --- MacPass/MPPasswordEditWindowController.m | 8 ++-- MacPass/MPPasswordInputController.m | 60 ++++++++++++++++++++++++ MacPass/MPSettingsHelper.m | 2 +- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/MacPass/MPPasswordEditWindowController.m b/MacPass/MPPasswordEditWindowController.m index 5708d8724..03e51cbc4 100644 --- a/MacPass/MPPasswordEditWindowController.m +++ b/MacPass/MPPasswordEditWindowController.m @@ -160,7 +160,7 @@ -(void) _askForTouchID:(NSString*)password { NSError *authError = nil; LAContext *myContext = [LAContext new]; if (@available(macOS 10.12.2, *)) { - if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) { +// if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) { NSAlert *alert = [NSAlert new]; [alert addButtonWithTitle:@"Yes"]; @@ -176,7 +176,7 @@ -(void) _askForTouchID:(NSString*)password { NSLog(@"User denied Touch ID. Deleting password from keychain."); [self _deletePasswordFromKeychain]; } - } +// } } else { // Fallback on earlier versions } @@ -184,7 +184,7 @@ -(void) _askForTouchID:(NSString*)password { - (void) _savePasswordInKeychain:(NSString*)password { MPDocument *document = self.document; //not sure if this is the UUID or not? - NSString *dbName = document.compositeKey.attributeKeys.firstObject; + NSString *dbName = document.displayName; NSError *error = nil; // KeychainPasswordItem *passwordItem = [[KeychainPasswordItem alloc] initWithService:@"MacPass" account:dbName accessGroup:nil]; @@ -202,7 +202,7 @@ - (void) _savePasswordInKeychain:(NSString*)password { - (void) _deletePasswordFromKeychain { MPDocument *document = self.document; //not sure if this is the UUID or not? - NSString *dbName = document.compositeKey.attributeKeys.firstObject; + NSString *dbName = document.displayName; NSError *error = nil; diff --git a/MacPass/MPPasswordInputController.m b/MacPass/MPPasswordInputController.m index 024386649..12b7ca79d 100644 --- a/MacPass/MPPasswordInputController.m +++ b/MacPass/MPPasswordInputController.m @@ -31,6 +31,8 @@ #import "HNHUi/HNHUi.h" #import "NSError+Messages.h" +#import "SAMKeychain.h" +#import "SAMKeychainQuery.h" @interface MPPasswordInputController () @@ -50,6 +52,7 @@ @interface MPPasswordInputController () @property (assign) BOOL showPassword; @property (nonatomic, assign) BOOL enablePassword; @property (copy) passwordInputCompletionBlock completionHandler; +@property (nonatomic, readonly) NSString *databaseName; @end @implementation MPPasswordInputController @@ -81,6 +84,11 @@ - (void)viewDidLoad { [self _reset]; } +-(void)viewDidAppear { + [super viewDidAppear]; + [self _enableTouchID]; //Maybe call this when the password text field is focused and not on viewDidAppear... +} + - (NSResponder *)reconmendedFirstResponder { return self.passwordTextField; } @@ -112,6 +120,12 @@ - (void)setEnablePassword:(BOOL)enablePassword { } } +- (NSString*) databaseName { + MPDocumentWindowController *documentWindow = self.windowController; + MPDocument *document = documentWindow.document; + return document.displayName; +} + #pragma mark - #pragma mark Private - (IBAction)_submit:(id)sender { @@ -211,4 +225,50 @@ - (void)toggleShowPassword { } } +- (void)_enableTouchID { + + if (![MPSettingsHelper.touchIdEnabledDatabases containsObject:self.databaseName]) { +// [_useTouchIdButton setEnabled:NO]; + return; //Do not ask for TouchID if its not enabled for this database. + } else { + [self _getPasswordFromKeychain]; + } + +// if (MPOSHelper.supportsTouchID) { +// LAContext *myContext = [LAContext new]; +// NSString *myLocalizedReasonString = NSLocalizedString(@"TOUCHBAR_TOUCH_ID_MESSAGE", @""); +// [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) { +// if (success) { +// // User authenticated successfully, take appropriate action +// NSLog(@"User authentication sucessful! Getting password from the keychain..."); +// [self _getPasswordFromKeychain]; +// } else { +// // User did not authenticate successfully, look at error and take appropriate action +// NSLog(@"User authentication failed. %@", error.localizedDescription); +// } +// }]; +// } else { +// NSLog(@"TouchID is not supported."); +// } +} + +- (void) _getPasswordFromKeychain{ + NSString *passwordItem = [SAMKeychain passwordForService:@"MacPass" account:self.databaseName]; + __autoreleasing NSError *err = nil; + + _passwordTextField.stringValue = passwordItem; + [self _submit:nil]; + +// NSString *pass = [passwordItem readPasswordAndReturnError:&err]; +// if (err != nil) { +// NSLog(@"Could not retrieve DB password from the keychain:"); +// } else { +// dispatch_sync(dispatch_get_main_queue(), ^{ +// _passwordTextField.stringValue = passwordItem; +// [self _submit:nil]; +// }); +// } + +} + @end diff --git a/MacPass/MPSettingsHelper.m b/MacPass/MPSettingsHelper.m index 16ad29693..1987868a3 100644 --- a/MacPass/MPSettingsHelper.m +++ b/MacPass/MPSettingsHelper.m @@ -164,7 +164,7 @@ + (NSDictionary *)_standardDefaults { kMPSettingsKeyQuitOnLastWindowClose: @NO, kMPSettingsKeyEnableAutosave: @YES, kMPSettingsKeyHideAfterCopyToClipboard: @NO, - kMPSettingsKeyDatabasesUsingTouchID: [NSMutableArray new] +// kMPSettingsKeyDatabasesUsingTouchID: [NSMutableArray new] }; }); return standardDefaults;