2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-02-12 05:13:12 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2014-02-12 05:13:12 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2014-02-12 05:13:12 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2014-02-12 05:13:12 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
#import "MPSiteModel.h"
|
2014-02-12 05:13:12 +00:00
|
|
|
#import "MPEntities.h"
|
|
|
|
#import "MPAppDelegate_Shared.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
|
|
|
#import "MPMacAppDelegate.h"
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
@interface MPSiteModel()
|
|
|
|
|
|
|
|
@property(nonatomic, strong) NSManagedObjectID *entityOID;
|
|
|
|
@property(nonatomic) BOOL initialized;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPSiteModel
|
2014-02-12 05:13:12 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
- (instancetype)initWithEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups {
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
[self setEntity:entity fuzzyGroups:fuzzyGroups];
|
2017-05-07 22:36:01 +00:00
|
|
|
self.initialized = YES;
|
2014-05-19 18:37:05 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
- (instancetype)initWithName:(NSString *)siteName forUser:(MPUserEntity *)user {
|
2015-03-05 22:28:04 +00:00
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
[self setTransientSiteName:siteName forUser:user];
|
2017-05-07 22:36:01 +00:00
|
|
|
self.initialized = YES;
|
2015-03-05 22:28:04 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
- (void)setEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups {
|
2014-05-19 18:37:05 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if ([self.entityOID isEqual:entity.permanentObjectID])
|
2014-05-19 18:37:05 +00:00
|
|
|
return;
|
2017-05-07 22:36:01 +00:00
|
|
|
self.entityOID = entity.permanentObjectID;
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
NSString *siteName = entity.name;
|
|
|
|
NSMutableAttributedString *attributedSiteName = [[NSMutableAttributedString alloc] initWithString:siteName];
|
|
|
|
for (NSUInteger f = 0, s = (NSUInteger)-1; f < [fuzzyGroups count]; ++f) {
|
|
|
|
s = [siteName rangeOfString:fuzzyGroups[f] options:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch
|
|
|
|
range:NSMakeRange( s + 1, [siteName length] - (s + 1) )].location;
|
|
|
|
if (s == NSNotFound)
|
|
|
|
break;
|
|
|
|
|
|
|
|
[attributedSiteName addAttribute:NSBackgroundColorAttributeName value:[NSColor alternateSelectedControlColor]
|
|
|
|
range:NSMakeRange( s, [fuzzyGroups[f] length] )];
|
|
|
|
}
|
|
|
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
|
|
paragraphStyle.alignment = NSCenterTextAlignment;
|
|
|
|
[attributedSiteName addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange( 0, [siteName length] )];
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
self.displayedName = attributedSiteName;
|
|
|
|
self.name = siteName;
|
2014-05-19 18:37:05 +00:00
|
|
|
self.algorithm = entity.algorithm;
|
|
|
|
self.lastUsed = entity.lastUsed;
|
|
|
|
self.type = entity.type;
|
|
|
|
self.typeName = entity.typeName;
|
|
|
|
self.uses = entity.uses_;
|
2017-08-13 02:26:48 +00:00
|
|
|
self.counter = [entity isKindOfClass:[MPGeneratedSiteEntity class]]? [(MPGeneratedSiteEntity *)entity counter]: MPCounterValueInitial;
|
2016-05-16 04:50:17 +00:00
|
|
|
self.loginGenerated = entity.loginGenerated;
|
2014-05-19 18:37:05 +00:00
|
|
|
|
2014-02-21 05:19:24 +00:00
|
|
|
// Find all password types and the index of the current type amongst them.
|
2014-06-27 03:13:21 +00:00
|
|
|
[self updateContent:entity];
|
2014-02-12 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
- (void)setTransientSiteName:(NSString *)siteName forUser:(MPUserEntity *)user {
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
self.entityOID = nil;
|
2015-03-05 22:28:04 +00:00
|
|
|
|
|
|
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
|
|
paragraphStyle.alignment = NSCenterTextAlignment;
|
|
|
|
self.displayedName = stra( siteName, @{
|
2017-04-01 04:30:25 +00:00
|
|
|
NSBackgroundColorAttributeName: [NSColor alternateSelectedControlColor],
|
|
|
|
NSParagraphStyleAttributeName : paragraphStyle,
|
2015-03-05 22:28:04 +00:00
|
|
|
} );
|
|
|
|
self.name = siteName;
|
|
|
|
self.algorithm = MPAlgorithmDefault;
|
|
|
|
self.lastUsed = nil;
|
2015-03-11 21:30:34 +00:00
|
|
|
self.type = user.defaultType;
|
2015-03-05 22:28:04 +00:00
|
|
|
self.typeName = [self.algorithm nameOfType:self.type];
|
|
|
|
self.uses = @0;
|
2017-08-13 02:26:48 +00:00
|
|
|
self.counter = MPCounterValueDefault;
|
2015-03-05 22:28:04 +00:00
|
|
|
|
|
|
|
// Find all password types and the index of the current type amongst them.
|
|
|
|
[self updateContent];
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteEntity *)entityInContext:(NSManagedObjectContext *)moc {
|
2014-02-12 05:13:12 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.entityOID)
|
2014-02-12 05:13:12 +00:00
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSError *error;
|
2017-08-13 02:26:48 +00:00
|
|
|
MPSiteEntity *entity = [moc existingObjectWithID:self.entityOID error:&error];
|
2014-02-12 05:13:12 +00:00
|
|
|
if (!entity)
|
2017-04-30 03:03:50 +00:00
|
|
|
MPError( error, @"Couldn't retrieve active site." );
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2017-08-13 02:26:48 +00:00
|
|
|
- (void)setCounter:(MPCounterValue)counter {
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.counter == counter)
|
2014-02-21 05:19:24 +00:00
|
|
|
return;
|
|
|
|
_counter = counter;
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.initialized)
|
2014-05-19 18:37:05 +00:00
|
|
|
// This wasn't a change to the entity.
|
|
|
|
return;
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.entityOID)
|
2015-03-05 22:28:04 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPSiteEntity *entity = [self entityInContext:context];
|
|
|
|
if ([entity isKindOfClass:[MPGeneratedSiteEntity class]]) {
|
|
|
|
((MPGeneratedSiteEntity *)entity).counter = counter;
|
|
|
|
[context saveToStore];
|
|
|
|
|
|
|
|
[self updateContent:entity];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
[self updateContent];
|
2014-02-21 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 04:50:17 +00:00
|
|
|
- (void)setLoginGenerated:(BOOL)loginGenerated {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.loginGenerated == loginGenerated)
|
2016-05-16 04:50:17 +00:00
|
|
|
return;
|
|
|
|
_loginGenerated = loginGenerated;
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (!self.initialized)
|
2016-05-16 04:50:17 +00:00
|
|
|
// This wasn't a change to the entity.
|
|
|
|
return;
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.entityOID)
|
2016-05-16 04:50:17 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPSiteEntity *entity = [self entityInContext:context];
|
|
|
|
entity.loginGenerated = loginGenerated;
|
|
|
|
[context saveToStore];
|
|
|
|
|
|
|
|
[self updateContent:entity];
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
[self updateContent];
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
- (MPAlgorithmVersion)algorithmVersion {
|
|
|
|
|
|
|
|
return self.algorithm.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAlgorithmVersion:(MPAlgorithmVersion)algorithmVersion {
|
|
|
|
|
|
|
|
if (algorithmVersion == self.algorithm.version)
|
|
|
|
return;
|
|
|
|
[self willChangeValueForKey:@"outdated"];
|
|
|
|
self.algorithm = MPAlgorithmForVersion( algorithmVersion )?: self.algorithm;
|
|
|
|
[self didChangeValueForKey:@"outdated"];
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.entityOID)
|
2015-09-23 04:31:33 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
|
|
|
MPSiteEntity *entity = [self entityInContext:context];
|
|
|
|
entity.algorithm = self.algorithm;
|
|
|
|
[context saveToStore];
|
|
|
|
|
|
|
|
[self updateContent:entity];
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
[self updateContent];
|
|
|
|
}
|
|
|
|
|
2016-04-23 15:54:07 +00:00
|
|
|
- (void)setQuestion:(NSString *)question {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if ([self.question isEqualToString:question])
|
2016-04-23 15:54:07 +00:00
|
|
|
return;
|
|
|
|
_question = question;
|
|
|
|
|
|
|
|
[self updateContent];
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
- (BOOL)outdated {
|
|
|
|
|
|
|
|
return self.algorithmVersion < MPAlgorithmVersionCurrent;
|
|
|
|
}
|
|
|
|
|
2014-06-26 05:19:42 +00:00
|
|
|
- (BOOL)generated {
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2017-08-13 02:26:48 +00:00
|
|
|
return self.type & MPResultTypeClassTemplate;
|
2014-06-26 05:19:42 +00:00
|
|
|
}
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2014-06-26 05:19:42 +00:00
|
|
|
- (BOOL)stored {
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2017-08-13 02:26:48 +00:00
|
|
|
return self.type & MPResultTypeClassStateful;
|
2014-02-21 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
- (BOOL)transient {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
return self.entityOID == nil;
|
2015-03-05 22:28:04 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:13:21 +00:00
|
|
|
- (void)updateContent {
|
|
|
|
|
2017-05-07 22:36:01 +00:00
|
|
|
if (self.entityOID)
|
2015-03-05 22:28:04 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2017-05-07 22:36:01 +00:00
|
|
|
[self updateContent:[MPSiteEntity existingObjectWithID:self.entityOID inContext:context]];
|
2015-03-05 22:28:04 +00:00
|
|
|
}];
|
2016-02-21 02:56:04 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
else
|
|
|
|
PearlNotMainQueue( ^{
|
2016-02-21 02:56:04 +00:00
|
|
|
[self updatePasswordWithResult:
|
2017-08-13 01:57:47 +00:00
|
|
|
[self.algorithm mpwTemplateForSiteNamed:self.name ofType:self.type withCounter:self.counter
|
|
|
|
usingKey:[MPAppDelegate_Shared get].key]];
|
2016-02-21 02:56:04 +00:00
|
|
|
[self updateLoginNameWithResult:
|
2017-08-13 01:57:47 +00:00
|
|
|
[self.algorithm mpwLoginForSiteNamed:self.name
|
|
|
|
usingKey:[MPAppDelegate_Shared get].key]];
|
2016-02-21 02:56:04 +00:00
|
|
|
[self updateAnswerWithResult:
|
2017-08-13 01:57:47 +00:00
|
|
|
[self.algorithm mpwAnswerForSiteNamed:self.name onQuestion:self.question
|
|
|
|
usingKey:[MPAppDelegate_Shared get].key]];
|
2015-03-05 22:28:04 +00:00
|
|
|
} );
|
2014-06-27 03:13:21 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)updateContent:(MPSiteEntity *)entity {
|
2014-06-27 03:13:21 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
[entity resolvePasswordUsingKey:[MPAppDelegate_Shared get].key result:^(NSString *result) {
|
|
|
|
[self updatePasswordWithResult:result];
|
|
|
|
}];
|
|
|
|
[entity resolveLoginUsingKey:[MPAppDelegate_Shared get].key result:^(NSString *result) {
|
|
|
|
[self updateLoginNameWithResult:result];
|
|
|
|
}];
|
2017-08-13 01:57:47 +00:00
|
|
|
[self updateAnswerWithResult:[self.algorithm mpwAnswerForSiteNamed:self.name onQuestion:self.question
|
|
|
|
usingKey:[MPAppDelegate_Shared get].key]];
|
2015-03-05 22:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatePasswordWithResult:(NSString *)result {
|
|
|
|
|
2014-06-29 03:45:06 +00:00
|
|
|
static NSRegularExpression *re_anyChar;
|
|
|
|
static dispatch_once_t once = 0;
|
|
|
|
dispatch_once( &once, ^{
|
|
|
|
re_anyChar = [NSRegularExpression regularExpressionWithPattern:@"." options:0 error:nil];
|
|
|
|
} );
|
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
NSString *displayResult = result;
|
|
|
|
if ([[MPConfig get].hidePasswords boolValue] && !([NSEvent modifierFlags] & NSAlternateKeyMask))
|
|
|
|
displayResult = [displayResult stringByReplacingMatchesOfExpression:re_anyChar withTemplate:@"●"];
|
2014-06-27 03:13:21 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.content = result;
|
|
|
|
self.displayedContent = displayResult;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateLoginNameWithResult:(NSString *)loginName {
|
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.loginName = loginName;
|
|
|
|
} );
|
2014-06-27 03:13:21 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 02:56:04 +00:00
|
|
|
- (void)updateAnswerWithResult:(NSString *)answer {
|
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.answer = answer;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:13:12 +00:00
|
|
|
@end
|