diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 0690cc3d..d4714c85 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -36,7 +36,7 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; @property (weak, readonly) KPKGroup *trash; @property (weak, readonly) KPKGroup *templates; -@property (nonatomic, strong) KPKCompositeKey *compositeKey; +@property (nonatomic, strong, readonly) KPKCompositeKey *compositeKey; //@property (nonatomic, copy) NSString *password; //@property (nonatomic, strong) NSURL *key; @@ -56,7 +56,25 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; #pragma mark Lock/Decrypt - (void)lockDatabase:(id)sender; +/** + * Decrypts the databse 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 stirng @"" + * @param keyFileURL URL for the keyfile to use, 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; +/** + * Changes the password of the database. Some sanity checks are applied and the change is aborted if the new values arent valid + * + * @param password new password, can be nil + * @param keyFileURL new key URL can be nil + * + * @return YES if the password was change, NO otherwise + */ +- (BOOL)changePassword:(NSString *)password keyFileURL:(NSURL *)keyFileURL; /** * Returns the suggest key URL for this document. This might be nil. * If the user did disable remeberKeyFiles in the settings, this always returns nil diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index c1d7d70a..16afc23a 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -52,6 +52,7 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { @property (strong, nonatomic) KPKTree *tree; @property (weak, nonatomic) KPKGroup *root; +@property (nonatomic, strong) KPKCompositeKey *compositeKey; @property (assign) BOOL readOnly; @property (strong) NSURL *lockFileURL; @@ -244,12 +245,22 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { return isUnlocked; } +- (BOOL)changePassword:(NSString *)password keyFileURL:(NSURL *)keyFileURL { + /* sanity check? */ + if([password length] == 0 && keyFileURL == nil) { + return NO; + } + [self.compositeKey setPassword:password andKeyfile:keyFileURL]; + /* We need to store the key file once the user actually writes the database */ + return YES; +} + - (NSURL *)suggestedKeyURL { - if(!self.isAllowedToStoreKeyFile) { - return nil; - } - NSDictionary *keysForFiles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases]; - NSString *keyPath = keysForFiles[[[self fileURL] path]]; + if(!self.isAllowedToStoreKeyFile) { + return nil; + } + NSDictionary *keysForFiles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases]; + NSString *keyPath = keysForFiles[[[self fileURL] path]]; if(!keyPath) { return nil; } diff --git a/MacPass/MPPasswordEditWindowController.m b/MacPass/MPPasswordEditWindowController.m index b5378286..7f7f3ced 100644 --- a/MacPass/MPPasswordEditWindowController.m +++ b/MacPass/MPPasswordEditWindowController.m @@ -85,7 +85,7 @@ #pragma mark Actions - (IBAction)save:(id)sender { - _currentDocument.compositeKey = [[KPKCompositeKey alloc] initWithPassword:[self.passwordTextField stringValue] key:[self.keyfilePathControl URL]]; + [_currentDocument changePassword:[self.passwordTextField stringValue] keyFileURL:[self.keyfilePathControl URL]]; [self dismissSheet:NSRunStoppedResponse]; if(self.delegate && [self.delegate respondsToSelector:@selector(didFinishPasswordEditing:)]) { [self.delegate didFinishPasswordEditing:YES];