MainViewController+App.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #import "MainViewController+App.h"
  17. #import "AppManager.h"
  18. #import "FIRAuthAPNSToken.h"
  19. #import "FIRAuthAPNSTokenManager.h"
  20. #import "FIRAuthAppCredential.h"
  21. #import "FIRAuthAppCredentialManager.h"
  22. #import "FIRAuth_Internal.h"
  23. #import "MainViewController+Internal.h"
  24. #import "FIRVerifyClientRequest.h"
  25. #import "FIRVerifyClientResponse.h"
  26. #import "FIRSendVerificationCodeRequest.h"
  27. #import "FIRAuthBackend.h"
  28. #import "FIRApp.h"
  29. static NSString *const kTokenRefreshErrorAlertTitle = @"Get Token Error";
  30. static NSString *const kTokenRefreshedAlertTitle = @"Token";
  31. @implementation MainViewController (App)
  32. - (StaticContentTableViewSection *)appSection {
  33. __weak typeof(self) weakSelf = self;
  34. return [StaticContentTableViewSection sectionWithTitle:@"APP" cells:@[
  35. [StaticContentTableViewCell cellWithTitle:@"Get Token"
  36. action:^{ [weakSelf getUserTokenResultWithForce:NO]; }],
  37. [StaticContentTableViewCell cellWithTitle:@"Get Token Force Refresh"
  38. action:^{ [weakSelf getUserTokenResultWithForce:YES]; }],
  39. [StaticContentTableViewCell cellWithTitle:@"Add Auth State Change Listener"
  40. action:^{ [weakSelf addAuthStateListener]; }],
  41. [StaticContentTableViewCell cellWithTitle:@"Remove Last Auth State Change Listener"
  42. action:^{ [weakSelf removeAuthStateListener]; }],
  43. [StaticContentTableViewCell cellWithTitle:@"Add ID Token Change Listener"
  44. action:^{ [weakSelf addIDTokenListener]; }],
  45. [StaticContentTableViewCell cellWithTitle:@"Remove Last ID Token Change Listener"
  46. action:^{ [weakSelf removeIDTokenListener]; }],
  47. [StaticContentTableViewCell cellWithTitle:@"Verify Client"
  48. action:^{ [weakSelf verifyClient]; }],
  49. [StaticContentTableViewCell cellWithTitle:@"Delete App"
  50. action:^{ [weakSelf deleteApp]; }],
  51. ]];
  52. }
  53. - (void)getUserTokenResultWithForce:(BOOL)force {
  54. [[self user] getIDTokenResultForcingRefresh:force
  55. completion:^(FIRAuthTokenResult *_Nullable tokenResult,
  56. NSError *_Nullable error) {
  57. if (error) {
  58. [self showMessagePromptWithTitle:kTokenRefreshErrorAlertTitle
  59. message:error.localizedDescription
  60. showCancelButton:NO
  61. completion:nil];
  62. [self logFailure:@"refresh token failed" error:error];
  63. return;
  64. }
  65. [self logSuccess:@"refresh token succeeded."];
  66. NSString *message = [tokenResult.claims description];
  67. [self showMessagePromptWithTitle:kTokenRefreshedAlertTitle
  68. message:message
  69. showCancelButton:NO
  70. completion:nil];
  71. }];
  72. }
  73. - (void)addAuthStateListener {
  74. __weak typeof(self) weakSelf = self;
  75. NSUInteger index = self.authStateDidChangeListeners.count;
  76. [self log:[NSString stringWithFormat:@"Auth State Did Change Listener #%lu was added.",
  77. (unsigned long)index]];
  78. FIRAuthStateDidChangeListenerHandle handle =
  79. [[AppManager auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth,
  80. FIRUser *_Nullable user) {
  81. [weakSelf log:[NSString stringWithFormat:
  82. @"Auth State Did Change Listener #%lu was invoked on user '%@'.",
  83. (unsigned long)index, user.uid]];
  84. }];
  85. [self.authStateDidChangeListeners addObject:handle];
  86. }
  87. - (void)removeAuthStateListener {
  88. if (!self.authStateDidChangeListeners.count) {
  89. [self log:@"No remaining Auth State Did Change Listeners."];
  90. return;
  91. }
  92. NSUInteger index = self.authStateDidChangeListeners.count - 1;
  93. FIRAuthStateDidChangeListenerHandle handle = self.authStateDidChangeListeners.lastObject;
  94. [[AppManager auth] removeAuthStateDidChangeListener:handle];
  95. [self.authStateDidChangeListeners removeObject:handle];
  96. NSString *logString =
  97. [NSString stringWithFormat:@"Auth State Did Change Listener #%lu was removed.",
  98. (unsigned long)index];
  99. [self log:logString];
  100. }
  101. - (void)addIDTokenListener {
  102. __weak typeof(self) weakSelf = self;
  103. NSUInteger index = self.IDTokenDidChangeListeners.count;
  104. [self log:[NSString stringWithFormat:@"ID Token Did Change Listener #%lu was added.",
  105. (unsigned long)index]];
  106. FIRIDTokenDidChangeListenerHandle handle =
  107. [[AppManager auth] addIDTokenDidChangeListener:^(FIRAuth *_Nonnull auth,
  108. FIRUser *_Nullable user) {
  109. [weakSelf log:[NSString stringWithFormat:
  110. @"ID Token Did Change Listener #%lu was invoked on user '%@'.",
  111. (unsigned long)index, user.uid]];
  112. }];
  113. [self.IDTokenDidChangeListeners addObject:handle];
  114. }
  115. - (void)removeIDTokenListener {
  116. if (!self.IDTokenDidChangeListeners.count) {
  117. [self log:@"No remaining ID Token Did Change Listeners."];
  118. return;
  119. }
  120. NSUInteger index = self.IDTokenDidChangeListeners.count - 1;
  121. FIRIDTokenDidChangeListenerHandle handle = self.IDTokenDidChangeListeners.lastObject;
  122. [[AppManager auth] removeIDTokenDidChangeListener:handle];
  123. [self.IDTokenDidChangeListeners removeObject:handle];
  124. NSString *logString =
  125. [NSString stringWithFormat:@"ID Token Did Change Listener #%lu was removed.",
  126. (unsigned long)index];
  127. [self log:logString];
  128. }
  129. - (void)verifyClient {
  130. [[AppManager auth].tokenManager getTokenWithCallback:^(FIRAuthAPNSToken *_Nullable token,
  131. NSError *_Nullable error) {
  132. if (!token) {
  133. [self logFailure:@"Verify iOS client failed." error:error];
  134. return;
  135. }
  136. FIRVerifyClientRequest *request =
  137. [[FIRVerifyClientRequest alloc] initWithAppToken:token.string
  138. isSandbox:token.type == FIRAuthAPNSTokenTypeSandbox
  139. requestConfiguration:[AppManager auth].requestConfiguration];
  140. [FIRAuthBackend verifyClient:request callback:^(FIRVerifyClientResponse *_Nullable response,
  141. NSError *_Nullable error) {
  142. if (error) {
  143. [self logFailure:@"Verify iOS client failed." error:error];
  144. return;
  145. }
  146. NSTimeInterval timeout = [response.suggestedTimeOutDate timeIntervalSinceNow];
  147. [[AppManager auth].appCredentialManager
  148. didStartVerificationWithReceipt:response.receipt
  149. timeout:timeout
  150. callback:^(FIRAuthAppCredential *credential) {
  151. if (!credential.secret) {
  152. [self logFailure:@"Failed to receive remote notification to verify app identity."
  153. error:error];
  154. return;
  155. }
  156. NSString *testPhoneNumber = @"+16509964692";
  157. FIRSendVerificationCodeRequest *request =
  158. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:testPhoneNumber
  159. appCredential:credential
  160. reCAPTCHAToken:nil
  161. requestConfiguration:
  162. [AppManager auth].requestConfiguration];
  163. [FIRAuthBackend sendVerificationCode:request
  164. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  165. NSError *_Nullable error) {
  166. if (error) {
  167. [self logFailure:@"Verify iOS client failed." error:error];
  168. return;
  169. } else {
  170. [self logSuccess:@"Verify iOS client succeeded."];
  171. [self showMessagePrompt:@"Verify iOS client succeed."];
  172. }
  173. }];
  174. }];
  175. }];
  176. }];
  177. }
  178. - (void)deleteApp {
  179. [[FIRApp defaultApp] deleteApp:^(BOOL success) {
  180. [self log:success ? @"App deleted successfully." : @"Failed to delete app."];
  181. }];
  182. }
  183. @end