4 #import "utilities/debugging.h"
6 #import "keychain/ot/OTConstants.h"
7 #import "keychain/ot/OTDetermineHSA2AccountStatusOperation.h"
8 #import "keychain/ot/OTStates.h"
9 #import "keychain/ckks/CKKSAccountStateTracker.h"
11 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
12 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
13 #import "keychain/ot/ObjCImprovements.h"
15 @interface OTDetermineHSA2AccountStatusOperation ()
16 @property OTOperationDependencies* deps;
18 @property OctagonState* stateIfNotHSA2;
19 @property OctagonState* stateIfNoAccount;
20 @property NSOperation* finishedOp;
23 @implementation OTDetermineHSA2AccountStatusOperation
24 @synthesize intendedState = _intendedState;
26 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
27 stateIfHSA2:(OctagonState*)stateIfHSA2
28 stateIfNotHSA2:(OctagonState*)stateIfNotHSA2
29 stateIfNoAccount:(OctagonState*)stateIfNoAccount
30 errorState:(OctagonState*)errorState
32 if((self = [super init])) {
35 _intendedState = stateIfHSA2;
36 _stateIfNotHSA2 = stateIfNotHSA2;
37 _stateIfNoAccount = stateIfNoAccount;
38 _nextState = errorState;
45 self.finishedOp = [[NSOperation alloc] init];
46 [self dependOnBeforeGroupFinished:self.finishedOp];
49 NSString* primaryAccountAltDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:&error];
52 if(primaryAccountAltDSID != nil) {
53 secnotice("octagon", "iCloud account is present; checking HSA2 status");
55 bool hsa2 = [self.deps.authKitAdapter accountIsHSA2ByAltDSID:primaryAccountAltDSID];
56 secnotice("octagon", "HSA2 is %@", hsa2 ? @"enabled" : @"disabled");
58 [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC *(OTAccountMetadataClassC * metadata) {
60 metadata.icloudAccountState = OTAccountMetadataClassC_AccountState_ACCOUNT_AVAILABLE;
62 metadata.icloudAccountState = OTAccountMetadataClassC_AccountState_NO_ACCOUNT;
64 metadata.altDSID = primaryAccountAltDSID;
68 // If there's an HSA2 account, return to 'initializing' here, as we want to centralize decisions on what to do next
70 self.nextState = self.intendedState;
72 //[self.deps.accountStateTracker setHSA2iCloudAccountStatus:CKKSAccountStatusNoAccount];
73 self.nextState = self.stateIfNotHSA2;
77 secnotice("octagon", "iCloud account is not present: %@", error);
79 [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC *(OTAccountMetadataClassC * metadata) {
80 metadata.icloudAccountState = OTAccountMetadataClassC_AccountState_NO_ACCOUNT;
81 metadata.altDSID = nil;
85 self.nextState = self.stateIfNoAccount;
89 secerror("octagon: unable to save new account state: %@", error);
92 [self runBeforeGroupFinished:self.finishedOp];