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