aboutsummaryrefslogtreecommitdiffhomepage
path: root/AuthSamples/Sample/SettingsViewController.m
blob: e7be9a6ebd5dba6acc559d5284ba741c97458e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "SettingsViewController.h"

#import <objc/runtime.h>

#import "FIRApp.h"
#import "FIROptions.h"
#import "FirebaseAuth.h"
#import "StaticContentTableViewManager.h"
#import "UIViewController+Alerts.h"

#if INTERNAL_GOOGLE3_BUILD
#import "googlemac/iPhone/Identity/Firebear/Auth/Source/FIRAuth_Internal.h"
#import "googlemac/iPhone/Identity/Firebear/Auth/Source/FIRAuthAPNSToken.h"
#import "googlemac/iPhone/Identity/Firebear/Auth/Source/FIRAuthAPNSTokenManager.h"
#import "googlemac/iPhone/Identity/Firebear/Auth/Source/FIRAuthAppCredential.h"
#import "googlemac/iPhone/Identity/Firebear/Auth/Source/FIRAuthAppCredentialManager.h"
#import "googlemac/iPhone/InstanceID/Firebase/Lib/Source/FIRInstanceID+Internal.h"
#else
@interface FIRInstanceID : NSObject
+ (void)notifyTokenRefresh;
@end
#endif

/** @var kIdentityToolkitRequestClassName
    @brief The class name of Identity Toolkit requests.
 */
static NSString *const kIdentityToolkitRequestClassName = @"FIRIdentityToolkitRequest";

/** @var kSecureTokenRequestClassName
    @brief The class name of Secure Token Service requests.
 */
static NSString *const kSecureTokenRequestClassName = @"FIRSecureTokenRequest";

/** @var kIdentityToolkitSandboxHost
    @brief The host of Identity Toolkit sandbox server.
 */
static NSString *const kIdentityToolkitSandboxHost = @"www-googleapis-staging.sandbox.google.com";

/** @var kSecureTokenSandboxHost
    @brief The host of Secure Token Service sandbox server.
 */
static NSString *const kSecureTokenSandboxHost = @"staging-securetoken.sandbox.googleapis.com";

/** @var kGoogleServiceInfoPlists
    @brief a C-array of plist file base names of Google service info to initialize FIRApp.
 */
static NSString *const kGoogleServiceInfoPlists[] = {
  @"GoogleService-Info",
  @"GoogleService-Info_multi"
};

/** @var gAPIEndpoints
    @brief List of API Hosts by request class name.
 */
static NSDictionary<NSString *, NSArray<NSString *> *> *gAPIHosts;

/** @var gFirebaseAppOptions
    @brief List of FIROptions.
 */
static NSArray<FIROptions *> *gFirebaseAppOptions;

/** @protocol RequestClass
    @brief A de-facto protocol followed by request class objects to access its API host.
 */
@protocol RequestClass <NSObject>
- (NSString *)host;
- (void)setHost:(NSString *)host;
@end

/** @category FIROptions(ProjectID)
    @brief A category to FIROption to add the project ID property.
 */
@interface FIROptions (ProjectID)

/** @property projectID
    @brief The Firebase project ID.
 */
@property(nonatomic, copy) NSString *projectID;

@end

@implementation FIROptions (ProjectID)

- (NSString *)projectID {
  return objc_getAssociatedObject(self, @selector(projectID));
}

- (void)setProjectID:(NSString *)projectID {
  objc_setAssociatedObject(self, @selector(projectID), projectID, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end

/** @fn versionString
    @brief Constructs a version string to display.
    @param string The version in string form.
    @param number The version in number form.
 */
static NSString *versionString(const unsigned char *string, const double number) {
  return [NSString stringWithFormat:@"\"%s\" (%g)", string, number];
}

/** @fn requestHost
    @brief Retrieves the API host for the request class.
    @param requestClassName The name of the request class.
 */
static NSString *APIHost(NSString *requestClassName) {
  return [(id<RequestClass>)NSClassFromString(requestClassName) host];
}

/** @fn truncatedString
    @brief Truncates a string under a maximum length.
    @param string The original string to be truncated.
    @param length The maximum length of the truncated string.
    @return The truncated string, which is not longer than @c length.
 */
static NSString *truncatedString(NSString *string, NSUInteger length) {
  if (string.length <= length) {
    return string;
  }
  NSUInteger half = (length - 3) / 2;
  return [NSString stringWithFormat:@"%@...%@",
                                    [string substringToIndex:half],
                                    [string substringFromIndex:string.length - half]];
}

/** @fn hexString
    @brief Converts a piece of data into a hexadecimal string.
    @param data The raw data.
    @return The hexadecimal string representation of the data.
 */
static NSString *hexString(NSData *data) {
  NSMutableString *string = [NSMutableString stringWithCapacity:data.length * 2];
  const unsigned char *bytes = data.bytes;
  for (int idx = 0; idx < data.length; ++idx) {
    [string appendFormat:@"%02X", (int)bytes[idx]];
  }
  return string;
}

@implementation SettingsViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setUpAPIHosts];
  [self setUpFirebaseAppOptions];
  [self loadTableView];
}

- (IBAction)done:(id)sender {
  [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setUpAPIHosts {
  if (gAPIHosts) {
    return;
  }
  gAPIHosts = @{
    kIdentityToolkitRequestClassName : @[
      APIHost(kIdentityToolkitRequestClassName),
      kIdentityToolkitSandboxHost,
    ],
    kSecureTokenRequestClassName : @[
      APIHost(kSecureTokenRequestClassName),
      kSecureTokenSandboxHost,
    ],
  };
}

- (void)setUpFirebaseAppOptions {
  if (gFirebaseAppOptions) {
    return;
  }
  int numberOfOptions = sizeof(kGoogleServiceInfoPlists) / sizeof(*kGoogleServiceInfoPlists);
  NSMutableArray *appOptions = [[NSMutableArray alloc] initWithCapacity:numberOfOptions];
  for (int i = 0; i < numberOfOptions; i++) {
    NSString *plistFileName = kGoogleServiceInfoPlists[i];
    NSString *plistFilePath = [[NSBundle mainBundle] pathForResource:plistFileName
                                                              ofType:@"plist"];
    NSDictionary *optionsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
    FIROptions *options = [[FIROptions alloc]
        initWithGoogleAppID:optionsDictionary[@"GOOGLE_APP_ID"]
                   bundleID:optionsDictionary[@"BUNDLE_ID"]
                GCMSenderID:optionsDictionary[@"GCM_SENDER_ID"]
                     APIKey:optionsDictionary[@"API_KEY"]
                   clientID:optionsDictionary[@"CLIENT_ID"]
                 trackingID:optionsDictionary[@"TRACKING_ID"]
            androidClientID:optionsDictionary[@"ANDROID_CLIENT_ID"]
                databaseURL:optionsDictionary[@"DATABASE_URL"]
              storageBucket:optionsDictionary[@"STORAGE_BUCKET"]
          deepLinkURLScheme:nil];
    options.projectID = optionsDictionary[@"PROJECT_ID"];
    [appOptions addObject:options];
  }
  gFirebaseAppOptions = [appOptions copy];
}

- (void)loadTableView {
  __weak typeof(self) weakSelf = self;
  _tableViewManager.contents = [StaticContentTableViewContent contentWithSections:@[
    [StaticContentTableViewSection sectionWithTitle:@"Versions" cells:@[
      [StaticContentTableViewCell cellWithTitle:@"FirebaseAuth"
                                          value:versionString(
          FirebaseAuthVersionString, FirebaseAuthVersionNumber)],
    ]],
    [StaticContentTableViewSection sectionWithTitle:@"Client Identity" cells:@[
      [StaticContentTableViewCell cellWithTitle:@"Project"
                                          value:[self currentProjectID]
                                         action:^{
        [weakSelf toggleClientProject];
      }],
    ]],
    [StaticContentTableViewSection sectionWithTitle:@"API Hosts" cells:@[
      [StaticContentTableViewCell cellWithTitle:@"Identity Toolkit"
                                          value:APIHost(kIdentityToolkitRequestClassName)
                                         action:^{
        [weakSelf toggleAPIHostWithRequestClassName:kIdentityToolkitRequestClassName];
      }],
      [StaticContentTableViewCell cellWithTitle:@"Secure Token"
                                          value:APIHost(kSecureTokenRequestClassName)
                                         action:^{
        [weakSelf toggleAPIHostWithRequestClassName:kSecureTokenRequestClassName];
      }],
    ]],
#if INTERNAL_GOOGLE3_BUILD
    [StaticContentTableViewSection sectionWithTitle:@"Phone Auth" cells:@[
      [StaticContentTableViewCell cellWithTitle:@"APNs Token"
                                          value:[self APNSTokenString]
                                         action:^{
        [weakSelf clearAPNSToken];
      }],
      [StaticContentTableViewCell cellWithTitle:@"App Credential"
                                          value:[self appCredentialString]
                                         action:^{
        [weakSelf clearAppCredential];
      }],
    ]],
#endif  // INTERNAL_GOOGLE3_BUILD
  ]];
}

/** @fn toggleAPIHostWithRequestClassName:
    @brief Toggles the host name of the server that handles RPCs.
    @param requestClassName The name of the RPC request class.
 */
- (void)toggleAPIHostWithRequestClassName:(NSString *)requestClassName {
  NSString *currentHost = APIHost(requestClassName);
  NSArray<NSString *> *allHosts = gAPIHosts[requestClassName];
  NSString *newHost = allHosts[([allHosts indexOfObject:currentHost] + 1) % allHosts.count];
  [(id<RequestClass>)NSClassFromString(requestClassName) setHost:newHost];
  [self loadTableView];
}

/** @fn currentProjectID
    @brief Returns the the current Firebase project ID.
 */
- (NSString *)currentProjectID {
  NSString *APIKey = [FIRApp defaultApp].options.APIKey;
  for (FIROptions *options in gFirebaseAppOptions) {
    if ([options.APIKey isEqualToString:APIKey]) {
      return options.projectID;
    }
  }
  return nil;
}

/** @fn toggleClientProject
    @brief Toggles the Firebase/Google project this client presents by recreating the default
        FIRApp instance with different options.
 */
- (void)toggleClientProject {
  NSString *APIKey = [FIRApp defaultApp].options.APIKey;
  for (NSUInteger i = 0 ; i < gFirebaseAppOptions.count; i++) {
    FIROptions *options = gFirebaseAppOptions[i];
    if ([options.APIKey isEqualToString:APIKey]) {
      __weak typeof(self) weakSelf = self;
      [[FIRApp defaultApp] deleteApp:^(BOOL success) {
        if (success) {
          [FIRInstanceID notifyTokenRefresh];  // b/28967043
          dispatch_async(dispatch_get_main_queue(), ^() {
            FIROptions *options = gFirebaseAppOptions[(i + 1) % gFirebaseAppOptions.count];
            [FIRApp configureWithOptions:options];
            [weakSelf loadTableView];
          });
        }
      }];
      return;
    }
  }
}

#if INTERNAL_GOOGLE3_BUILD

/** @fn APNSTokenString
    @brief Returns a string representing APNS token.
 */
- (NSString *)APNSTokenString {
  FIRAuthAPNSToken *token = [FIRAuth auth].tokenManager.token;
  if (!token) {
    return @"";
  }
  return [NSString stringWithFormat:@"%@(%@)",
                                    truncatedString(hexString(token.data), 19),
                                    token.type == FIRAuthAPNSTokenTypeProd ? @"P" : @"S"];
}

/** @fn clearAPNSToken
    @brief Clears the saved app credential.
 */
- (void)clearAPNSToken {
  FIRAuthAPNSToken *token = [FIRAuth auth].tokenManager.token;
  if (!token) {
    return;
  }
  NSString *tokenType = token.type == FIRAuthAPNSTokenTypeProd ? @"Production" : @"Sandbox";
  NSString *message = [NSString stringWithFormat:@"token: %@\ntype: %@",
                                                 hexString(token.data), tokenType];
  [self showMessagePromptWithTitle:@"Clear APNs Token?"
                           message:message
                  showCancelButton:YES
                        completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
    if (userPressedOK) {
      [FIRAuth auth].tokenManager.token = nil;
      [self loadTableView];
    }
  }];
}

/** @fn appCredentialString
    @brief Returns a string representing app credential.
 */
- (NSString *)appCredentialString {
  FIRAuthAppCredential *credential = [FIRAuth auth].appCredentialManager.credential;
  if (!credential) {
    return @"";
  }
  return [NSString stringWithFormat:@"%@/%@",
                                    truncatedString(credential.receipt, 13),
                                    truncatedString(credential.secret, 13)];
}

/** @fn clearAppCredential
    @brief Clears the saved app credential.
 */
- (void)clearAppCredential {
  FIRAuthAppCredential *credential = [FIRAuth auth].appCredentialManager.credential;
  if (!credential) {
    return;
  }
  NSString *message = [NSString stringWithFormat:@"receipt: %@\nsecret: %@",
                                                 credential.receipt, credential.secret];
  [self showMessagePromptWithTitle:@"Clear App Credential?"
                           message:message
                  showCancelButton:YES
                        completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
    if (userPressedOK) {
      [[FIRAuth auth].appCredentialManager clearCredential];
      [self loadTableView];
    }
  }];
}

#endif  // INTERNAL_GOOGLE3_BUILD

@end