MainViewController+OAuth.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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:@"Link with Google"
  28. action:^{ [weakSelf linkWithGoogleHeadfulLite]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate with Google"
  30. action:^{ [weakSelf reauthenticateWithGoogleHeadfulLite]; }],
  31. [StaticContentTableViewCell cellWithTitle:@"Sign in with Twitter"
  32. action:^{ [weakSelf signInTwitterHeadfulLite]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub"
  34. action:^{ [weakSelf signInGitHubHeadfulLite]; }],
  35. [StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub (Access token)"
  36. action:^{ [weakSelf signInWithGitHub]; }],
  37. [StaticContentTableViewCell cellWithTitle:@"Sign in with Microsoft"
  38. action:^{ [weakSelf signInMicrosoftHeadfulLite]; }],
  39. [StaticContentTableViewCell cellWithTitle:@"Sign in with Yahoo"
  40. action:^{ [weakSelf signInYahooHeadfulLite]; }],
  41. [StaticContentTableViewCell cellWithTitle:@"Sign in with Linkedin"
  42. action:^{ [weakSelf signInLinkedinHeadfulLite]; }],
  43. ]];
  44. }
  45. - (void)signInGoogleHeadfulLite {
  46. FIROAuthProvider *provider = self.googleOAuthProvider;
  47. provider.customParameters = @{
  48. @"prompt" : @"consent",
  49. };
  50. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  51. [self showSpinner:^{
  52. [[AppManager auth] signInWithProvider:provider
  53. UIDelegate:nil
  54. completion:^(FIRAuthDataResult *_Nullable authResult,
  55. NSError *_Nullable error) {
  56. [self hideSpinner:^{
  57. if (error) {
  58. [self logFailure:@"sign-in with provider (Google) failed" error:error];
  59. } else if (authResult.additionalUserInfo) {
  60. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  61. if (self.isNewUserToggleOn) {
  62. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  63. @"New user" : @"Existing user";
  64. [self showMessagePromptWithTitle:@"New or Existing"
  65. message:newUserString
  66. showCancelButton:NO
  67. completion:nil];
  68. }
  69. }
  70. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  71. }];
  72. }];
  73. }];
  74. }
  75. - (void)linkWithGoogleHeadfulLite {
  76. FIROAuthProvider *provider = self.googleOAuthProvider;
  77. provider.customParameters = @{
  78. @"prompt" : @"consent",
  79. };
  80. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  81. [self showSpinner:^{
  82. [[AppManager auth].currentUser linkWithProvider:provider
  83. UIDelegate:nil
  84. completion:^(FIRAuthDataResult *_Nullable authResult,
  85. NSError *_Nullable error) {
  86. [self hideSpinner:^{
  87. if (error) {
  88. [self logFailure:@"Reauthenticate with provider (Google) failed" error:error];
  89. } else if (authResult.additionalUserInfo) {
  90. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  91. if (self.isNewUserToggleOn) {
  92. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  93. @"New user" : @"Existing user";
  94. [self showMessagePromptWithTitle:@"New or Existing"
  95. message:newUserString
  96. showCancelButton:NO
  97. completion:nil];
  98. }
  99. }
  100. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
  101. }];
  102. }];
  103. }];
  104. }
  105. - (void)reauthenticateWithGoogleHeadfulLite {
  106. FIROAuthProvider *provider = self.googleOAuthProvider;
  107. provider.customParameters = @{
  108. @"prompt" : @"consent",
  109. };
  110. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  111. [self showSpinner:^{
  112. [[AppManager auth].currentUser reauthenticateWithProvider:provider
  113. UIDelegate:nil
  114. completion:^(FIRAuthDataResult *_Nullable authResult,
  115. NSError *_Nullable error) {
  116. [self hideSpinner:^{
  117. if (error) {
  118. [self logFailure:@"Link with provider (Google) failed" error:error];
  119. } else if (authResult.additionalUserInfo) {
  120. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  121. if (self.isNewUserToggleOn) {
  122. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  123. @"New user" : @"Existing user";
  124. [self showMessagePromptWithTitle:@"New or Existing"
  125. message:newUserString
  126. showCancelButton:NO
  127. completion:nil];
  128. }
  129. }
  130. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
  131. }];
  132. }];
  133. }];
  134. }
  135. - (void)signInTwitterHeadfulLite {
  136. FIROAuthProvider *provider = self.twitterOAuthProvider;
  137. [self showSpinner:^{
  138. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  139. NSError *_Nullable error) {
  140. if (error) {
  141. [self logFailure:@"sign-in with Twitter failed" error:error];
  142. return;
  143. }
  144. [[AppManager auth] signInWithCredential:credential
  145. completion:^(FIRAuthDataResult *_Nullable
  146. authResult,
  147. NSError *_Nullable error) {
  148. [self hideSpinner:^{
  149. if (error) {
  150. [self logFailure:@"sign-in with Twitter (headful-lite) failed" error:error];
  151. return;
  152. } else {
  153. [self logSuccess:@"sign-in with Twitter (headful-lite) succeeded."];
  154. }
  155. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  156. }];
  157. }];
  158. }];
  159. }];
  160. }
  161. - (void)signInWithGitHub {
  162. [self showTextInputPromptWithMessage:@"GitHub Access Token:"
  163. completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
  164. if (!userPressedOK || !accessToken.length) {
  165. return;
  166. }
  167. FIROAuthCredential *credential =
  168. [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
  169. if (credential) {
  170. [[AppManager auth] signInWithCredential:credential
  171. completion:^(FIRAuthDataResult *_Nullable result,
  172. NSError *_Nullable error) {
  173. if (error) {
  174. [self logFailure:@"sign-in with provider failed" error:error];
  175. } else {
  176. [self logSuccess:@"sign-in with provider succeeded."];
  177. }
  178. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  179. }];
  180. }
  181. }];
  182. }
  183. - (void)signInGitHubHeadfulLite {
  184. FIROAuthProvider *provider = self.gitHubOAuthProvider;
  185. [self showSpinner:^{
  186. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  187. NSError *_Nullable error) {
  188. if (error) {
  189. [self logFailure:@"sign-in with GitHub failed" error:error];
  190. return;
  191. }
  192. [[AppManager auth] signInWithCredential:credential
  193. completion:^(FIRAuthDataResult *_Nullable
  194. authResult,
  195. NSError *_Nullable error) {
  196. [self hideSpinner:^{
  197. if (error) {
  198. [self logFailure:@"sign-in with GitHub (headful-lite) failed" error:error];
  199. return;
  200. } else {
  201. [self logSuccess:@"sign-in with GitHub (headful-lite) succeeded."];
  202. }
  203. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  204. }];
  205. }];
  206. }];
  207. }];
  208. }
  209. - (void)signInLinkedinHeadfulLite {
  210. FIROAuthProvider *provider = self.linkedinOAuthProvider;
  211. [self showSpinner:^{
  212. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  213. NSError *_Nullable error) {
  214. if (error) {
  215. [self logFailure:@"sign-in with Linkedin failed" error:error];
  216. return;
  217. }
  218. [[AppManager auth] signInWithCredential:credential
  219. completion:^(FIRAuthDataResult *_Nullable
  220. authResult,
  221. NSError *_Nullable error) {
  222. [self hideSpinner:^{
  223. if (error) {
  224. [self logFailure:@"sign-in with Linkedin (headful-lite) failed" error:error];
  225. return;
  226. } else {
  227. [self logSuccess:@"sign-in with Linkedin (headful-lite) succeeded."];
  228. }
  229. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  230. }];
  231. }];
  232. }];
  233. }];
  234. }
  235. - (void)signInMicrosoftHeadfulLite {
  236. FIROAuthProvider *provider = self.microsoftOAuthProvider;
  237. provider.customParameters = @{
  238. @"prompt" : @"consent",
  239. @"login_hint" : @"tu8731@gmail.com",
  240. };
  241. provider.scopes = @[ @"user.readwrite,calendars.read" ];
  242. [self showSpinner:^{
  243. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  244. NSError *_Nullable error) {
  245. if (error) {
  246. [self logFailure:@"sign-in with Microsoft failed" error:error];
  247. return;
  248. }
  249. [[AppManager auth] signInWithCredential:credential
  250. completion:^(FIRAuthDataResult *_Nullable
  251. authResult,
  252. NSError *_Nullable error) {
  253. [self hideSpinner:^{
  254. if (error) {
  255. [self logFailure:@"sign-in with Microsoft failed" error:error];
  256. return;
  257. } else {
  258. [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
  259. }
  260. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  261. }];
  262. }];
  263. }];
  264. }];
  265. }
  266. - (void)signInYahooHeadfulLite {
  267. FIROAuthProvider *provider = self.yahooOAuthProvider;
  268. [self showSpinner:^{
  269. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  270. NSError *_Nullable error) {
  271. if (error) {
  272. [self logFailure:@"sign-in with Yahoo failed" error:error];
  273. return;
  274. }
  275. [[AppManager auth] signInWithCredential:credential
  276. completion:^(FIRAuthDataResult *_Nullable
  277. authResult,
  278. NSError *_Nullable error) {
  279. [self hideSpinner:^{
  280. if (error) {
  281. [self logFailure:@"sign-in with Yahoo (headful-lite) failed" error:error];
  282. return;
  283. } else {
  284. [self logSuccess:@"sign-in with Yahoo (headful-lite) succeeded."];
  285. }
  286. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  287. }];
  288. }];
  289. }];
  290. }];
  291. }
  292. @end
  293. NS_ASSUME_NONNULL_END