pEpObjCAdapter/PEPIdentity.m
author Dirk Zimmermann <dz@pep.security>
Fri, 20 Sep 2019 10:01:48 +0200
branchIOSAD-141
changeset 1250 b6a4d30abd26
parent 1198 ab5c05c35481
child 1395 d07913ef56ee
permissions -rw-r--r--
IOSAD-141 Test for "hammering" the engine.
     1 //
     2 //  PEPIdentity.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Dirk Zimmermann on 30.10.17.
     6 //  Copyright © 2017 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPIdentity.h"
    10 #import "PEPConstants.h"
    11 
    12 #import "pEpEngine.h"
    13 #import "PEPMessageUtil.h"
    14 #import "PEPSession.h"
    15 
    16 #import "NSObject+Extension.h"
    17 
    18 @implementation PEPIdentity
    19 
    20 - (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
    21                                  userID:(NSString * _Nullable)userID
    22                                userName:(NSString * _Nullable)userName
    23                                   isOwn:(BOOL)isOwn
    24                             fingerPrint:(NSString * _Nullable)fingerPrint
    25                                commType:(PEPCommType)commType
    26                                language:(NSString * _Nullable)language {
    27     if (self = [super init]) {
    28         self.address = address;
    29         self.userID = userID;
    30         self.userName = userName;
    31         self.isOwn = isOwn;
    32         self.fingerPrint = fingerPrint;
    33         self.commType = commType;
    34         self.language = language;
    35     }
    36     return self;
    37 }
    38 
    39 - (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
    40                                  userID:(NSString * _Nullable)userID
    41                                userName:(NSString * _Nullable)userName
    42                                   isOwn:(BOOL)isOwn
    43                             fingerPrint:(NSString * _Nullable)fingerPrint
    44 {
    45     return [self initWithAddress:address userID:userID userName:userName isOwn:isOwn
    46                      fingerPrint:fingerPrint commType:PEPCommTypeUnknown language:nil];
    47 }
    48 
    49 - (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
    50                                  userID:(NSString * _Nullable)userID
    51                                userName:(NSString * _Nullable)userName
    52                                   isOwn:(BOOL)isOwn
    53 {
    54     return [self initWithAddress:address userID:userID userName:userName
    55                            isOwn:isOwn fingerPrint:nil commType:PEPCommTypeUnknown language:nil];
    56 }
    57 
    58 - (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
    59 {
    60     return [self initWithAddress:address userID:nil userName:nil isOwn:NO fingerPrint:nil
    61                         commType:PEPCommTypeUnknown language:nil];
    62 }
    63 
    64 - (nonnull instancetype)initWithDictionary:(NSDictionary *)dictionary
    65 {
    66     return [self initWithAddress:dictionary[kPepAddress] userID:dictionary[kPepUserID]
    67                         userName:dictionary[kPepUsername]
    68                            isOwn:[dictionary[kPepIsOwnIdentity] boolValue]
    69                      fingerPrint:dictionary[kPepFingerprint]
    70                         commType:[dictionary[kPepCommType] intValue]
    71                         language:dictionary[@"lang"]];
    72 }
    73 
    74 - (nonnull instancetype)initWithIdentity:(PEPIdentity * _Nonnull)identity
    75 {
    76     return [self initWithAddress:identity.address userID:identity.userID
    77                         userName:identity.userName
    78                            isOwn:identity.isOwn
    79                      fingerPrint:identity.fingerPrint
    80                         commType:identity.commType
    81                         language:identity.language];
    82 }
    83 
    84 // MARK: Faking directory
    85 
    86 - (PEPDict * _Nonnull)dictionary
    87 {
    88     // most adapter use should be ok.
    89     return (PEPDict *) self;
    90 }
    91 
    92 - (PEPMutableDict * _Nonnull)mutableDictionary
    93 {
    94     // most adapter use should be ok.
    95     return (PEPMutableDict *) self;
    96 }
    97 
    98 - (NSNumber * _Nullable)isPEPUser:(PEPSession * _Nullable)session
    99                             error:(NSError * _Nullable * _Nullable)error
   100 {
   101     if (!session) {
   102         session = [PEPSession new];
   103     }
   104     return [session isPEPUser:self error:error];
   105 }
   106 
   107 - (BOOL)isConfirmed
   108 {
   109     return self.commType & PEP_ct_confirmed;
   110 }
   111 
   112 // MARK: - Equality
   113 
   114 /**
   115  The keys that should be used to decide `isEqual` and compute the `hash`.
   116  */
   117 static NSArray *s_keys;
   118 
   119 - (BOOL)isEqualToPEPIdentity:(PEPIdentity * _Nonnull)identity
   120 {
   121     return [self isEqualToObject:identity basedOnKeys:s_keys];
   122 }
   123 
   124 - (NSUInteger)hash
   125 {
   126     return [self hashBasedOnKeys:s_keys];
   127 }
   128 
   129 - (BOOL)isEqual:(id)object
   130 {
   131     if (object == self) {
   132         return YES;
   133     }
   134     if (!object || ![object isKindOfClass:[self class]]) {
   135         return NO;
   136     }
   137 
   138     return [self isEqualToPEPIdentity:object];
   139 }
   140 
   141 // MARK: - NSKeyValueCoding
   142 
   143 - (NSUInteger)comm_type
   144 {
   145     return self.commType;
   146 }
   147 
   148 - (void)setComm_type:(PEPCommType)ct
   149 {
   150     self.commType = ct;
   151 }
   152 
   153 - (NSString *)fpr
   154 {
   155     return self.fingerPrint;
   156 }
   157 
   158 - (void)setFpr:(NSString *)fpr
   159 {
   160     self.fingerPrint = fpr;
   161 }
   162 
   163 - (NSString *)user_id
   164 {
   165     return self.userID;
   166 }
   167 
   168 - (void)setUser_id:(NSString *)uid
   169 {
   170     self.userID = uid;
   171 }
   172 
   173 - (NSString *)username
   174 {
   175     return self.userName;
   176 }
   177 
   178 - (void)setUsername:(NSString *)userName
   179 {
   180     self.userName = userName;
   181 }
   182 
   183 - (NSString *)lang
   184 {
   185     return self.language;
   186 }
   187 
   188 - (void)setLang:(NSString *)l
   189 {
   190     self.language = l;
   191 }
   192 
   193 - (void)reset
   194 {
   195     self.commType = PEP_ct_unknown;
   196     self.language = nil;
   197     self.fingerPrint = nil;
   198     self.userID = nil;
   199     self.userName = nil;
   200     self.isOwn = NO;
   201 }
   202 
   203 - (BOOL)enableKeySync:(NSError * _Nullable * _Nullable)error
   204 {
   205     PEPSession *session = [PEPSession new];
   206     return [session enableSyncForIdentity:self error:error];
   207 }
   208 
   209 - (BOOL)disableKeySync:(NSError * _Nullable * _Nullable)error
   210 {
   211     PEPSession *session = [PEPSession new];
   212     return [session disableSyncForIdentity:self error:error];
   213 }
   214 
   215 - (NSNumber * _Nullable)queryKeySyncEnabled:(NSError * _Nullable * _Nullable)error
   216 {
   217     PEPSession *session = [PEPSession new];
   218     return [session queryKeySyncEnabledForIdentity:self error:error];
   219 }
   220 
   221 // MARK: - NSDictionary - Helpers
   222 
   223 - (NSArray<NSArray<NSString *> *> *)keyValuePairs
   224 {
   225     NSMutableArray *result = [@[ @[kPepAddress, self.address],
   226                                  @[kPepCommType,
   227                                    [NSNumber numberWithInteger:(NSInteger) self.commType]],
   228                                  @[kPepIsOwnIdentity, [NSNumber numberWithBool:self.isOwn]]]
   229                               mutableCopy];
   230 
   231     if (self.fingerPrint) {
   232         [result addObject:@[kPepFingerprint, self.fingerPrint]];
   233     }
   234 
   235     if (self.userID) {
   236         [result addObject:@[kPepUserID, self.userID]];
   237     }
   238 
   239     if (self.userName) {
   240         [result addObject:@[kPepUsername, self.userName]];
   241     }
   242 
   243     if (self.language) {
   244         [result addObject:@[@"lang", self.language]];
   245     }
   246 
   247     return result;
   248 }
   249 
   250 // MARK: - NSDictionary
   251 
   252 - (nullable id)objectForKey:(NSString *)key
   253 {
   254     return [self valueForKey:key];
   255 }
   256 
   257 - (NSInteger)count
   258 {
   259     return [[self keyValuePairs] count];
   260 }
   261 
   262 - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
   263 {
   264     BOOL stop = NO;
   265     NSArray *pairs = [self keyValuePairs];
   266     for (NSArray *pair in pairs) {
   267         block(pair[0], pair[1], &stop);
   268         if (stop) {
   269             break;
   270         }
   271     }
   272 }
   273 
   274 // MARK: - NSMutableCopying
   275 
   276 - (id)mutableCopyWithZone:(nullable NSZone *)zone
   277 {
   278     return [[PEPIdentity alloc] initWithAddress:self.address userID:self.userID
   279                                        userName:self.userName isOwn:self.isOwn
   280                                     fingerPrint:self.fingerPrint
   281                                        commType:self.commType language:self.language];
   282 }
   283 
   284 // MARK: - Debug
   285 
   286 - (NSString *)description
   287 {
   288     return [NSString stringWithFormat:
   289             @"<PEPIdentity %@ userID:%@ userName:%@ isOwn:%d fpr:%@ ct:%ld lang:%@>",
   290             self.address, self.userID, self.userName, self.isOwn, self.fingerPrint,
   291             (long) self.commType, self.language];
   292 }
   293 
   294 // MARK: - Static Initialization
   295 
   296 + (void)initialize
   297 {
   298     s_keys = @[@"address"];
   299 }
   300 
   301 @end