MainViewController+OAuth.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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+OAuth.h"
  17. #import "AppManager.h"
  18. #import "FIROAuthProvider.h"
  19. #import "MainViewController+Internal.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation MainViewController (OAuth)
  22. - (StaticContentTableViewSection *)oAuthSection {
  23. __weak typeof(self) weakSelf = self;
  24. return [StaticContentTableViewSection sectionWithTitle:@"OAuth" cells:@[
  25. [StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
  26. action:^{ [weakSelf signInGoogleHeadfulLite]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Sign In with Twitter (headful-lite)"
  28. action:^{ [weakSelf signInTwitterHeadfulLite]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Sign In with Linkedin"
  30. action:^{ [weakSelf signInLinkedinHeadfulLite]; }],
  31. [StaticContentTableViewCell cellWithTitle:@"Sign In with GitHub (access token)"
  32. action:^{ [weakSelf signInWithGitHub]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Sign In with GitHub (headful-lite)"
  34. action:^{ [weakSelf signInGitHubHeadfulLite]; }],
  35. [StaticContentTableViewCell cellWithTitle:@"Sign in with Microsoft"
  36. action:^{ [weakSelf signInMicrosoftHeadfulLite]; }],
  37. [StaticContentTableViewCell cellWithTitle:@"Sign In with Yahoo"
  38. action:^{ [weakSelf signInYahooHeadfulLite]; }],
  39. ]];
  40. }
  41. - (void)signInGoogleHeadfulLite {
  42. FIROAuthProvider *provider = self.googleOAuthProvider;
  43. provider.customParameters = @{
  44. @"prompt" : @"consent",
  45. };
  46. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  47. [self showSpinner:^{
  48. [[AppManager auth] signInWithProvider:provider
  49. UIDelegate:nil
  50. completion:^(FIRAuthDataResult *_Nullable authResult,
  51. NSError *_Nullable error) {
  52. [self hideSpinner:^{
  53. if (error) {
  54. [self logFailure:@"sign-in with provider (Google) failed" error:error];
  55. } else if (authResult.additionalUserInfo) {
  56. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  57. if (self.isNewUserToggleOn) {
  58. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  59. @"New user" : @"Existing user";
  60. [self showMessagePromptWithTitle:@"New or Existing"
  61. message:newUserString
  62. showCancelButton:NO
  63. completion:nil];
  64. }
  65. }
  66. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  67. }];
  68. }];
  69. }];
  70. }
  71. - (void)signInTwitterHeadfulLite {
  72. FIROAuthProvider *provider = self.twitterOAuthProvider;
  73. [self showSpinner:^{
  74. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  75. NSError *_Nullable error) {
  76. if (error) {
  77. [self logFailure:@"sign-in with Twitter failed" error:error];
  78. return;
  79. }
  80. [[AppManager auth] signInWithCredential:credential
  81. completion:^(FIRAuthDataResult *_Nullable
  82. authResult,
  83. NSError *_Nullable error) {
  84. [self hideSpinner:^{
  85. if (error) {
  86. [self logFailure:@"sign-in with Twitter (headful-lite) failed" error:error];
  87. return;
  88. } else {
  89. [self logSuccess:@"sign-in with Twitter (headful-lite) succeeded."];
  90. }
  91. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  92. }];
  93. }];
  94. }];
  95. }];
  96. }
  97. - (void)signInWithGitHub {
  98. [self showTextInputPromptWithMessage:@"GitHub Access Token:"
  99. completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
  100. if (!userPressedOK || !accessToken.length) {
  101. return;
  102. }
  103. FIROAuthCredential *credential =
  104. [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
  105. if (credential) {
  106. [[AppManager auth] signInWithCredential:credential
  107. completion:^(FIRAuthDataResult *_Nullable result,
  108. NSError *_Nullable error) {
  109. if (error) {
  110. [self logFailure:@"sign-in with provider failed" error:error];
  111. } else {
  112. [self logSuccess:@"sign-in with provider succeeded."];
  113. }
  114. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  115. }];
  116. }
  117. }];
  118. }
  119. - (void)signInGitHubHeadfulLite {
  120. FIROAuthProvider *provider = self.gitHubOAuthProvider;
  121. [self showSpinner:^{
  122. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  123. NSError *_Nullable error) {
  124. if (error) {
  125. [self logFailure:@"sign-in with GitHub failed" error:error];
  126. return;
  127. }
  128. [[AppManager auth] signInWithCredential:credential
  129. completion:^(FIRAuthDataResult *_Nullable
  130. authResult,
  131. NSError *_Nullable error) {
  132. [self hideSpinner:^{
  133. if (error) {
  134. [self logFailure:@"sign-in with GitHub (headful-lite) failed" error:error];
  135. return;
  136. } else {
  137. [self logSuccess:@"sign-in with GitHub (headful-lite) succeeded."];
  138. }
  139. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  140. }];
  141. }];
  142. }];
  143. }];
  144. }
  145. - (void)signInLinkedinHeadfulLite {
  146. FIROAuthProvider *provider = self.linkedinOAuthProvider;
  147. [self showSpinner:^{
  148. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  149. NSError *_Nullable error) {
  150. if (error) {
  151. [self logFailure:@"sign-in with Linkedin failed" error:error];
  152. return;
  153. }
  154. [[AppManager auth] signInWithCredential:credential
  155. completion:^(FIRAuthDataResult *_Nullable
  156. authResult,
  157. NSError *_Nullable error) {
  158. [self hideSpinner:^{
  159. if (error) {
  160. [self logFailure:@"sign-in with Linkedin (headful-lite) failed" error:error];
  161. return;
  162. } else {
  163. [self logSuccess:@"sign-in with Linkedin (headful-lite) succeeded."];
  164. }
  165. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  166. }];
  167. }];
  168. }];
  169. }];
  170. }
  171. - (void)signInMicrosoftHeadfulLite {
  172. FIROAuthProvider *provider = self.microsoftOAuthProvider;
  173. provider.customParameters = @{
  174. @"prompt" : @"consent",
  175. @"login_hint" : @"tu8731@gmail.com",
  176. };
  177. provider.scopes = @[ @"user.readwrite,calendars.read" ];
  178. [self showSpinner:^{
  179. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  180. NSError *_Nullable error) {
  181. if (error) {
  182. [self logFailure:@"sign-in with Microsoft failed" error:error];
  183. return;
  184. }
  185. [[AppManager auth] signInWithCredential:credential
  186. completion:^(FIRAuthDataResult *_Nullable
  187. authResult,
  188. NSError *_Nullable error) {
  189. [self hideSpinner:^{
  190. if (error) {
  191. [self logFailure:@"sign-in with Microsoft failed" error:error];
  192. return;
  193. } else {
  194. [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
  195. }
  196. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  197. }];
  198. }];
  199. }];
  200. }];
  201. }
  202. - (void)signInYahooHeadfulLite {
  203. FIROAuthProvider *provider = self.yahooOAuthProvider;
  204. [self showSpinner:^{
  205. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  206. NSError *_Nullable error) {
  207. if (error) {
  208. [self logFailure:@"sign-in with Yahoo failed" error:error];
  209. return;
  210. }
  211. [[AppManager auth] signInWithCredential:credential
  212. completion:^(FIRAuthDataResult *_Nullable
  213. authResult,
  214. NSError *_Nullable error) {
  215. [self hideSpinner:^{
  216. if (error) {
  217. [self logFailure:@"sign-in with Yahoo (headful-lite) failed" error:error];
  218. return;
  219. } else {
  220. [self logSuccess:@"sign-in with Yahoo (headful-lite) succeeded."];
  221. }
  222. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  223. }];
  224. }];
  225. }];
  226. }];
  227. }
  228. @end
  229. NS_ASSUME_NONNULL_END