Skip to content

Commit

Permalink
fixed password input controller for unlocking the database
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesnow committed Oct 4, 2019
1 parent 9eae38f commit 4beb950
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
8 changes: 4 additions & 4 deletions MacPass/MPPasswordEditWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand All @@ -176,15 +176,15 @@ -(void) _askForTouchID:(NSString*)password {
NSLog(@"User denied Touch ID. Deleting password from keychain.");
[self _deletePasswordFromKeychain];
}
}
// }
} else {
// Fallback on earlier versions
}
}
- (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];
Expand All @@ -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;


Expand Down
60 changes: 60 additions & 0 deletions MacPass/MPPasswordInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#import "HNHUi/HNHUi.h"

#import "NSError+Messages.h"
#import "SAMKeychain.h"
#import "SAMKeychainQuery.h"

@interface MPPasswordInputController ()

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion MacPass/MPSettingsHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ + (NSDictionary *)_standardDefaults {
kMPSettingsKeyQuitOnLastWindowClose: @NO,
kMPSettingsKeyEnableAutosave: @YES,
kMPSettingsKeyHideAfterCopyToClipboard: @NO,
kMPSettingsKeyDatabasesUsingTouchID: [NSMutableArray new]
// kMPSettingsKeyDatabasesUsingTouchID: [NSMutableArray new]
};
});
return standardDefaults;
Expand Down

0 comments on commit 4beb950

Please sign in to comment.