2 * Copyright (c) 2019 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "OTFollowup.h"
28 #if __has_include(<CoreFollowUp/CoreFollowUp.h>) && !TARGET_OS_SIMULATOR
29 #import <CoreFollowUp/CoreFollowUp.h>
30 #define HAVE_COREFOLLOW_UP 1
33 #import <CoreCDP/CDPFollowUpController.h>
34 #import <CoreCDP/CDPFollowUpContext.h>
36 #include "utilities/debugging.h"
38 static NSString * const kOTFollowupEventCompleteKey = @"OTFollowupContextType";
40 NSString* OTFollowupContextTypeToString(OTFollowupContextType contextType)
43 case OTFollowupContextTypeNone:
45 case OTFollowupContextTypeRecoveryKeyRepair:
46 return @"recovery key";
47 case OTFollowupContextTypeStateRepair:
49 case OTFollowupContextTypeOfflinePasscodeChange:
50 return @"offline passcode change";
54 @interface OTFollowup()
55 @property id<OctagonFollowUpControllerProtocol> cdpd;
56 @property NSTimeInterval previousFollowupEnd;
57 @property NSTimeInterval followupStart;
58 @property NSTimeInterval followupEnd;
60 @property NSMutableSet<NSString*>* postedCFUTypes;
63 @implementation OTFollowup : NSObject
65 - (id)initWithFollowupController:(id<OctagonFollowUpControllerProtocol>)cdpFollowupController
67 if (self = [super init]) {
68 self.cdpd = cdpFollowupController;
70 _postedCFUTypes = [NSMutableSet set];
75 - (CDPFollowUpContext *)createCDPFollowupContext:(OTFollowupContextType)contextType
77 switch (contextType) {
78 case OTFollowupContextTypeStateRepair: {
79 return [CDPFollowUpContext contextForStateRepair];
81 case OTFollowupContextTypeRecoveryKeyRepair: {
82 return [CDPFollowUpContext contextForRecoveryKeyRepair];
84 case OTFollowupContextTypeOfflinePasscodeChange: {
85 return [CDPFollowUpContext contextForOfflinePasscodeChange];
93 - (BOOL)postFollowUp:(OTFollowupContextType)contextType
94 error:(NSError **)error
96 CDPFollowUpContext *context = [self createCDPFollowupContext:contextType];
101 NSError *followupError = nil;
103 secnotice("followup", "Posting a follow up (for Octagon) of type %@", OTFollowupContextTypeToString(contextType));
104 BOOL result = [self.cdpd postFollowUpWithContext:context error:&followupError];
107 [self.postedCFUTypes addObject:OTFollowupContextTypeToString(contextType)];
110 *error = followupError;
117 - (BOOL)clearFollowUp:(OTFollowupContextType)contextType
118 error:(NSError **)error
120 // Note(caw): we don't track metrics for clearing CFU prompts.
121 CDPFollowUpContext *context = [self createCDPFollowupContext:contextType];
126 secnotice("followup", "Clearing follow ups (for Octagon) of type %@", OTFollowupContextTypeToString(contextType));
127 BOOL result = [self.cdpd clearFollowUpWithContext:context error:error];
129 [self.postedCFUTypes removeObject:OTFollowupContextTypeToString(contextType)];
136 - (NSDictionary *_Nullable)sysdiagnoseStatus
138 NSMutableDictionary *pendingCFUs = nil;
140 #if HAVE_COREFOLLOW_UP
141 if ([FLFollowUpController class]) {
142 NSError *error = nil;
143 pendingCFUs = [NSMutableDictionary dictionary];
145 FLFollowUpController *followUpController = [[FLFollowUpController alloc] initWithClientIdentifier:@"com.apple.corecdp"];
146 NSArray <FLFollowUpItem*>* followUps = [followUpController pendingFollowUpItems:&error];
148 secnotice("octagon", "Fetching pending follow ups failed with: %@", error);
149 pendingCFUs[@"error"] = [error description];
151 for (FLFollowUpItem *followUp in followUps) {
152 NSDate *creationDate = followUp.notification.creationDate;
153 pendingCFUs[followUp.uniqueIdentifier] = creationDate;
160 - (NSDictionary<NSString*,NSNumber *> *)sfaStatus {
161 NSMutableDictionary<NSString*, NSNumber*>* values = [NSMutableDictionary dictionary];
162 #if HAVE_COREFOLLOW_UP
163 if ([FLFollowUpController class]) {
164 NSError *error = nil;
167 FLFollowUpController *followUpController = [[FLFollowUpController alloc] initWithClientIdentifier:@"com.apple.corecdp"];
169 NSArray <FLFollowUpItem*>* followUps = [followUpController pendingFollowUpItems:&error];
171 secnotice("octagon", "Fetching pending follow ups failed with: %@", error);
173 for (FLFollowUpItem *followUp in followUps) {
174 NSInteger created = 10000;
176 NSDate *creationDate = followUp.notification.creationDate;
178 created = [CKKSAnalytics fuzzyDaysSinceDate:creationDate];
180 NSString *key = [NSString stringWithFormat:@"OACFU-%@", followUp.uniqueIdentifier];
181 values[key] = @(created);
190 @implementation OTFollowup (Testing)
191 - (BOOL)hasPosted:(OTFollowupContextType)contextType
193 return [self.postedCFUTypes containsObject:OTFollowupContextTypeToString(contextType)];
196 - (void)clearAllPostedFlags
198 [self.postedCFUTypes removeAllObjects];