1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2019 Google
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
|
|
|
18 |
|
|
|
19 |
#import <GoogleUtilities/GULUserDefaults.h>
|
|
|
20 |
#import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
|
|
|
21 |
#import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinService.h"
|
|
|
22 |
|
|
|
23 |
const NSTimeInterval kFIRMessagingDefaultCheckinInterval = 7 * 24 * 60 * 60; // 7 days.
|
|
|
24 |
static NSString *const kCheckinKeychainContentSeparatorString = @"|";
|
|
|
25 |
|
|
|
26 |
@interface FIRMessagingCheckinPreferences ()
|
|
|
27 |
|
|
|
28 |
@property(nonatomic, readwrite, copy) NSString *deviceID;
|
|
|
29 |
@property(nonatomic, readwrite, copy) NSString *secretToken;
|
|
|
30 |
@property(nonatomic, readwrite, copy) NSString *digest;
|
|
|
31 |
@property(nonatomic, readwrite, copy) NSString *versionInfo;
|
|
|
32 |
@property(nonatomic, readwrite, copy) NSString *deviceDataVersion;
|
|
|
33 |
|
|
|
34 |
@property(nonatomic, readwrite, strong) NSMutableDictionary *gServicesData;
|
|
|
35 |
@property(nonatomic, readwrite, assign) int64_t lastCheckinTimestampMillis;
|
|
|
36 |
|
|
|
37 |
// This flag indicates that we have already saved the above deviceID and secret
|
|
|
38 |
// to our keychain and hence we don't need to save again. This is helpful since
|
|
|
39 |
// on checkin refresh we can avoid writing to the Keychain which can sometimes
|
|
|
40 |
// be very buggy. For info check this https://forums.developer.apple.com/thread/4743
|
|
|
41 |
@property(nonatomic, readwrite, assign) BOOL hasPreCachedAuthCredentials;
|
|
|
42 |
|
|
|
43 |
@end
|
|
|
44 |
|
|
|
45 |
@implementation FIRMessagingCheckinPreferences
|
|
|
46 |
|
|
|
47 |
+ (FIRMessagingCheckinPreferences *)preferencesFromKeychainContents:(NSString *)keychainContent {
|
|
|
48 |
NSString *deviceID = [self checkinDeviceIDFromKeychainContent:keychainContent];
|
|
|
49 |
NSString *secret = [self checkinSecretFromKeychainContent:keychainContent];
|
|
|
50 |
if ([deviceID length] && [secret length]) {
|
|
|
51 |
return [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:deviceID secretToken:secret];
|
|
|
52 |
} else {
|
|
|
53 |
return nil;
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
- (instancetype)initWithDeviceID:(NSString *)deviceID secretToken:(NSString *)secretToken {
|
|
|
58 |
self = [super init];
|
|
|
59 |
if (self) {
|
|
|
60 |
self.deviceID = [deviceID copy];
|
|
|
61 |
self.secretToken = [secretToken copy];
|
|
|
62 |
}
|
|
|
63 |
return self;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
- (void)reset {
|
|
|
67 |
self.deviceID = nil;
|
|
|
68 |
self.secretToken = nil;
|
|
|
69 |
self.digest = nil;
|
|
|
70 |
self.versionInfo = nil;
|
|
|
71 |
self.gServicesData = nil;
|
|
|
72 |
self.deviceDataVersion = nil;
|
|
|
73 |
self.lastCheckinTimestampMillis = 0;
|
|
|
74 |
}
|
|
|
75 |
- (NSDictionary *)checkinPlistContents {
|
|
|
76 |
NSMutableDictionary *checkinPlistContents = [NSMutableDictionary dictionary];
|
|
|
77 |
checkinPlistContents[kFIRMessagingDigestStringKey] = self.digest ?: @"";
|
|
|
78 |
checkinPlistContents[kFIRMessagingVersionInfoStringKey] = self.versionInfo ?: @"";
|
|
|
79 |
checkinPlistContents[kFIRMessagingDeviceDataVersionKey] = self.deviceDataVersion ?: @"";
|
|
|
80 |
checkinPlistContents[kFIRMessagingLastCheckinTimeKey] = @(self.lastCheckinTimestampMillis);
|
|
|
81 |
checkinPlistContents[kFIRMessagingGServicesDictionaryKey] =
|
|
|
82 |
[self.gServicesData count] ? self.gServicesData : @{};
|
|
|
83 |
return checkinPlistContents;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
- (BOOL)hasCheckinInfo {
|
|
|
87 |
return (self.deviceID.length && self.secretToken.length);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
- (BOOL)hasValidCheckinInfo {
|
|
|
91 |
int64_t currentTimestampInMillis = FIRMessagingCurrentTimestampInMilliseconds();
|
|
|
92 |
int64_t timeSinceLastCheckinInMillis = currentTimestampInMillis - self.lastCheckinTimestampMillis;
|
|
|
93 |
|
|
|
94 |
BOOL hasCheckinInfo = [self hasCheckinInfo];
|
|
|
95 |
NSString *lastLocale = [[GULUserDefaults standardUserDefaults]
|
|
|
96 |
stringForKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
|
|
|
97 |
// If it's app's first time open and checkin is already fetched and no locale information is
|
|
|
98 |
// stored, then checkin info is valid. We should not checkin again because locale is considered
|
|
|
99 |
// "changed".
|
|
|
100 |
if (hasCheckinInfo && !lastLocale) {
|
|
|
101 |
NSString *currentLocale = FIRMessagingCurrentLocale();
|
|
|
102 |
[[GULUserDefaults standardUserDefaults] setObject:currentLocale
|
|
|
103 |
forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
|
|
|
104 |
return YES;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// If locale has changed, checkin info is no longer valid.
|
|
|
108 |
// Also update locale information if changed. (Only do it here not in token refresh)
|
|
|
109 |
if (FIRMessagingHasLocaleChanged()) {
|
|
|
110 |
NSString *currentLocale = FIRMessagingCurrentLocale();
|
|
|
111 |
[[GULUserDefaults standardUserDefaults] setObject:currentLocale
|
|
|
112 |
forKey:kFIRMessagingInstanceIDUserDefaultsKeyLocale];
|
|
|
113 |
return NO;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return (hasCheckinInfo &&
|
|
|
117 |
(timeSinceLastCheckinInMillis / 1000.0 < kFIRMessagingDefaultCheckinInterval));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
- (void)setHasPreCachedAuthCredentials:(BOOL)hasPreCachedAuthCredentials {
|
|
|
121 |
_hasPreCachedAuthCredentials = hasPreCachedAuthCredentials;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
- (NSString *)checkinKeychainContent {
|
|
|
125 |
if ([self.deviceID length] && [self.secretToken length]) {
|
|
|
126 |
return [NSString stringWithFormat:@"%@%@%@", self.deviceID,
|
|
|
127 |
kCheckinKeychainContentSeparatorString, self.secretToken];
|
|
|
128 |
} else {
|
|
|
129 |
return nil;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
- (void)updateWithCheckinPlistContents:(NSDictionary *)checkinPlistContent {
|
|
|
134 |
for (NSString *key in checkinPlistContent) {
|
|
|
135 |
if ([kFIRMessagingDigestStringKey isEqualToString:key]) {
|
|
|
136 |
self.digest = [checkinPlistContent[key] copy];
|
|
|
137 |
} else if ([kFIRMessagingVersionInfoStringKey isEqualToString:key]) {
|
|
|
138 |
self.versionInfo = [checkinPlistContent[key] copy];
|
|
|
139 |
} else if ([kFIRMessagingLastCheckinTimeKey isEqualToString:key]) {
|
|
|
140 |
self.lastCheckinTimestampMillis = [checkinPlistContent[key] longLongValue];
|
|
|
141 |
} else if ([kFIRMessagingGServicesDictionaryKey isEqualToString:key]) {
|
|
|
142 |
self.gServicesData = [checkinPlistContent[key] mutableCopy];
|
|
|
143 |
} else if ([kFIRMessagingDeviceDataVersionKey isEqualToString:key]) {
|
|
|
144 |
self.deviceDataVersion = [checkinPlistContent[key] copy];
|
|
|
145 |
}
|
|
|
146 |
// Otherwise we have some keys we don't care about
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
+ (NSString *)checkinDeviceIDFromKeychainContent:(NSString *)keychainContent {
|
|
|
151 |
return [self checkinKeychainContent:keychainContent forIndex:0];
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
+ (NSString *)checkinSecretFromKeychainContent:(NSString *)keychainContent {
|
|
|
155 |
return [self checkinKeychainContent:keychainContent forIndex:1];
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
+ (NSString *)checkinKeychainContent:(NSString *)keychainContent forIndex:(int)index {
|
|
|
159 |
NSArray *keychainComponents =
|
|
|
160 |
[keychainContent componentsSeparatedByString:kCheckinKeychainContentSeparatorString];
|
|
|
161 |
if (index >= 0 && index < 2 && [keychainComponents count] == 2) {
|
|
|
162 |
return keychainComponents[index];
|
|
|
163 |
} else {
|
|
|
164 |
return nil;
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
@end
|