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 "FirebaseInstallations/Source/Library/FIRInstallationsItem.h"
|
|
|
18 |
|
|
|
19 |
#import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
|
|
|
20 |
#import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredItem.h"
|
|
|
21 |
|
|
|
22 |
#import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
|
|
|
23 |
|
|
|
24 |
@implementation FIRInstallationsItem
|
|
|
25 |
|
|
|
26 |
- (instancetype)initWithAppID:(NSString *)appID firebaseAppName:(NSString *)firebaseAppName {
|
|
|
27 |
self = [super init];
|
|
|
28 |
if (self) {
|
|
|
29 |
_appID = [appID copy];
|
|
|
30 |
_firebaseAppName = [firebaseAppName copy];
|
|
|
31 |
}
|
|
|
32 |
return self;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
|
|
|
36 |
FIRInstallationsItem *clone = [[FIRInstallationsItem alloc] initWithAppID:self.appID
|
|
|
37 |
firebaseAppName:self.firebaseAppName];
|
|
|
38 |
clone.firebaseInstallationID = [self.firebaseInstallationID copy];
|
|
|
39 |
clone.refreshToken = [self.refreshToken copy];
|
|
|
40 |
clone.authToken = [self.authToken copy];
|
|
|
41 |
clone.registrationStatus = self.registrationStatus;
|
|
|
42 |
clone.IIDDefaultToken = [self.IIDDefaultToken copy];
|
|
|
43 |
|
|
|
44 |
return clone;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
- (void)updateWithStoredItem:(FIRInstallationsStoredItem *)item {
|
|
|
48 |
self.firebaseInstallationID = item.firebaseInstallationID;
|
|
|
49 |
self.refreshToken = item.refreshToken;
|
|
|
50 |
self.authToken = item.authToken;
|
|
|
51 |
self.registrationStatus = item.registrationStatus;
|
|
|
52 |
self.IIDDefaultToken = item.IIDDefaultToken;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
- (FIRInstallationsStoredItem *)storedItem {
|
|
|
56 |
FIRInstallationsStoredItem *storedItem = [[FIRInstallationsStoredItem alloc] init];
|
|
|
57 |
storedItem.firebaseInstallationID = self.firebaseInstallationID;
|
|
|
58 |
storedItem.refreshToken = self.refreshToken;
|
|
|
59 |
storedItem.authToken = self.authToken;
|
|
|
60 |
storedItem.registrationStatus = self.registrationStatus;
|
|
|
61 |
storedItem.IIDDefaultToken = self.IIDDefaultToken;
|
|
|
62 |
return storedItem;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
- (nonnull NSString *)identifier {
|
|
|
66 |
return [[self class] identifierWithAppID:self.appID appName:self.firebaseAppName];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
- (BOOL)isValid:(NSError *_Nullable *)outError {
|
|
|
70 |
NSMutableArray<NSString *> *validationIssues = [NSMutableArray array];
|
|
|
71 |
|
|
|
72 |
if (self.appID.length == 0) {
|
|
|
73 |
[validationIssues addObject:@"`appID` must not be empty"];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if (self.firebaseAppName.length == 0) {
|
|
|
77 |
[validationIssues addObject:@"`firebaseAppName` must not be empty"];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (self.firebaseInstallationID.length == 0) {
|
|
|
81 |
[validationIssues addObject:@"`firebaseInstallationID` must not be empty"];
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
switch (self.registrationStatus) {
|
|
|
85 |
case FIRInstallationStatusUnknown:
|
|
|
86 |
[validationIssues addObject:@"invalid `registrationStatus`"];
|
|
|
87 |
break;
|
|
|
88 |
|
|
|
89 |
case FIRInstallationStatusRegistered:
|
|
|
90 |
if (self.refreshToken == 0) {
|
|
|
91 |
[validationIssues addObject:@"registered installation must have non-empty `refreshToken`"];
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if (self.authToken.token == 0) {
|
|
|
95 |
[validationIssues
|
|
|
96 |
addObject:@"registered installation must have non-empty `authToken.token`"];
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (self.authToken.expirationDate == nil) {
|
|
|
100 |
[validationIssues
|
|
|
101 |
addObject:@"registered installation must have non-empty `authToken.expirationDate`"];
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
case FIRInstallationStatusUnregistered:
|
|
|
105 |
break;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
BOOL isValid = validationIssues.count == 0;
|
|
|
109 |
|
|
|
110 |
if (!isValid && outError) {
|
|
|
111 |
NSString *failureReason =
|
|
|
112 |
[NSString stringWithFormat:@"FIRInstallationsItem validation errors: %@",
|
|
|
113 |
[validationIssues componentsJoinedByString:@", "]];
|
|
|
114 |
*outError =
|
|
|
115 |
[FIRInstallationsErrorUtil installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
|
|
|
116 |
failureReason:failureReason
|
|
|
117 |
underlyingError:nil];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return isValid;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
+ (NSString *)identifierWithAppID:(NSString *)appID appName:(NSString *)appName {
|
|
|
124 |
return [appID stringByAppendingString:appName];
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
+ (NSString *)generateFID {
|
|
|
128 |
NSUUID *UUID = [NSUUID UUID];
|
|
|
129 |
uuid_t UUIDBytes;
|
|
|
130 |
[UUID getUUIDBytes:UUIDBytes];
|
|
|
131 |
|
|
|
132 |
NSUInteger UUIDLength = sizeof(uuid_t);
|
|
|
133 |
NSData *UUIDData = [NSData dataWithBytes:UUIDBytes length:UUIDLength];
|
|
|
134 |
|
|
|
135 |
uint8_t UUIDLast4Bits = UUIDBytes[UUIDLength - 1] & 0b00001111;
|
|
|
136 |
|
|
|
137 |
// FID first 4 bits must be `0111`. The last 4 UUID bits will be cut later to form a proper FID.
|
|
|
138 |
// To keep 16 random bytes we copy these last 4 UUID to the FID 1st byte after `0111` prefix.
|
|
|
139 |
uint8_t FIDPrefix = 0b01110000 | UUIDLast4Bits;
|
|
|
140 |
NSMutableData *FIDData = [NSMutableData dataWithBytes:&FIDPrefix length:1];
|
|
|
141 |
|
|
|
142 |
[FIDData appendData:UUIDData];
|
|
|
143 |
NSString *FIDString = [self base64URLEncodedStringWithData:FIDData];
|
|
|
144 |
|
|
|
145 |
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5 bytes.
|
|
|
146 |
// Our generated ID has 16 bytes UUID + 1 byte prefix which after encoding with base64 will become
|
|
|
147 |
// 23 characters plus 1 character for "=" padding.
|
|
|
148 |
|
|
|
149 |
// Remove the 23rd character that was added because of the extra 4 bits at the
|
|
|
150 |
// end of our 17 byte data and the '=' padding.
|
|
|
151 |
return [FIDString substringWithRange:NSMakeRange(0, 22)];
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
+ (NSString *)base64URLEncodedStringWithData:(NSData *)data {
|
|
|
155 |
NSString *string = [data base64EncodedStringWithOptions:0];
|
|
|
156 |
string = [string stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
|
|
|
157 |
string = [string stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
|
|
|
158 |
return string;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
@end
|