2012-06-04 09:27:02 +00:00
|
|
|
//
|
2014-09-21 14:39:09 +00:00
|
|
|
// MPEntities.m
|
2012-06-04 09:27:02 +00:00
|
|
|
// MasterPassword-iOS
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 31/05/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPEntities.h"
|
2013-04-27 04:34:28 +00:00
|
|
|
#import "MPAppDelegate_Shared.h"
|
2015-02-28 15:01:41 +00:00
|
|
|
#import "MPAppDelegate_Key.h"
|
2016-01-14 15:03:30 +00:00
|
|
|
#import "MPAppDelegate_InApp.h"
|
2012-06-04 09:27:02 +00:00
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
@implementation NSManagedObjectContext(MP)
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2013-04-13 17:40:17 +00:00
|
|
|
- (BOOL)saveToStore {
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2013-09-13 12:14:58 +00:00
|
|
|
__block BOOL success = YES;
|
2014-04-07 03:34:18 +00:00
|
|
|
if ([self hasChanges]) {
|
2013-09-13 12:14:58 +00:00
|
|
|
[self performBlockAndWait:^{
|
|
|
|
@try {
|
|
|
|
NSError *error = nil;
|
|
|
|
if (!(success = [self save:&error]))
|
2014-08-23 05:39:47 +00:00
|
|
|
err( @"While saving: %@", [error fullDescription] );
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
success = NO;
|
2014-10-23 01:17:02 +00:00
|
|
|
err( @"While saving: %@", [exception fullDescription] );
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
}];
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
|
|
|
|
return success && (!self.parentContext || [self.parentContext saveToStore]);
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2016-02-21 02:56:04 +00:00
|
|
|
@implementation MPSiteQuestionEntity(MP)
|
|
|
|
|
|
|
|
- (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key {
|
|
|
|
|
|
|
|
return [self.site.algorithm resolveAnswerForQuestion:self usingKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resolveQuestionAnswerUsingKey:(MPKey *)key result:(void ( ^ )(NSString *))result {
|
|
|
|
|
|
|
|
[self.site.algorithm resolveAnswerForQuestion:self usingKey:key result:result];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
@implementation MPSiteEntity(MP)
|
2012-06-04 09:27:02 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (MPFixableResult)findAndFixInconsistenciesInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
|
|
|
return MPFixableResultNoProblems;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteType)type {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return (MPSiteType)[self.type_ unsignedIntegerValue];
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (void)setLoginGenerated:(BOOL)aLoginGenerated {
|
|
|
|
|
|
|
|
self.loginGenerated_ = @(aLoginGenerated);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)loginGenerated {
|
|
|
|
|
|
|
|
return [self.loginGenerated_ boolValue];
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)setType:(MPSiteType)aType {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.type_ = @(aType);
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
- (NSString *)typeName {
|
|
|
|
|
|
|
|
return [self.algorithm nameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)typeShortName {
|
|
|
|
|
|
|
|
return [self.algorithm shortNameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)typeClassName {
|
|
|
|
|
|
|
|
return [self.algorithm classNameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class)typeClass {
|
|
|
|
|
|
|
|
return [self.algorithm classOfType:self.type];
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:55:02 +00:00
|
|
|
- (NSUInteger)uses {
|
|
|
|
|
|
|
|
return [self.uses_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUses:(NSUInteger)anUses {
|
2012-06-04 09:27:02 +00:00
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.uses_ = @(anUses);
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (id<MPAlgorithm>)algorithm {
|
2012-07-12 06:41:18 +00:00
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
return MPAlgorithmForVersion(
|
|
|
|
MIN( MPAlgorithmVersionCurrent,
|
|
|
|
MAX( MPAlgorithmVersion0, (MPAlgorithmVersion)[self.version_ unsignedIntegerValue] ) ) );
|
2012-07-12 06:41:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (void)setAlgorithm:(id<MPAlgorithm>)algorithm {
|
2012-07-12 06:41:18 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
self.version_ = @([algorithm version]);
|
2012-07-12 06:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)requiresExplicitMigration {
|
|
|
|
|
|
|
|
return [self.requiresExplicitMigration_ boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRequiresExplicitMigration:(BOOL)requiresExplicitMigration {
|
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.requiresExplicitMigration_ = @(requiresExplicitMigration);
|
2012-07-12 06:41:18 +00:00
|
|
|
}
|
2012-06-04 22:55:02 +00:00
|
|
|
|
|
|
|
- (NSUInteger)use {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-04 22:55:02 +00:00
|
|
|
self.lastUsed = [NSDate date];
|
|
|
|
return ++self.uses;
|
2012-06-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
return strf( @"%@:%@", [self class], [self name] );
|
2012-06-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)debugDescription {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
__block NSString *debugDescription = strf( @"{%@: [recursing]}", [self class] );
|
|
|
|
|
|
|
|
static BOOL recursing = NO;
|
|
|
|
PearlIfNotRecursing( &recursing, ^{
|
|
|
|
@try {
|
|
|
|
debugDescription = strf(
|
|
|
|
@"{%@: name=%@, user=%@, type=%lu, uses=%ld, lastUsed=%@, version=%ld, loginName=%@, requiresExplicitMigration=%d}",
|
|
|
|
NSStringFromClass( [self class] ), self.name, self.user.name, (long)self.type, (long)self.uses, self.lastUsed,
|
|
|
|
(long)[self.algorithm version],
|
|
|
|
self.loginName, self.requiresExplicitMigration );
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
debugDescription = strf( @"{%@: inaccessible: %@}",
|
|
|
|
NSStringFromClass( [self class] ), [exception fullDescription] );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return debugDescription;
|
2012-07-17 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
- (BOOL)tryMigrateExplicitly:(BOOL)explicit {
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
MPAlgorithmVersion algorithmVersion;
|
|
|
|
while ((algorithmVersion = [self.algorithm version]) < MPAlgorithmDefaultVersion) {
|
2015-09-23 04:31:33 +00:00
|
|
|
MPAlgorithmVersion toVersion = algorithmVersion + 1;
|
2014-09-24 11:58:23 +00:00
|
|
|
if (![MPAlgorithmForVersion( toVersion ) tryMigrateSite:self explicit:explicit]) {
|
2014-09-21 14:39:09 +00:00
|
|
|
wrn( @"%@ migration to version: %ld failed for site: %@",
|
2014-09-24 11:58:23 +00:00
|
|
|
explicit? @"Explicit": @"Automatic", (long)toVersion, self );
|
2012-07-17 20:57:11 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
inf( @"%@ migration to version: %ld succeeded for site: %@",
|
|
|
|
explicit? @"Explicit": @"Automatic", (long)toVersion, self );
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
return YES;
|
2012-06-04 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (NSString *)resolveLoginUsingKey:(MPKey *)key {
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return [self.algorithm resolveLoginForSite:self usingKey:key];
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)resolvePasswordUsingKey:(MPKey *)key {
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return [self.algorithm resolvePasswordForSite:self usingKey:key];
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 02:56:04 +00:00
|
|
|
- (NSString *)resolveSiteAnswerUsingKey:(MPKey *)key {
|
|
|
|
|
|
|
|
return [self.algorithm resolveAnswerForSite:self usingKey:key];
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (void)resolveLoginUsingKey:(MPKey *)key result:(void ( ^ )(NSString *))result {
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
[self.algorithm resolveLoginForSite:self usingKey:key result:result];
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resolvePasswordUsingKey:(MPKey *)key result:(void ( ^ )(NSString *))result {
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
[self.algorithm resolvePasswordForSite:self usingKey:key result:result];
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 02:56:04 +00:00
|
|
|
- (void)resolveSiteAnswerUsingKey:(MPKey *)key result:(void ( ^ )(NSString *))result {
|
|
|
|
|
|
|
|
[self.algorithm resolveAnswerForSite:self usingKey:key result:result];
|
|
|
|
}
|
|
|
|
|
2016-04-24 03:56:16 +00:00
|
|
|
- (void)resolveAnswerUsingKey:(MPKey *)key forQuestion:(NSString *)question result:(void ( ^ )(NSString *))result {
|
|
|
|
|
|
|
|
MPSiteQuestionEntity *questionEntity = [MPSiteQuestionEntity new];
|
|
|
|
questionEntity.site = self;
|
|
|
|
questionEntity.keyword = question;
|
|
|
|
[questionEntity resolveQuestionAnswerUsingKey:key result:result];
|
|
|
|
}
|
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
@end
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
@implementation MPGeneratedSiteEntity(MP)
|
2012-06-04 09:27:02 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (MPFixableResult)findAndFixInconsistenciesInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
|
|
|
MPFixableResult result = [super findAndFixInconsistenciesInContext:context];
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
if (!self.type || self.type == (MPSiteType)NSNotFound || ![[self.algorithm allTypes] containsObject:self.type_])
|
2014-09-15 05:00:23 +00:00
|
|
|
// Invalid self.type
|
|
|
|
result = MPApplyFix( result, ^MPFixableResult {
|
|
|
|
wrn( @"Invalid type for: %@ of %@, type: %ld. Will use %ld instead.",
|
|
|
|
self.name, self.user.name, (long)self.type, (long)self.user.defaultType );
|
|
|
|
self.type = self.user.defaultType;
|
|
|
|
return MPFixableResultProblemsFixed;
|
|
|
|
} );
|
2014-09-21 14:39:09 +00:00
|
|
|
if (!self.type || self.type == (MPSiteType)NSNotFound || ![[self.algorithm allTypes] containsObject:self.type_])
|
2014-09-15 05:00:23 +00:00
|
|
|
// Invalid self.user.defaultType
|
|
|
|
result = MPApplyFix( result, ^MPFixableResult {
|
|
|
|
wrn( @"Invalid type for: %@ of %@, type: %ld. Will use %ld instead.",
|
2017-04-20 01:58:10 +00:00
|
|
|
self.name, self.user.name, (long)self.type, (long)[self.algorithm defaultType] );
|
|
|
|
self.type = [self.algorithm defaultType];
|
2014-09-15 05:00:23 +00:00
|
|
|
return MPFixableResultProblemsFixed;
|
|
|
|
} );
|
|
|
|
if (![self isKindOfClass:[self.algorithm classOfType:self.type]])
|
|
|
|
// Mismatch between self.type and self.class
|
|
|
|
result = MPApplyFix( result, ^MPFixableResult {
|
2014-09-21 14:39:09 +00:00
|
|
|
for (MPSiteType newType = self.type; self.type != (newType = [self.algorithm nextType:newType]);)
|
2014-09-15 05:00:23 +00:00
|
|
|
if ([self isKindOfClass:[self.algorithm classOfType:newType]]) {
|
|
|
|
wrn( @"Mismatching type for: %@ of %@, type: %lu, class: %@. Will use %ld instead.",
|
|
|
|
self.name, self.user.name, (long)self.type, self.class, (long)newType );
|
|
|
|
self.type = newType;
|
|
|
|
return MPFixableResultProblemsFixed;
|
|
|
|
}
|
|
|
|
|
|
|
|
err( @"Mismatching type for: %@ of %@, type: %lu, class: %@. Couldn't find a type to fix problem with.",
|
|
|
|
self.name, self.user.name, (long)self.type, self.class );
|
|
|
|
return MPFixableResultProblemsNotFixed;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:55:02 +00:00
|
|
|
- (NSUInteger)counter {
|
|
|
|
|
|
|
|
return [self.counter_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCounter:(NSUInteger)aCounter {
|
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.counter_ = @(aCounter);
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
@end
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
@implementation MPStoredSiteEntity(MP)
|
2012-06-04 09:27:02 +00:00
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (MPFixableResult)findAndFixInconsistenciesInContext:(NSManagedObjectContext *)context {
|
|
|
|
|
|
|
|
MPFixableResult result = [super findAndFixInconsistenciesInContext:context];
|
|
|
|
|
|
|
|
if (self.contentObject && ![self.contentObject isKindOfClass:[NSData class]])
|
|
|
|
result = MPApplyFix( result, ^MPFixableResult {
|
|
|
|
MPKey *key = [MPAppDelegate_Shared get].key;
|
|
|
|
if (key && [[MPAppDelegate_Shared get] activeUserInContext:context] == self.user) {
|
|
|
|
wrn( @"Content object not encrypted for: %@ of %@. Will re-encrypt.", self.name, self.user.name );
|
2014-09-21 14:39:09 +00:00
|
|
|
[self.algorithm savePassword:[self.contentObject description] toSite:self usingKey:key];
|
2014-09-15 05:00:23 +00:00
|
|
|
return MPFixableResultProblemsFixed;
|
|
|
|
}
|
|
|
|
|
|
|
|
err( @"Content object not encrypted for: %@ of %@. Couldn't fix, please sign in.", self.name, self.user.name );
|
|
|
|
return MPFixableResultProblemsNotFixed;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
@end
|
2012-06-04 22:55:02 +00:00
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
@implementation MPUserEntity(MP)
|
2012-06-04 22:55:02 +00:00
|
|
|
|
|
|
|
- (NSUInteger)avatar {
|
|
|
|
|
|
|
|
return [self.avatar_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAvatar:(NSUInteger)anAvatar {
|
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.avatar_ = @(anAvatar);
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)saveKey {
|
|
|
|
|
|
|
|
return [self.saveKey_ boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSaveKey:(BOOL)aSaveKey {
|
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.saveKey_ = @(aSaveKey);
|
2012-06-04 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 07:14:36 +00:00
|
|
|
- (BOOL)touchID {
|
|
|
|
|
2016-01-14 16:22:15 +00:00
|
|
|
return [self.touchID_ boolValue] && [[MPAppDelegate_Shared get] isFeatureUnlocked:MPProductTouchID];
|
2016-01-14 07:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTouchID:(BOOL)aTouchID {
|
|
|
|
|
|
|
|
self.touchID_ = @(aTouchID);
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteType)defaultType {
|
2012-06-11 14:15:49 +00:00
|
|
|
|
2017-04-20 01:58:10 +00:00
|
|
|
return (MPSiteType)[self.defaultType_ unsignedIntegerValue]?: self.algorithm.defaultType;
|
2012-06-11 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)setDefaultType:(MPSiteType)aDefaultType {
|
2012-06-14 19:56:54 +00:00
|
|
|
|
2012-08-19 18:02:23 +00:00
|
|
|
self.defaultType_ = @(aDefaultType);
|
2012-06-14 19:56:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (id<MPAlgorithm>)algorithm {
|
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
return MPAlgorithmForVersion(
|
|
|
|
MIN( MPAlgorithmVersionCurrent,
|
|
|
|
MAX( MPAlgorithmVersion0, (MPAlgorithmVersion)[self.version_ unsignedIntegerValue] ) ) );
|
2015-02-28 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAlgorithm:(id<MPAlgorithm>)version {
|
|
|
|
|
|
|
|
self.version_ = @([version version]);
|
|
|
|
[[MPAppDelegate_Shared get] forgetSavedKeyFor:self];
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
- (NSString *)userID {
|
|
|
|
|
|
|
|
return [MPUserEntity idFor:self.name];
|
2012-06-11 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2012-06-14 19:56:54 +00:00
|
|
|
+ (NSString *)idFor:(NSString *)userName {
|
|
|
|
|
|
|
|
return [[userName hashWith:PearlHashSHA1] encodeHex];
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:55:02 +00:00
|
|
|
@end
|