MainViewController+App.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. NSMutableString *message = [[NSMutableString alloc] initWithString:
  67. [NSString stringWithFormat:@"Token : %@\n", tokenResult.token]];
  68. [message appendString:[NSString stringWithFormat:@"Auth Date : %@\n", tokenResult.authDate]];
  69. [message appendString:[NSString stringWithFormat:@"EXP Date : %@\n", tokenResult.expirationDate]];
  70. [message appendString:[NSString stringWithFormat:@"Issued Date : %@\n", tokenResult.issuedAtDate]];
  71. [self showMessagePromptWithTitle:kTokenRefreshedAlertTitle
  72. message:message
  73. showCancelButton:NO
  74. completion:nil];
  75. }];
  76. }
  77. - (void)addAuthStateListener {
  78. __weak typeof(self) weakSelf = self;
  79. NSUInteger index = self.authStateDidChangeListeners.count;
  80. [self log:[NSString stringWithFormat:@"Auth State Did Change Listener #%lu was added.",
  81. (unsigned long)index]];
  82. FIRAuthStateDidChangeListenerHandle handle =
  83. [[AppManager auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth,
  84. FIRUser *_Nullable user) {
  85. [weakSelf log:[NSString stringWithFormat:
  86. @"Auth State Did Change Listener #%lu was invoked on user '%@'.",
  87. (unsigned long)index, user.uid]];
  88. }];
  89. [self.authStateDidChangeListeners addObject:handle];
  90. }
  91. - (void)removeAuthStateListener {
  92. if (!self.authStateDidChangeListeners.count) {
  93. [self log:@"No remaining Auth State Did Change Listeners."];
  94. return;
  95. }
  96. NSUInteger index = self.authStateDidChangeListeners.count - 1;
  97. FIRAuthStateDidChangeListenerHandle handle = self.authStateDidChangeListeners.lastObject;
  98. [[AppManager auth] removeAuthStateDidChangeListener:handle];
  99. [self.authStateDidChangeListeners removeObject:handle];
  100. NSString *logString =
  101. [NSString stringWithFormat:@"Auth State Did Change Listener #%lu was removed.",
  102. (unsigned long)index];
  103. [self log:logString];
  104. }
  105. - (void)addIDTokenListener {
  106. __weak typeof(self) weakSelf = self;
  107. NSUInteger index = self.IDTokenDidChangeListeners.count;
  108. [self log:[NSString stringWithFormat:@"ID Token Did Change Listener #%lu was added.",
  109. (unsigned long)index]];
  110. FIRIDTokenDidChangeListenerHandle handle =
  111. [[AppManager auth] addIDTokenDidChangeListener:^(FIRAuth *_Nonnull auth,
  112. FIRUser *_Nullable user) {
  113. [weakSelf log:[NSString stringWithFormat:
  114. @"ID Token Did Change Listener #%lu was invoked on user '%@'.",
  115. (unsigned long)index, user.uid]];
  116. }];
  117. [self.IDTokenDidChangeListeners addObject:handle];
  118. }
  119. - (void)removeIDTokenListener {
  120. if (!self.IDTokenDidChangeListeners.count) {
  121. [self log:@"No remaining ID Token Did Change Listeners."];
  122. return;
  123. }
  124. NSUInteger index = self.IDTokenDidChangeListeners.count - 1;
  125. FIRIDTokenDidChangeListenerHandle handle = self.IDTokenDidChangeListeners.lastObject;
  126. [[AppManager auth] removeIDTokenDidChangeListener:handle];
  127. [self.IDTokenDidChangeListeners removeObject:handle];
  128. NSString *logString =
  129. [NSString stringWithFormat:@"ID Token Did Change Listener #%lu was removed.",
  130. (unsigned long)index];
  131. [self log:logString];
  132. }
  133. - (void)verifyClient {
  134. [[AppManager auth].tokenManager getTokenWithCallback:^(FIRAuthAPNSToken *_Nullable token,
  135. NSError *_Nullable error) {
  136. if (!token) {
  137. [self logFailure:@"Verify iOS client failed." error:error];
  138. return;
  139. }
  140. FIRVerifyClientRequest *request =
  141. [[FIRVerifyClientRequest alloc] initWithAppToken:token.string
  142. isSandbox:token.type == FIRAuthAPNSTokenTypeSandbox
  143. requestConfiguration:[AppManager auth].requestConfiguration];
  144. [FIRAuthBackend verifyClient:request callback:^(FIRVerifyClientResponse *_Nullable response,
  145. NSError *_Nullable error) {
  146. if (error) {
  147. [self logFailure:@"Verify iOS client failed." error:error];
  148. return;
  149. }
  150. NSTimeInterval timeout = [response.suggestedTimeOutDate timeIntervalSinceNow];
  151. [[AppManager auth].appCredentialManager
  152. didStartVerificationWithReceipt:response.receipt
  153. timeout:timeout
  154. callback:^(FIRAuthAppCredential *credential) {
  155. if (!credential.secret) {
  156. [self logFailure:@"Failed to receive remote notification to verify app identity."
  157. error:error];
  158. return;
  159. }
  160. NSString *testPhoneNumber = @"+16509964692";
  161. FIRSendVerificationCodeRequest *request =
  162. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:testPhoneNumber
  163. appCredential:credential
  164. reCAPTCHAToken:nil
  165. requestConfiguration:
  166. [AppManager auth].requestConfiguration];
  167. [FIRAuthBackend sendVerificationCode:request
  168. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  169. NSError *_Nullable error) {
  170. if (error) {
  171. [self logFailure:@"Verify iOS client failed." error:error];
  172. return;
  173. } else {
  174. [self logSuccess:@"Verify iOS client succeeded."];
  175. [self showMessagePrompt:@"Verify iOS client succeed."];
  176. }
  177. }];
  178. }];
  179. }];
  180. }];
  181. }
  182. - (void)deleteApp {
  183. [[FIRApp defaultApp] deleteApp:^(BOOL success) {
  184. [self log:success ? @"App deleted successfully." : @"Failed to delete app."];
  185. }];
  186. }
  187. @end