FUNContext.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Copyright 2017 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. #import "FUNContext.h"
  16. #import "FIRApp.h"
  17. #import "FIRAppInternal.h"
  18. #import "FUNInstanceIDProxy.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @interface FUNContext ()
  21. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  22. instanceIDToken:(NSString *_Nullable)instanceIDToken NS_DESIGNATED_INITIALIZER;
  23. @end
  24. @implementation FUNContext
  25. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  26. instanceIDToken:(NSString *_Nullable)instanceIDToken {
  27. self = [super init];
  28. if (self) {
  29. _authToken = [authToken copy];
  30. _instanceIDToken = [instanceIDToken copy];
  31. }
  32. return self;
  33. }
  34. @end
  35. @interface FUNContextProvider () {
  36. FIRApp *_app;
  37. FUNInstanceIDProxy *_instanceIDProxy;
  38. }
  39. @end
  40. @implementation FUNContextProvider
  41. - (instancetype)initWithApp:(FIRApp *)app {
  42. self = [super init];
  43. if (self) {
  44. _app = app;
  45. _instanceIDProxy = [[FUNInstanceIDProxy alloc] init];
  46. }
  47. return self;
  48. }
  49. // This is broken out so it can be mocked for tests.
  50. - (NSString *)instanceIDToken {
  51. return [_instanceIDProxy token];
  52. }
  53. - (void)getContext:(void (^)(FUNContext *_Nullable context, NSError *_Nullable error))completion {
  54. // Get the auth token.
  55. [_app getTokenForcingRefresh:NO
  56. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  57. if (error) {
  58. completion(nil, error);
  59. return;
  60. }
  61. // Get the instance id token.
  62. NSString *_Nullable instanceIDToken = [self instanceIDToken];
  63. FUNContext *context = [[FUNContext alloc] initWithAuthToken:token
  64. instanceIDToken:instanceIDToken];
  65. completion(context, nil);
  66. }];
  67. }
  68. @end
  69. NS_ASSUME_NONNULL_END