FIRUser.m 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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. */
  16. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
  17. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIREmailAuthProvider.h"
  18. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRFederatedAuthProvider.h"
  19. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  20. #import "FirebaseAuth/Sources/Auth/FIRAuthDataResult_Internal.h"
  21. #import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  22. #import "FirebaseAuth/Sources/Auth/FIRAuthOperationType.h"
  23. #import "FirebaseAuth/Sources/Auth/FIRAuthSerialTaskQueue.h"
  24. #import "FirebaseAuth/Sources/Auth/FIRAuthTokenResult_Internal.h"
  25. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  26. #import "FirebaseAuth/Sources/AuthProvider/Email/FIREmailPasswordAuthCredential.h"
  27. #import "FirebaseAuth/Sources/AuthProvider/FIRAuthCredential_Internal.h"
  28. #import "FirebaseAuth/Sources/AuthProvider/GameCenter/FIRGameCenterAuthCredential.h"
  29. #import "FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential_Internal.h"
  30. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  31. #import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
  32. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountRequest.h"
  33. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountResponse.h"
  34. #import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInRequest.h"
  35. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetAccountInfoRequest.h"
  36. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetAccountInfoResponse.h"
  37. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeRequest.h"
  38. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
  39. #import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoRequest.h"
  40. #import "FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoResponse.h"
  41. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterRequest.h"
  42. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterResponse.h"
  43. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
  44. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionResponse.h"
  45. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenRequest.h"
  46. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenResponse.h"
  47. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPasswordRequest.h"
  48. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPasswordResponse.h"
  49. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPhoneNumberRequest.h"
  50. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyPhoneNumberResponse.h"
  51. #import "FirebaseAuth/Sources/MultiFactor/FIRMultiFactor+Internal.h"
  52. #import "FirebaseAuth/Sources/SystemService/FIRSecureTokenService.h"
  53. #import "FirebaseAuth/Sources/User/FIRAdditionalUserInfo_Internal.h"
  54. #import "FirebaseAuth/Sources/User/FIRUserInfoImpl.h"
  55. #import "FirebaseAuth/Sources/User/FIRUserMetadata_Internal.h"
  56. #import "FirebaseAuth/Sources/User/FIRUser_Internal.h"
  57. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  58. #import "FirebaseAuth/Sources/Utilities/FIRAuthWebUtils.h"
  59. #if TARGET_OS_IOS
  60. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthProvider.h"
  61. #import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
  62. #endif
  63. NS_ASSUME_NONNULL_BEGIN
  64. /** @var kUserIDCodingKey
  65. @brief The key used to encode the user ID for NSSecureCoding.
  66. */
  67. static NSString *const kUserIDCodingKey = @"userID";
  68. /** @var kHasEmailPasswordCredentialCodingKey
  69. @brief The key used to encode the hasEmailPasswordCredential property for NSSecureCoding.
  70. */
  71. static NSString *const kHasEmailPasswordCredentialCodingKey = @"hasEmailPassword";
  72. /** @var kAnonymousCodingKey
  73. @brief The key used to encode the anonymous property for NSSecureCoding.
  74. */
  75. static NSString *const kAnonymousCodingKey = @"anonymous";
  76. /** @var kEmailCodingKey
  77. @brief The key used to encode the email property for NSSecureCoding.
  78. */
  79. static NSString *const kEmailCodingKey = @"email";
  80. /** @var kPhoneNumberCodingKey
  81. @brief The key used to encode the phoneNumber property for NSSecureCoding.
  82. */
  83. static NSString *const kPhoneNumberCodingKey = @"phoneNumber";
  84. /** @var kEmailVerifiedCodingKey
  85. @brief The key used to encode the isEmailVerified property for NSSecureCoding.
  86. */
  87. static NSString *const kEmailVerifiedCodingKey = @"emailVerified";
  88. /** @var kDisplayNameCodingKey
  89. @brief The key used to encode the displayName property for NSSecureCoding.
  90. */
  91. static NSString *const kDisplayNameCodingKey = @"displayName";
  92. /** @var kPhotoURLCodingKey
  93. @brief The key used to encode the photoURL property for NSSecureCoding.
  94. */
  95. static NSString *const kPhotoURLCodingKey = @"photoURL";
  96. /** @var kProviderDataKey
  97. @brief The key used to encode the providerData instance variable for NSSecureCoding.
  98. */
  99. static NSString *const kProviderDataKey = @"providerData";
  100. /** @var kAPIKeyCodingKey
  101. @brief The key used to encode the APIKey instance variable for NSSecureCoding.
  102. */
  103. static NSString *const kAPIKeyCodingKey = @"APIKey";
  104. /** @var kTokenServiceCodingKey
  105. @brief The key used to encode the tokenService instance variable for NSSecureCoding.
  106. */
  107. static NSString *const kTokenServiceCodingKey = @"tokenService";
  108. /** @var kMetadataCodingKey
  109. @brief The key used to encode the metadata instance variable for NSSecureCoding.
  110. */
  111. static NSString *const kMetadataCodingKey = @"metadata";
  112. static NSString *const kMultiFactorCodingKey = @"multiFactor";
  113. /** @var kTenantIDKey
  114. @brief The key used to encode the tenantID instance variable for NSSecureCoding.
  115. */
  116. static NSString *const kTenantIDCodingKey = @"tenantID";
  117. /** @var kMissingUsersErrorMessage
  118. @brief The error message when there is no users array in the getAccountInfo response.
  119. */
  120. static NSString *const kMissingUsersErrorMessage = @"users";
  121. /** @typedef CallbackWithError
  122. @brief The type for a callback block that only takes an error parameter.
  123. */
  124. typedef void (^CallbackWithError)(NSError *_Nullable);
  125. /** @typedef CallbackWithUserAndError
  126. @brief The type for a callback block that takes a user parameter and an error parameter.
  127. */
  128. typedef void (^CallbackWithUserAndError)(FIRUser *_Nullable, NSError *_Nullable);
  129. /** @typedef CallbackWithUserAndError
  130. @brief The type for a callback block that takes a user parameter and an error parameter.
  131. */
  132. typedef void (^CallbackWithAuthDataResultAndError)(FIRAuthDataResult *_Nullable,
  133. NSError *_Nullable);
  134. /** @var kMissingPasswordReason
  135. @brief The reason why the @c FIRAuthErrorCodeWeakPassword error is thrown.
  136. @remarks This error message will be localized in the future.
  137. */
  138. static NSString *const kMissingPasswordReason = @"Missing Password";
  139. /** @fn callInMainThreadWithError
  140. @brief Calls a callback in main thread with error.
  141. @param callback The callback to be called in main thread.
  142. @param error The error to pass to callback.
  143. */
  144. static void callInMainThreadWithError(_Nullable CallbackWithError callback,
  145. NSError *_Nullable error) {
  146. if (callback) {
  147. dispatch_async(dispatch_get_main_queue(), ^{
  148. callback(error);
  149. });
  150. }
  151. }
  152. /** @fn callInMainThreadWithUserAndError
  153. @brief Calls a callback in main thread with user and error.
  154. @param callback The callback to be called in main thread.
  155. @param user The user to pass to callback if there is no error.
  156. @param error The error to pass to callback.
  157. */
  158. static void callInMainThreadWithUserAndError(_Nullable CallbackWithUserAndError callback,
  159. FIRUser *_Nonnull user,
  160. NSError *_Nullable error) {
  161. if (callback) {
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. callback(error ? nil : user, error);
  164. });
  165. }
  166. }
  167. /** @fn callInMainThreadWithUserAndError
  168. @brief Calls a callback in main thread with user and error.
  169. @param callback The callback to be called in main thread.
  170. @param result The result to pass to callback if there is no error.
  171. @param error The error to pass to callback.
  172. */
  173. static void callInMainThreadWithAuthDataResultAndError(
  174. _Nullable CallbackWithAuthDataResultAndError callback,
  175. FIRAuthDataResult *_Nullable result,
  176. NSError *_Nullable error) {
  177. if (callback) {
  178. dispatch_async(dispatch_get_main_queue(), ^{
  179. callback(result, error);
  180. });
  181. }
  182. }
  183. @interface FIRUserProfileChangeRequest ()
  184. /** @fn initWithUser:
  185. @brief Designated initializer.
  186. @param user The user for which we are updating profile information.
  187. */
  188. - (nullable instancetype)initWithUser:(FIRUser *)user NS_DESIGNATED_INITIALIZER;
  189. @end
  190. @interface FIRUser ()
  191. /** @property anonymous
  192. @brief Whether the current user is anonymous.
  193. */
  194. @property(nonatomic, readwrite) BOOL anonymous;
  195. /** @property tenantID
  196. @brief The tenant ID of the current user. nil if none is available.
  197. */
  198. @property(nonatomic, readwrite, nullable) NSString *tenantID;
  199. @end
  200. @implementation FIRUser {
  201. /** @var _hasEmailPasswordCredential
  202. @brief Whether or not the user can be authenticated by using Firebase email and password.
  203. */
  204. BOOL _hasEmailPasswordCredential;
  205. /** @var _providerData
  206. @brief Provider specific user data.
  207. */
  208. NSDictionary<NSString *, FIRUserInfoImpl *> *_providerData;
  209. /** @var _taskQueue
  210. @brief Used to serialize the update profile calls.
  211. */
  212. FIRAuthSerialTaskQueue *_taskQueue;
  213. /** @var _tokenService
  214. @brief A secure token service associated with this user. For performing token exchanges and
  215. refreshing access tokens.
  216. */
  217. FIRSecureTokenService *_tokenService;
  218. }
  219. #pragma mark - Properties
  220. // Explicitly @synthesize because these properties are defined in FIRUserInfo protocol.
  221. @synthesize uid = _userID;
  222. @synthesize displayName = _displayName;
  223. @synthesize photoURL = _photoURL;
  224. @synthesize email = _email;
  225. @synthesize phoneNumber = _phoneNumber;
  226. #pragma mark -
  227. + (void)retrieveUserWithAuth:(FIRAuth *)auth
  228. accessToken:(nullable NSString *)accessToken
  229. accessTokenExpirationDate:(nullable NSDate *)accessTokenExpirationDate
  230. refreshToken:(nullable NSString *)refreshToken
  231. anonymous:(BOOL)anonymous
  232. callback:(FIRRetrieveUserCallback)callback {
  233. FIRSecureTokenService *tokenService =
  234. [[FIRSecureTokenService alloc] initWithRequestConfiguration:auth.requestConfiguration
  235. accessToken:accessToken
  236. accessTokenExpirationDate:accessTokenExpirationDate
  237. refreshToken:refreshToken];
  238. FIRUser *user = [[self alloc] initWithTokenService:tokenService];
  239. user.auth = auth;
  240. user.tenantID = auth.tenantID;
  241. user.requestConfiguration = auth.requestConfiguration;
  242. [user internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  243. if (error) {
  244. callback(nil, error);
  245. return;
  246. }
  247. FIRGetAccountInfoRequest *getAccountInfoRequest =
  248. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
  249. requestConfiguration:auth.requestConfiguration];
  250. [FIRAuthBackend
  251. getAccountInfo:getAccountInfoRequest
  252. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  253. if (error) {
  254. // No need to sign out user here for errors because the user hasn't been signed in
  255. // yet.
  256. callback(nil, error);
  257. return;
  258. }
  259. user.anonymous = anonymous;
  260. [user updateWithGetAccountInfoResponse:response];
  261. callback(user, nil);
  262. }];
  263. }];
  264. }
  265. - (instancetype)initWithTokenService:(FIRSecureTokenService *)tokenService {
  266. self = [super init];
  267. if (self) {
  268. _providerData = @{};
  269. _taskQueue = [[FIRAuthSerialTaskQueue alloc] init];
  270. _tokenService = tokenService;
  271. }
  272. return self;
  273. }
  274. #pragma mark - NSSecureCoding
  275. + (BOOL)supportsSecureCoding {
  276. return YES;
  277. }
  278. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  279. NSString *userID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kUserIDCodingKey];
  280. BOOL hasAnonymousKey = [aDecoder containsValueForKey:kAnonymousCodingKey];
  281. BOOL anonymous = [aDecoder decodeBoolForKey:kAnonymousCodingKey];
  282. BOOL hasEmailPasswordCredential =
  283. [aDecoder decodeBoolForKey:kHasEmailPasswordCredentialCodingKey];
  284. NSString *displayName = [aDecoder decodeObjectOfClass:[NSString class]
  285. forKey:kDisplayNameCodingKey];
  286. NSURL *photoURL = [aDecoder decodeObjectOfClass:[NSURL class] forKey:kPhotoURLCodingKey];
  287. NSString *email = [aDecoder decodeObjectOfClass:[NSString class] forKey:kEmailCodingKey];
  288. NSString *phoneNumber = [aDecoder decodeObjectOfClass:[NSString class]
  289. forKey:kPhoneNumberCodingKey];
  290. BOOL emailVerified = [aDecoder decodeBoolForKey:kEmailVerifiedCodingKey];
  291. NSSet *providerDataClasses =
  292. [NSSet setWithArray:@[ [NSDictionary class], [NSString class], [FIRUserInfoImpl class] ]];
  293. NSDictionary<NSString *, FIRUserInfoImpl *> *providerData =
  294. [aDecoder decodeObjectOfClasses:providerDataClasses forKey:kProviderDataKey];
  295. FIRSecureTokenService *tokenService = [aDecoder decodeObjectOfClass:[FIRSecureTokenService class]
  296. forKey:kTokenServiceCodingKey];
  297. FIRUserMetadata *metadata = [aDecoder decodeObjectOfClass:[FIRUserMetadata class]
  298. forKey:kMetadataCodingKey];
  299. NSString *tenantID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kTenantIDCodingKey];
  300. NSString *APIKey = [aDecoder decodeObjectOfClass:[NSString class] forKey:kAPIKeyCodingKey];
  301. #if TARGET_OS_IOS
  302. FIRMultiFactor *multiFactor = [aDecoder decodeObjectOfClass:[FIRMultiFactor class]
  303. forKey:kMultiFactorCodingKey];
  304. #endif
  305. if (!userID || !tokenService) {
  306. return nil;
  307. }
  308. self = [self initWithTokenService:tokenService];
  309. if (self) {
  310. _userID = userID;
  311. // Previous version of this code didn't save 'anonymous' bit directly but deduced it from
  312. // 'hasEmailPasswordCredential' and 'providerData' instead, so here backward compatibility is
  313. // provided to read old format data.
  314. _anonymous = hasAnonymousKey ? anonymous : (!hasEmailPasswordCredential && !providerData.count);
  315. _hasEmailPasswordCredential = hasEmailPasswordCredential;
  316. _email = email;
  317. _emailVerified = emailVerified;
  318. _displayName = displayName;
  319. _photoURL = photoURL;
  320. _providerData = providerData;
  321. _phoneNumber = phoneNumber;
  322. _metadata = metadata ?: [[FIRUserMetadata alloc] initWithCreationDate:nil lastSignInDate:nil];
  323. _tenantID = tenantID;
  324. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey];
  325. #if TARGET_OS_IOS
  326. _multiFactor = multiFactor ?: [[FIRMultiFactor alloc] init];
  327. #endif
  328. }
  329. return self;
  330. }
  331. - (void)encodeWithCoder:(NSCoder *)aCoder {
  332. [aCoder encodeObject:_userID forKey:kUserIDCodingKey];
  333. [aCoder encodeBool:self.anonymous forKey:kAnonymousCodingKey];
  334. [aCoder encodeBool:_hasEmailPasswordCredential forKey:kHasEmailPasswordCredentialCodingKey];
  335. [aCoder encodeObject:_providerData forKey:kProviderDataKey];
  336. [aCoder encodeObject:_email forKey:kEmailCodingKey];
  337. [aCoder encodeObject:_phoneNumber forKey:kPhoneNumberCodingKey];
  338. [aCoder encodeBool:_emailVerified forKey:kEmailVerifiedCodingKey];
  339. [aCoder encodeObject:_photoURL forKey:kPhotoURLCodingKey];
  340. [aCoder encodeObject:_displayName forKey:kDisplayNameCodingKey];
  341. [aCoder encodeObject:_metadata forKey:kMetadataCodingKey];
  342. [aCoder encodeObject:_tenantID forKey:kTenantIDCodingKey];
  343. [aCoder encodeObject:_auth.requestConfiguration.APIKey forKey:kAPIKeyCodingKey];
  344. [aCoder encodeObject:_tokenService forKey:kTokenServiceCodingKey];
  345. #if TARGET_OS_IOS
  346. [aCoder encodeObject:_multiFactor forKey:kMultiFactorCodingKey];
  347. #endif
  348. }
  349. #pragma mark -
  350. - (void)setAuth:(nullable FIRAuth *)auth {
  351. _auth = auth;
  352. _tokenService.requestConfiguration = auth.requestConfiguration;
  353. }
  354. - (NSString *)providerID {
  355. return @"Firebase";
  356. }
  357. - (NSArray<id<FIRUserInfo>> *)providerData {
  358. return _providerData.allValues;
  359. }
  360. /** @fn getAccountInfoRefreshingCache:
  361. @brief Gets the users's account data from the server, updating our local values.
  362. @param callback Invoked when the request to getAccountInfo has completed, or when an error has
  363. been detected. Invoked asynchronously on the auth global work queue in the future.
  364. */
  365. - (void)getAccountInfoRefreshingCache:(void (^)(FIRGetAccountInfoResponseUser *_Nullable user,
  366. NSError *_Nullable error))callback {
  367. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  368. if (error) {
  369. callback(nil, error);
  370. return;
  371. }
  372. FIRGetAccountInfoRequest *getAccountInfoRequest =
  373. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
  374. requestConfiguration:self->_auth.requestConfiguration];
  375. [FIRAuthBackend
  376. getAccountInfo:getAccountInfoRequest
  377. callback:^(FIRGetAccountInfoResponse *_Nullable response, NSError *_Nullable error) {
  378. if (error) {
  379. [self signOutIfTokenIsInvalidWithError:error];
  380. callback(nil, error);
  381. return;
  382. }
  383. [self updateWithGetAccountInfoResponse:response];
  384. if (![self updateKeychain:&error]) {
  385. callback(nil, error);
  386. return;
  387. }
  388. callback(response.users.firstObject, nil);
  389. }];
  390. }];
  391. }
  392. - (void)updateWithGetAccountInfoResponse:(FIRGetAccountInfoResponse *)response {
  393. FIRGetAccountInfoResponseUser *user = response.users.firstObject;
  394. _userID = user.localID;
  395. _email = user.email;
  396. _emailVerified = user.emailVerified;
  397. _displayName = user.displayName;
  398. _photoURL = user.photoURL;
  399. _phoneNumber = user.phoneNumber;
  400. _hasEmailPasswordCredential = user.passwordHash.length > 0;
  401. _metadata = [[FIRUserMetadata alloc] initWithCreationDate:user.creationDate
  402. lastSignInDate:user.lastLoginDate];
  403. NSMutableDictionary<NSString *, FIRUserInfoImpl *> *providerData =
  404. [NSMutableDictionary dictionary];
  405. for (FIRGetAccountInfoResponseProviderUserInfo *providerUserInfo in user.providerUserInfo) {
  406. FIRUserInfoImpl *userInfo =
  407. [FIRUserInfoImpl userInfoWithGetAccountInfoResponseProviderUserInfo:providerUserInfo];
  408. if (userInfo) {
  409. providerData[providerUserInfo.providerID] = userInfo;
  410. }
  411. }
  412. _providerData = [providerData copy];
  413. #if TARGET_OS_IOS
  414. _multiFactor = [[FIRMultiFactor alloc] initWithMFAEnrollments:user.MFAEnrollments];
  415. _multiFactor.user = self;
  416. #endif
  417. }
  418. /** @fn executeUserUpdateWithChanges:callback:
  419. @brief Performs a setAccountInfo request by mutating the results of a getAccountInfo response,
  420. atomically in regards to other calls to this method.
  421. @param changeBlock A block responsible for mutating a template @c FIRSetAccountInfoRequest
  422. @param callback A block to invoke when the change is complete. Invoked asynchronously on the
  423. auth global work queue in the future.
  424. */
  425. - (void)executeUserUpdateWithChanges:(void (^)(FIRGetAccountInfoResponseUser *,
  426. FIRSetAccountInfoRequest *))changeBlock
  427. callback:(nonnull FIRUserProfileChangeCallback)callback {
  428. [_taskQueue enqueueTask:^(FIRAuthSerialTaskCompletionBlock _Nonnull complete) {
  429. [self getAccountInfoRefreshingCache:^(FIRGetAccountInfoResponseUser *_Nullable user,
  430. NSError *_Nullable error) {
  431. if (error) {
  432. complete();
  433. callback(error);
  434. return;
  435. }
  436. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  437. NSError *_Nullable error) {
  438. if (error) {
  439. complete();
  440. callback(error);
  441. return;
  442. }
  443. FIRAuthRequestConfiguration *configuration = self->_auth.requestConfiguration;
  444. // Mutate setAccountInfoRequest in block:
  445. FIRSetAccountInfoRequest *setAccountInfoRequest =
  446. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:configuration];
  447. setAccountInfoRequest.accessToken = accessToken;
  448. changeBlock(user, setAccountInfoRequest);
  449. // Execute request:
  450. [FIRAuthBackend
  451. setAccountInfo:setAccountInfoRequest
  452. callback:^(FIRSetAccountInfoResponse *_Nullable response,
  453. NSError *_Nullable error) {
  454. if (error) {
  455. [self signOutIfTokenIsInvalidWithError:error];
  456. complete();
  457. callback(error);
  458. return;
  459. }
  460. if (response.IDToken && response.refreshToken) {
  461. FIRSecureTokenService *tokenService = [[FIRSecureTokenService alloc]
  462. initWithRequestConfiguration:configuration
  463. accessToken:response.IDToken
  464. accessTokenExpirationDate:response.approximateExpirationDate
  465. refreshToken:response.refreshToken];
  466. [self setTokenService:tokenService
  467. callback:^(NSError *_Nullable error) {
  468. complete();
  469. callback(error);
  470. }];
  471. return;
  472. }
  473. complete();
  474. callback(nil);
  475. }];
  476. }];
  477. }];
  478. }];
  479. }
  480. /** @fn updateKeychain:
  481. @brief Updates the keychain for user token or info changes.
  482. @param error The error if NO is returned.
  483. @return Whether the operation is successful.
  484. */
  485. - (BOOL)updateKeychain:(NSError *_Nullable *_Nullable)error {
  486. return [_auth updateKeychainWithUser:self error:error];
  487. }
  488. /** @fn setTokenService:callback:
  489. @brief Sets a new token service for the @c FIRUser instance.
  490. @param tokenService The new token service object.
  491. @param callback The block to be called in the global auth working queue once finished.
  492. @remarks The method makes sure the token service has access and refresh token and the new tokens
  493. are saved in the keychain before calling back.
  494. */
  495. - (void)setTokenService:(FIRSecureTokenService *)tokenService
  496. callback:(nonnull CallbackWithError)callback {
  497. [tokenService fetchAccessTokenForcingRefresh:NO
  498. callback:^(NSString *_Nullable token,
  499. NSError *_Nullable error, BOOL tokenUpdated) {
  500. if (error) {
  501. callback(error);
  502. return;
  503. }
  504. self->_tokenService = tokenService;
  505. if (![self updateKeychain:&error]) {
  506. callback(error);
  507. return;
  508. }
  509. callback(nil);
  510. }];
  511. }
  512. #pragma mark -
  513. /** @fn updateEmail:password:callback:
  514. @brief Updates email address and/or password for the current user.
  515. @remarks May fail if there is already an email/password-based account for the same email
  516. address.
  517. @param email The email address for the user, if to be updated.
  518. @param password The new password for the user, if to be updated.
  519. @param callback The block called when the user profile change has finished. Invoked
  520. asynchronously on the auth global work queue in the future.
  521. @remarks May fail with a @c FIRAuthErrorCodeRequiresRecentLogin error code.
  522. Call @c reauthentateWithCredential:completion: beforehand to avoid this error case.
  523. */
  524. - (void)updateEmail:(nullable NSString *)email
  525. password:(nullable NSString *)password
  526. callback:(nonnull FIRUserProfileChangeCallback)callback {
  527. if (password && ![password length]) {
  528. callback([FIRAuthErrorUtils weakPasswordErrorWithServerResponseReason:kMissingPasswordReason]);
  529. return;
  530. }
  531. BOOL hadEmailPasswordCredential = _hasEmailPasswordCredential;
  532. [self
  533. executeUserUpdateWithChanges:^(FIRGetAccountInfoResponseUser *user,
  534. FIRSetAccountInfoRequest *request) {
  535. if (email) {
  536. request.email = email;
  537. }
  538. if (password) {
  539. request.password = password;
  540. }
  541. }
  542. callback:^(NSError *error) {
  543. if (error) {
  544. callback(error);
  545. return;
  546. }
  547. if (email) {
  548. self->_email = [email copy];
  549. }
  550. if (self->_email) {
  551. if (!hadEmailPasswordCredential) {
  552. // The list of providers need to be updated for the newly added email-password provider.
  553. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  554. NSError *_Nullable error) {
  555. if (error) {
  556. callback(error);
  557. return;
  558. }
  559. FIRAuthRequestConfiguration *requestConfiguration = self->_auth.requestConfiguration;
  560. FIRGetAccountInfoRequest *getAccountInfoRequest =
  561. [[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
  562. requestConfiguration:requestConfiguration];
  563. [FIRAuthBackend
  564. getAccountInfo:getAccountInfoRequest
  565. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  566. NSError *_Nullable error) {
  567. if (error) {
  568. [self signOutIfTokenIsInvalidWithError:error];
  569. callback(error);
  570. return;
  571. }
  572. for (FIRGetAccountInfoResponseUser *userAccountInfo in response.users) {
  573. // Set the account to non-anonymous if there are any providers, even if
  574. // they're not email/password ones.
  575. if (userAccountInfo.providerUserInfo.count > 0) {
  576. self.anonymous = NO;
  577. }
  578. for (FIRGetAccountInfoResponseProviderUserInfo
  579. *providerUserInfo in userAccountInfo.providerUserInfo) {
  580. if ([providerUserInfo.providerID
  581. isEqualToString:FIREmailAuthProviderID]) {
  582. self->_hasEmailPasswordCredential = YES;
  583. break;
  584. }
  585. }
  586. }
  587. [self updateWithGetAccountInfoResponse:response];
  588. if (![self updateKeychain:&error]) {
  589. callback(error);
  590. return;
  591. }
  592. callback(nil);
  593. }];
  594. }];
  595. return;
  596. }
  597. }
  598. if (![self updateKeychain:&error]) {
  599. callback(error);
  600. return;
  601. }
  602. callback(nil);
  603. }];
  604. }
  605. - (void)updateEmail:(NSString *)email completion:(nullable FIRUserProfileChangeCallback)completion {
  606. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  607. [self updateEmail:email
  608. password:nil
  609. callback:^(NSError *_Nullable error) {
  610. callInMainThreadWithError(completion, error);
  611. }];
  612. });
  613. }
  614. - (void)updatePassword:(NSString *)password
  615. completion:(nullable FIRUserProfileChangeCallback)completion {
  616. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  617. [self updateEmail:nil
  618. password:password
  619. callback:^(NSError *_Nullable error) {
  620. callInMainThreadWithError(completion, error);
  621. }];
  622. });
  623. }
  624. #if TARGET_OS_IOS
  625. /** @fn internalUpdateOrLinkPhoneNumberCredential:completion:
  626. @brief Updates the phone number for the user. On success, the cached user profile data is
  627. updated.
  628. @param phoneAuthCredential The new phone number credential corresponding to the phone number
  629. to be added to the Firebase account, if a phone number is already linked to the account this
  630. new phone number will replace it.
  631. @param isLinkOperation Boolean value indicating whether or not this is a link operation.
  632. @param completion Optionally; the block invoked when the user profile change has finished.
  633. Invoked asynchronously on the global work queue in the future.
  634. */
  635. - (void)internalUpdateOrLinkPhoneNumberCredential:(FIRPhoneAuthCredential *)phoneAuthCredential
  636. isLinkOperation:(BOOL)isLinkOperation
  637. completion:(FIRUserProfileChangeCallback)completion {
  638. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  639. if (error) {
  640. completion(error);
  641. return;
  642. }
  643. FIRAuthOperationType operation =
  644. isLinkOperation ? FIRAuthOperationTypeLink : FIRAuthOperationTypeUpdate;
  645. FIRVerifyPhoneNumberRequest *request = [[FIRVerifyPhoneNumberRequest alloc]
  646. initWithVerificationID:phoneAuthCredential.verificationID
  647. verificationCode:phoneAuthCredential.verificationCode
  648. operation:operation
  649. requestConfiguration:self->_auth.requestConfiguration];
  650. request.accessToken = accessToken;
  651. [FIRAuthBackend verifyPhoneNumber:request
  652. callback:^(FIRVerifyPhoneNumberResponse *_Nullable response,
  653. NSError *_Nullable error) {
  654. if (error) {
  655. [self signOutIfTokenIsInvalidWithError:error];
  656. completion(error);
  657. return;
  658. }
  659. // Get account info to update cached user info.
  660. [self getAccountInfoRefreshingCache:^(
  661. FIRGetAccountInfoResponseUser *_Nullable user,
  662. NSError *_Nullable error) {
  663. if (error) {
  664. [self signOutIfTokenIsInvalidWithError:error];
  665. completion(error);
  666. return;
  667. }
  668. self.anonymous = NO;
  669. if (![self updateKeychain:&error]) {
  670. completion(error);
  671. return;
  672. }
  673. completion(nil);
  674. }];
  675. }];
  676. }];
  677. }
  678. - (void)updatePhoneNumberCredential:(FIRPhoneAuthCredential *)phoneAuthCredential
  679. completion:(nullable FIRUserProfileChangeCallback)completion {
  680. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  681. [self internalUpdateOrLinkPhoneNumberCredential:phoneAuthCredential
  682. isLinkOperation:NO
  683. completion:^(NSError *_Nullable error) {
  684. callInMainThreadWithError(completion, error);
  685. }];
  686. });
  687. }
  688. #endif
  689. - (FIRUserProfileChangeRequest *)profileChangeRequest {
  690. __block FIRUserProfileChangeRequest *result;
  691. dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
  692. result = [[FIRUserProfileChangeRequest alloc] initWithUser:self];
  693. });
  694. return result;
  695. }
  696. - (void)setDisplayName:(NSString *)displayName {
  697. _displayName = [displayName copy];
  698. }
  699. - (void)setPhotoURL:(NSURL *)photoURL {
  700. _photoURL = [photoURL copy];
  701. }
  702. - (NSString *)rawAccessToken {
  703. return _tokenService.rawAccessToken;
  704. }
  705. - (NSDate *)accessTokenExpirationDate {
  706. return _tokenService.accessTokenExpirationDate;
  707. }
  708. #pragma mark -
  709. - (void)reloadWithCompletion:(nullable FIRUserProfileChangeCallback)completion {
  710. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  711. [self getAccountInfoRefreshingCache:^(FIRGetAccountInfoResponseUser *_Nullable user,
  712. NSError *_Nullable error) {
  713. callInMainThreadWithError(completion, error);
  714. }];
  715. });
  716. }
  717. #pragma mark -
  718. - (void)reauthenticateWithCredential:(FIRAuthCredential *)credential
  719. completion:(nullable FIRAuthDataResultCallback)completion {
  720. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  721. [self->_auth
  722. internalSignInAndRetrieveDataWithCredential:credential
  723. isReauthentication:YES
  724. callback:^(FIRAuthDataResult *_Nullable authResult,
  725. NSError *_Nullable error) {
  726. if (error) {
  727. // If "user not found" error returned by backend,
  728. // translate to user mismatch error which is more
  729. // accurate.
  730. if (error.code == FIRAuthErrorCodeUserNotFound) {
  731. error = [FIRAuthErrorUtils userMismatchError];
  732. }
  733. callInMainThreadWithAuthDataResultAndError(
  734. completion, authResult, error);
  735. return;
  736. }
  737. if (![authResult.user.uid
  738. isEqual:[self->_auth getUserID]]) {
  739. callInMainThreadWithAuthDataResultAndError(
  740. completion, authResult,
  741. [FIRAuthErrorUtils userMismatchError]);
  742. return;
  743. }
  744. // Successful reauthenticate
  745. [self
  746. setTokenService:authResult.user->_tokenService
  747. callback:^(NSError *_Nullable error) {
  748. callInMainThreadWithAuthDataResultAndError(
  749. completion, authResult, error);
  750. }];
  751. }];
  752. });
  753. }
  754. - (void)reauthenticateWithProvider:(id<FIRFederatedAuthProvider>)provider
  755. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  756. completion:(nullable FIRAuthDataResultCallback)completion {
  757. #if TARGET_OS_IOS
  758. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  759. [provider getCredentialWithUIDelegate:UIDelegate
  760. completion:^(FIRAuthCredential *_Nullable credential,
  761. NSError *_Nullable error) {
  762. [self reauthenticateWithCredential:credential
  763. completion:completion];
  764. }];
  765. });
  766. #endif // TARGET_OS_IOS
  767. }
  768. - (nullable NSString *)refreshToken {
  769. __block NSString *result;
  770. dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
  771. result = self->_tokenService.refreshToken;
  772. });
  773. return result;
  774. }
  775. - (void)getIDTokenWithCompletion:(nullable FIRAuthTokenCallback)completion {
  776. // |getIDTokenForcingRefresh:completion:| is also a public API so there is no need to dispatch to
  777. // global work queue here.
  778. [self getIDTokenForcingRefresh:NO completion:completion];
  779. }
  780. - (void)getIDTokenForcingRefresh:(BOOL)forceRefresh
  781. completion:(nullable FIRAuthTokenCallback)completion {
  782. [self getIDTokenResultForcingRefresh:forceRefresh
  783. completion:^(FIRAuthTokenResult *_Nullable tokenResult,
  784. NSError *_Nullable error) {
  785. if (completion) {
  786. dispatch_async(dispatch_get_main_queue(), ^{
  787. completion(tokenResult.token, error);
  788. });
  789. }
  790. }];
  791. }
  792. - (void)getIDTokenResultWithCompletion:(nullable FIRAuthTokenResultCallback)completion {
  793. [self getIDTokenResultForcingRefresh:NO
  794. completion:^(FIRAuthTokenResult *_Nullable tokenResult,
  795. NSError *_Nullable error) {
  796. if (completion) {
  797. dispatch_async(dispatch_get_main_queue(), ^{
  798. completion(tokenResult, error);
  799. });
  800. }
  801. }];
  802. }
  803. - (void)getIDTokenResultForcingRefresh:(BOOL)forceRefresh
  804. completion:(nullable FIRAuthTokenResultCallback)completion {
  805. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  806. [self internalGetTokenForcingRefresh:forceRefresh
  807. callback:^(NSString *_Nullable token, NSError *_Nullable error) {
  808. FIRAuthTokenResult *tokenResult;
  809. if (token) {
  810. tokenResult = [FIRAuthTokenResult tokenResultWithToken:token];
  811. }
  812. if (completion) {
  813. dispatch_async(dispatch_get_main_queue(), ^{
  814. completion(tokenResult, error);
  815. });
  816. }
  817. }];
  818. });
  819. }
  820. /** @fn parseIDToken:error:
  821. @brief Parses the provided IDToken and returns an instance of FIRAuthTokenResult containing
  822. claims obtained from the IDToken.
  823. @param token The raw text of the Firebase IDToken encoded in base64.
  824. @param error An out parameter which would contain any error that occurs during parsing.
  825. @return An instance of FIRAuthTokenResult containing claims obtained from the IDToken.
  826. @remarks IDToken returned from the backend in some cases is of a length that is not a multiple
  827. of 4. In these cases this function pads the token with as many "=" characters as needed and
  828. then attempts to parse the token. If the token cannot be parsed an error is returned via the
  829. "error" out parameter.
  830. */
  831. - (nullable FIRAuthTokenResult *)parseIDToken:(NSString *)token error:(NSError **)error {
  832. // Though this is an internal method, errors returned here are surfaced in user-visible
  833. // callbacks.
  834. if (error) {
  835. *error = nil;
  836. }
  837. NSArray *tokenStringArray = [token componentsSeparatedByString:@"."];
  838. // The JWT should have three parts, though we only use the second in this method.
  839. if (tokenStringArray.count != 3) {
  840. if (error) {
  841. *error = [FIRAuthErrorUtils malformedJWTErrorWithToken:token underlyingError:nil];
  842. }
  843. return nil;
  844. }
  845. // The token payload is always the second index of the array.
  846. NSString *IDToken = tokenStringArray[1];
  847. // Convert the base64URL encoded string to a base64 encoded string.
  848. // Replace "_" with "/"
  849. NSMutableString *tokenPayload = [[IDToken stringByReplacingOccurrencesOfString:@"_"
  850. withString:@"/"] mutableCopy];
  851. // Replace "-" with "+"
  852. [tokenPayload replaceOccurrencesOfString:@"-"
  853. withString:@"+"
  854. options:kNilOptions
  855. range:NSMakeRange(0, tokenPayload.length)];
  856. // Pad the token payload with "=" signs if the payload's length is not a multiple of 4.
  857. while ((tokenPayload.length % 4) != 0) {
  858. [tokenPayload appendFormat:@"="];
  859. }
  860. NSData *decodedTokenPayloadData =
  861. [[NSData alloc] initWithBase64EncodedString:tokenPayload
  862. options:NSDataBase64DecodingIgnoreUnknownCharacters];
  863. if (!decodedTokenPayloadData) {
  864. if (error) {
  865. *error = [FIRAuthErrorUtils malformedJWTErrorWithToken:token underlyingError:nil];
  866. }
  867. return nil;
  868. }
  869. NSError *jsonError = nil;
  870. NSJSONReadingOptions options = NSJSONReadingMutableContainers | NSJSONReadingAllowFragments;
  871. NSDictionary *tokenPayloadDictionary =
  872. [NSJSONSerialization JSONObjectWithData:decodedTokenPayloadData
  873. options:options
  874. error:&jsonError];
  875. if (jsonError != nil) {
  876. if (error) {
  877. *error = [FIRAuthErrorUtils malformedJWTErrorWithToken:token underlyingError:jsonError];
  878. }
  879. return nil;
  880. }
  881. if (!tokenPayloadDictionary) {
  882. if (error) {
  883. *error = [FIRAuthErrorUtils malformedJWTErrorWithToken:token underlyingError:nil];
  884. }
  885. return nil;
  886. }
  887. FIRAuthTokenResult *result = [FIRAuthTokenResult tokenResultWithToken:token];
  888. return result;
  889. }
  890. /** @fn internalGetTokenForcingRefresh:callback:
  891. @brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
  892. @param callback The block to invoke when the token is available. Invoked asynchronously on the
  893. global work thread in the future.
  894. */
  895. - (void)internalGetTokenWithCallback:(nonnull FIRAuthTokenCallback)callback {
  896. [self internalGetTokenForcingRefresh:NO callback:callback];
  897. }
  898. - (void)internalGetTokenForcingRefresh:(BOOL)forceRefresh
  899. callback:(nonnull FIRAuthTokenCallback)callback {
  900. [_tokenService fetchAccessTokenForcingRefresh:forceRefresh
  901. callback:^(NSString *_Nullable token,
  902. NSError *_Nullable error, BOOL tokenUpdated) {
  903. if (error) {
  904. [self signOutIfTokenIsInvalidWithError:error];
  905. callback(nil, error);
  906. return;
  907. }
  908. if (tokenUpdated) {
  909. if (![self updateKeychain:&error]) {
  910. callback(nil, error);
  911. return;
  912. }
  913. }
  914. callback(token, nil);
  915. }];
  916. }
  917. - (void)sendEmailVerificationBeforeUpdatingEmail:(nonnull NSString *)email
  918. completion:(nullable FIRAuthVoidErrorCallback)completion {
  919. [self internalVerifyBeforeUpdateEmailWithNewEmail:email
  920. actionCodeSettings:nil
  921. completion:completion];
  922. }
  923. - (void)sendEmailVerificationBeforeUpdatingEmail:(nonnull NSString *)email
  924. actionCodeSettings:(nonnull FIRActionCodeSettings *)actionCodeSettings
  925. completion:(nullable FIRAuthVoidErrorCallback)completion {
  926. [self internalVerifyBeforeUpdateEmailWithNewEmail:email
  927. actionCodeSettings:actionCodeSettings
  928. completion:completion];
  929. }
  930. - (void)internalVerifyBeforeUpdateEmailWithNewEmail:(NSString *)newEmail
  931. actionCodeSettings:
  932. (nullable FIRActionCodeSettings *)actionCodeSettings
  933. completion:(FIRVerifyBeforeUpdateEmailCallback)completion {
  934. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  935. [self
  936. internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  937. if (error) {
  938. callInMainThreadWithError(completion, error);
  939. return;
  940. }
  941. FIRAuthRequestConfiguration *configuration = self->_auth.requestConfiguration;
  942. FIRActionCodeSettings *settings = actionCodeSettings;
  943. FIRGetOOBConfirmationCodeRequest *request = [FIRGetOOBConfirmationCodeRequest
  944. verifyBeforeUpdateEmailWithAccessToken:accessToken
  945. newEmail:newEmail
  946. actionCodeSettings:settings
  947. requestConfiguration:configuration];
  948. [FIRAuthBackend
  949. getOOBConfirmationCode:request
  950. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  951. NSError *_Nullable error) {
  952. callInMainThreadWithError(completion, error);
  953. }];
  954. }];
  955. });
  956. }
  957. - (void)linkWithCredential:(FIRAuthCredential *)credential
  958. completion:(nullable FIRAuthDataResultCallback)completion {
  959. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  960. if (self->_providerData[credential.provider]) {
  961. callInMainThreadWithAuthDataResultAndError(completion, nil,
  962. [FIRAuthErrorUtils providerAlreadyLinkedError]);
  963. return;
  964. }
  965. FIRAuthDataResult *result = [[FIRAuthDataResult alloc] initWithUser:self
  966. additionalUserInfo:nil];
  967. if ([credential isKindOfClass:[FIREmailPasswordAuthCredential class]]) {
  968. if (self->_hasEmailPasswordCredential) {
  969. callInMainThreadWithAuthDataResultAndError(completion, nil,
  970. [FIRAuthErrorUtils providerAlreadyLinkedError]);
  971. return;
  972. }
  973. FIREmailPasswordAuthCredential *emailPasswordCredential =
  974. (FIREmailPasswordAuthCredential *)credential;
  975. if (emailPasswordCredential.password) {
  976. [self updateEmail:emailPasswordCredential.email
  977. password:emailPasswordCredential.password
  978. callback:^(NSError *error) {
  979. if (error) {
  980. callInMainThreadWithAuthDataResultAndError(completion, nil, error);
  981. } else {
  982. callInMainThreadWithAuthDataResultAndError(completion, result, nil);
  983. }
  984. }];
  985. } else {
  986. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  987. NSError *_Nullable error) {
  988. NSDictionary<NSString *, NSString *> *queryItems =
  989. [FIRAuthWebUtils parseURL:emailPasswordCredential.link];
  990. if (![queryItems count]) {
  991. NSURLComponents *urlComponents =
  992. [NSURLComponents componentsWithString:emailPasswordCredential.link];
  993. queryItems = [FIRAuthWebUtils parseURL:urlComponents.query];
  994. }
  995. NSString *actionCode = queryItems[@"oobCode"];
  996. FIRAuthRequestConfiguration *requestConfiguration = self.auth.requestConfiguration;
  997. FIREmailLinkSignInRequest *request =
  998. [[FIREmailLinkSignInRequest alloc] initWithEmail:emailPasswordCredential.email
  999. oobCode:actionCode
  1000. requestConfiguration:requestConfiguration];
  1001. request.IDToken = accessToken;
  1002. [FIRAuthBackend
  1003. emailLinkSignin:request
  1004. callback:^(FIREmailLinkSignInResponse *_Nullable response,
  1005. NSError *_Nullable error) {
  1006. if (error) {
  1007. callInMainThreadWithAuthDataResultAndError(completion, nil, error);
  1008. } else {
  1009. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  1010. NSError *_Nullable error) {
  1011. if (error) {
  1012. callInMainThreadWithAuthDataResultAndError(completion, nil, error);
  1013. return;
  1014. }
  1015. FIRGetAccountInfoRequest *getAccountInfoRequest =
  1016. [[FIRGetAccountInfoRequest alloc]
  1017. initWithAccessToken:accessToken
  1018. requestConfiguration:requestConfiguration];
  1019. [FIRAuthBackend
  1020. getAccountInfo:getAccountInfoRequest
  1021. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  1022. NSError *_Nullable error) {
  1023. if (error) {
  1024. [self signOutIfTokenIsInvalidWithError:error];
  1025. callInMainThreadWithAuthDataResultAndError(completion, nil,
  1026. error);
  1027. return;
  1028. }
  1029. self.anonymous = NO;
  1030. [self updateWithGetAccountInfoResponse:response];
  1031. if (![self updateKeychain:&error]) {
  1032. callInMainThreadWithAuthDataResultAndError(completion, nil,
  1033. error);
  1034. return;
  1035. }
  1036. callInMainThreadWithAuthDataResultAndError(completion,
  1037. result, nil);
  1038. }];
  1039. }];
  1040. }
  1041. }];
  1042. }];
  1043. }
  1044. return;
  1045. }
  1046. if ([credential isKindOfClass:[FIRGameCenterAuthCredential class]]) {
  1047. FIRGameCenterAuthCredential *gameCenterCredential = (FIRGameCenterAuthCredential *)credential;
  1048. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  1049. NSError *_Nullable error) {
  1050. FIRAuthRequestConfiguration *requestConfiguration = self.auth.requestConfiguration;
  1051. FIRSignInWithGameCenterRequest *gameCenterRequest = [[FIRSignInWithGameCenterRequest alloc]
  1052. initWithPlayerID:gameCenterCredential.playerID
  1053. publicKeyURL:gameCenterCredential.publicKeyURL
  1054. signature:gameCenterCredential.signature
  1055. salt:gameCenterCredential.salt
  1056. timestamp:gameCenterCredential.timestamp
  1057. displayName:gameCenterCredential.displayName
  1058. requestConfiguration:requestConfiguration];
  1059. gameCenterRequest.accessToken = accessToken;
  1060. [FIRAuthBackend
  1061. signInWithGameCenter:gameCenterRequest
  1062. callback:^(FIRSignInWithGameCenterResponse *_Nullable response,
  1063. NSError *_Nullable error) {
  1064. if (error) {
  1065. callInMainThreadWithAuthDataResultAndError(completion, nil, error);
  1066. } else {
  1067. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  1068. NSError *_Nullable error) {
  1069. if (error) {
  1070. callInMainThreadWithAuthDataResultAndError(completion, nil, error);
  1071. return;
  1072. }
  1073. FIRGetAccountInfoRequest *getAccountInfoRequest =
  1074. [[FIRGetAccountInfoRequest alloc]
  1075. initWithAccessToken:accessToken
  1076. requestConfiguration:requestConfiguration];
  1077. [FIRAuthBackend
  1078. getAccountInfo:getAccountInfoRequest
  1079. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  1080. NSError *_Nullable error) {
  1081. if (error) {
  1082. [self signOutIfTokenIsInvalidWithError:error];
  1083. callInMainThreadWithAuthDataResultAndError(completion,
  1084. nil, error);
  1085. return;
  1086. }
  1087. self.anonymous = NO;
  1088. [self updateWithGetAccountInfoResponse:response];
  1089. if (![self updateKeychain:&error]) {
  1090. callInMainThreadWithAuthDataResultAndError(completion,
  1091. nil, error);
  1092. return;
  1093. }
  1094. callInMainThreadWithAuthDataResultAndError(completion,
  1095. result, nil);
  1096. }];
  1097. }];
  1098. }
  1099. }];
  1100. }];
  1101. return;
  1102. }
  1103. #if TARGET_OS_IOS
  1104. if ([credential isKindOfClass:[FIRPhoneAuthCredential class]]) {
  1105. FIRPhoneAuthCredential *phoneAuthCredential = (FIRPhoneAuthCredential *)credential;
  1106. [self internalUpdateOrLinkPhoneNumberCredential:phoneAuthCredential
  1107. isLinkOperation:YES
  1108. completion:^(NSError *_Nullable error) {
  1109. if (error) {
  1110. callInMainThreadWithAuthDataResultAndError(
  1111. completion, nil, error);
  1112. } else {
  1113. callInMainThreadWithAuthDataResultAndError(
  1114. completion, result, nil);
  1115. }
  1116. }];
  1117. return;
  1118. }
  1119. #endif
  1120. [self->_taskQueue enqueueTask:^(FIRAuthSerialTaskCompletionBlock _Nonnull complete) {
  1121. CallbackWithAuthDataResultAndError completeWithError =
  1122. ^(FIRAuthDataResult *result, NSError *error) {
  1123. complete();
  1124. callInMainThreadWithAuthDataResultAndError(completion, result, error);
  1125. };
  1126. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  1127. NSError *_Nullable error) {
  1128. if (error) {
  1129. completeWithError(nil, error);
  1130. return;
  1131. }
  1132. FIRAuthRequestConfiguration *requestConfiguration = self->_auth.requestConfiguration;
  1133. FIRVerifyAssertionRequest *request =
  1134. [[FIRVerifyAssertionRequest alloc] initWithProviderID:credential.provider
  1135. requestConfiguration:requestConfiguration];
  1136. [credential prepareVerifyAssertionRequest:request];
  1137. request.accessToken = accessToken;
  1138. [FIRAuthBackend
  1139. verifyAssertion:request
  1140. callback:^(FIRVerifyAssertionResponse *response, NSError *error) {
  1141. if (error) {
  1142. [self signOutIfTokenIsInvalidWithError:error];
  1143. completeWithError(nil, error);
  1144. return;
  1145. }
  1146. FIRAdditionalUserInfo *additionalUserInfo =
  1147. [FIRAdditionalUserInfo userInfoWithVerifyAssertionResponse:response];
  1148. FIROAuthCredential *updatedOAuthCredential =
  1149. [[FIROAuthCredential alloc] initWithVerifyAssertionResponse:response];
  1150. FIRAuthDataResult *result =
  1151. [[FIRAuthDataResult alloc] initWithUser:self
  1152. additionalUserInfo:additionalUserInfo
  1153. credential:updatedOAuthCredential];
  1154. // Update the new token and refresh user info again.
  1155. self->_tokenService = [[FIRSecureTokenService alloc]
  1156. initWithRequestConfiguration:requestConfiguration
  1157. accessToken:response.IDToken
  1158. accessTokenExpirationDate:response.approximateExpirationDate
  1159. refreshToken:response.refreshToken];
  1160. [self internalGetTokenWithCallback:^(NSString *_Nullable accessToken,
  1161. NSError *_Nullable error) {
  1162. if (error) {
  1163. completeWithError(nil, error);
  1164. return;
  1165. }
  1166. FIRGetAccountInfoRequest *getAccountInfoRequest =
  1167. [[FIRGetAccountInfoRequest alloc]
  1168. initWithAccessToken:accessToken
  1169. requestConfiguration:requestConfiguration];
  1170. [FIRAuthBackend
  1171. getAccountInfo:getAccountInfoRequest
  1172. callback:^(FIRGetAccountInfoResponse *_Nullable response,
  1173. NSError *_Nullable error) {
  1174. if (error) {
  1175. [self signOutIfTokenIsInvalidWithError:error];
  1176. completeWithError(nil, error);
  1177. return;
  1178. }
  1179. self.anonymous = NO;
  1180. [self updateWithGetAccountInfoResponse:response];
  1181. if (![self updateKeychain:&error]) {
  1182. completeWithError(nil, error);
  1183. return;
  1184. }
  1185. completeWithError(result, nil);
  1186. }];
  1187. }];
  1188. }];
  1189. }];
  1190. }];
  1191. });
  1192. }
  1193. - (void)linkWithProvider:(id<FIRFederatedAuthProvider>)provider
  1194. UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
  1195. completion:(nullable FIRAuthDataResultCallback)completion {
  1196. #if TARGET_OS_IOS
  1197. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  1198. [provider getCredentialWithUIDelegate:UIDelegate
  1199. completion:^(FIRAuthCredential *_Nullable credential,
  1200. NSError *_Nullable error) {
  1201. [self linkWithCredential:credential completion:completion];
  1202. }];
  1203. });
  1204. #endif // TARGET_OS_IOS
  1205. }
  1206. - (void)unlinkFromProvider:(NSString *)provider
  1207. completion:(nullable FIRAuthResultCallback)completion {
  1208. [_taskQueue enqueueTask:^(FIRAuthSerialTaskCompletionBlock _Nonnull complete) {
  1209. CallbackWithError completeAndCallbackWithError = ^(NSError *error) {
  1210. complete();
  1211. callInMainThreadWithUserAndError(completion, self, error);
  1212. };
  1213. [self
  1214. internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  1215. if (error) {
  1216. completeAndCallbackWithError(error);
  1217. return;
  1218. }
  1219. FIRAuthRequestConfiguration *requestConfiguration = self->_auth.requestConfiguration;
  1220. FIRSetAccountInfoRequest *setAccountInfoRequest =
  1221. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:requestConfiguration];
  1222. setAccountInfoRequest.accessToken = accessToken;
  1223. if (!self->_providerData[provider]) {
  1224. completeAndCallbackWithError([FIRAuthErrorUtils noSuchProviderError]);
  1225. return;
  1226. }
  1227. setAccountInfoRequest.deleteProviders = @[ provider ];
  1228. [FIRAuthBackend
  1229. setAccountInfo:setAccountInfoRequest
  1230. callback:^(FIRSetAccountInfoResponse *_Nullable response,
  1231. NSError *_Nullable error) {
  1232. if (error) {
  1233. [self signOutIfTokenIsInvalidWithError:error];
  1234. completeAndCallbackWithError(error);
  1235. return;
  1236. }
  1237. // We can't just use the provider info objects in FIRSetAccountInfoResponse
  1238. // because they don't have localID and email fields. Remove the specific
  1239. // provider manually.
  1240. NSMutableDictionary *mutableProviderData = [self->_providerData mutableCopy];
  1241. [mutableProviderData removeObjectForKey:provider];
  1242. self->_providerData = [mutableProviderData copy];
  1243. if ([provider isEqualToString:FIREmailAuthProviderID]) {
  1244. self->_hasEmailPasswordCredential = NO;
  1245. }
  1246. #if TARGET_OS_IOS
  1247. // After successfully unlinking a phone auth provider, remove the phone number
  1248. // from the cached user info.
  1249. if ([provider isEqualToString:FIRPhoneAuthProviderID]) {
  1250. self->_phoneNumber = nil;
  1251. }
  1252. #endif
  1253. if (response.IDToken && response.refreshToken) {
  1254. FIRSecureTokenService *tokenService = [[FIRSecureTokenService alloc]
  1255. initWithRequestConfiguration:requestConfiguration
  1256. accessToken:response.IDToken
  1257. accessTokenExpirationDate:response.approximateExpirationDate
  1258. refreshToken:response.refreshToken];
  1259. [self setTokenService:tokenService
  1260. callback:^(NSError *_Nullable error) {
  1261. completeAndCallbackWithError(error);
  1262. }];
  1263. return;
  1264. }
  1265. if (![self updateKeychain:&error]) {
  1266. completeAndCallbackWithError(error);
  1267. return;
  1268. }
  1269. completeAndCallbackWithError(nil);
  1270. }];
  1271. }];
  1272. }];
  1273. }
  1274. - (void)sendEmailVerificationWithCompletion:(nullable FIRSendEmailVerificationCallback)completion {
  1275. [self sendEmailVerificationWithNullableActionCodeSettings:nil completion:completion];
  1276. }
  1277. - (void)sendEmailVerificationWithActionCodeSettings:(FIRActionCodeSettings *)actionCodeSettings
  1278. completion:
  1279. (nullable FIRSendEmailVerificationCallback)completion {
  1280. [self sendEmailVerificationWithNullableActionCodeSettings:actionCodeSettings
  1281. completion:completion];
  1282. }
  1283. /** @fn sendEmailVerificationWithNullableActionCodeSettings:completion:
  1284. @brief Initiates email verification for the user.
  1285. @param actionCodeSettings Optionally, a @c FIRActionCodeSettings object containing settings
  1286. related to the handling action codes.
  1287. */
  1288. - (void)sendEmailVerificationWithNullableActionCodeSettings:
  1289. (nullable FIRActionCodeSettings *)actionCodeSettings
  1290. completion:
  1291. (nullable FIRSendEmailVerificationCallback)
  1292. completion {
  1293. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  1294. [self
  1295. internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  1296. if (error) {
  1297. callInMainThreadWithError(completion, error);
  1298. return;
  1299. }
  1300. FIRAuthRequestConfiguration *configuration = self->_auth.requestConfiguration;
  1301. FIRGetOOBConfirmationCodeRequest *request =
  1302. [FIRGetOOBConfirmationCodeRequest verifyEmailRequestWithAccessToken:accessToken
  1303. actionCodeSettings:actionCodeSettings
  1304. requestConfiguration:configuration];
  1305. [FIRAuthBackend
  1306. getOOBConfirmationCode:request
  1307. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  1308. NSError *_Nullable error) {
  1309. [self signOutIfTokenIsInvalidWithError:error];
  1310. callInMainThreadWithError(completion, error);
  1311. }];
  1312. }];
  1313. });
  1314. }
  1315. - (void)deleteWithCompletion:(nullable FIRUserProfileChangeCallback)completion {
  1316. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  1317. [self
  1318. internalGetTokenWithCallback:^(NSString *_Nullable accessToken, NSError *_Nullable error) {
  1319. if (error) {
  1320. callInMainThreadWithError(completion, error);
  1321. return;
  1322. }
  1323. FIRDeleteAccountRequest *deleteUserRequest =
  1324. [[FIRDeleteAccountRequest alloc] initWitLocalID:self->_userID
  1325. accessToken:accessToken
  1326. requestConfiguration:self->_auth.requestConfiguration];
  1327. [FIRAuthBackend deleteAccount:deleteUserRequest
  1328. callback:^(NSError *_Nullable error) {
  1329. if (error) {
  1330. callInMainThreadWithError(completion, error);
  1331. return;
  1332. }
  1333. if (![self->_auth signOutByForceWithUserID:self->_userID
  1334. error:&error]) {
  1335. callInMainThreadWithError(completion, error);
  1336. return;
  1337. }
  1338. callInMainThreadWithError(completion, error);
  1339. }];
  1340. }];
  1341. });
  1342. }
  1343. /** @fn signOutIfTokenIsInvalidWithError:
  1344. @brief Signs out this user if the user or the token is invalid.
  1345. @param error The error from the server.
  1346. */
  1347. - (void)signOutIfTokenIsInvalidWithError:(nullable NSError *)error {
  1348. NSInteger errorCode = error.code;
  1349. if (errorCode == FIRAuthErrorCodeUserNotFound || errorCode == FIRAuthErrorCodeUserDisabled ||
  1350. errorCode == FIRAuthErrorCodeInvalidUserToken ||
  1351. errorCode == FIRAuthErrorCodeUserTokenExpired) {
  1352. FIRLogNotice(kFIRLoggerAuth, @"I-AUT000016",
  1353. @"Invalid user token detected, user is automatically signed out.");
  1354. [_auth signOutByForceWithUserID:_userID error:NULL];
  1355. }
  1356. }
  1357. @end
  1358. @implementation FIRUserProfileChangeRequest {
  1359. /** @var _user
  1360. @brief The user associated with the change request.
  1361. */
  1362. FIRUser *_user;
  1363. /** @var _displayName
  1364. @brief The display name value to set if @c _displayNameSet is YES.
  1365. */
  1366. NSString *_displayName;
  1367. /** @var _displayNameSet
  1368. @brief Indicates the display name should be part of the change request.
  1369. */
  1370. BOOL _displayNameSet;
  1371. /** @var _photoURL
  1372. @brief The photo URL value to set if @c _displayNameSet is YES.
  1373. */
  1374. NSURL *_photoURL;
  1375. /** @var _photoURLSet
  1376. @brief Indicates the photo URL should be part of the change request.
  1377. */
  1378. BOOL _photoURLSet;
  1379. /** @var _consumed
  1380. @brief Indicates the @c commitChangesWithCallback: method has already been invoked.
  1381. */
  1382. BOOL _consumed;
  1383. }
  1384. - (nullable instancetype)initWithUser:(FIRUser *)user {
  1385. self = [super init];
  1386. if (self) {
  1387. _user = user;
  1388. }
  1389. return self;
  1390. }
  1391. - (nullable NSString *)displayName {
  1392. return _displayName;
  1393. }
  1394. - (void)setDisplayName:(nullable NSString *)displayName {
  1395. dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
  1396. if (self->_consumed) {
  1397. [NSException
  1398. raise:NSInternalInconsistencyException
  1399. format:@"%@", @"Invalid call to setDisplayName: after commitChangesWithCallback:."];
  1400. return;
  1401. }
  1402. self->_displayNameSet = YES;
  1403. self->_displayName = [displayName copy];
  1404. });
  1405. }
  1406. - (nullable NSURL *)photoURL {
  1407. return _photoURL;
  1408. }
  1409. - (void)setPhotoURL:(nullable NSURL *)photoURL {
  1410. dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
  1411. if (self->_consumed) {
  1412. [NSException raise:NSInternalInconsistencyException
  1413. format:@"%@", @"Invalid call to setPhotoURL: after commitChangesWithCallback:."];
  1414. return;
  1415. }
  1416. self->_photoURLSet = YES;
  1417. self->_photoURL = [photoURL copy];
  1418. });
  1419. }
  1420. /** @fn hasUpdates
  1421. @brief Indicates at least one field has a value which needs to be committed.
  1422. */
  1423. - (BOOL)hasUpdates {
  1424. return _displayNameSet || _photoURLSet;
  1425. }
  1426. - (void)commitChangesWithCompletion:(nullable FIRUserProfileChangeCallback)completion {
  1427. dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
  1428. if (self->_consumed) {
  1429. [NSException raise:NSInternalInconsistencyException
  1430. format:@"%@", @"commitChangesWithCallback: should only be called once."];
  1431. return;
  1432. }
  1433. self->_consumed = YES;
  1434. // Return fast if there is nothing to update:
  1435. if (![self hasUpdates]) {
  1436. callInMainThreadWithError(completion, nil);
  1437. return;
  1438. }
  1439. NSString *displayName = [self->_displayName copy];
  1440. BOOL displayNameWasSet = self->_displayNameSet;
  1441. NSURL *photoURL = [self->_photoURL copy];
  1442. BOOL photoURLWasSet = self->_photoURLSet;
  1443. [self->_user
  1444. executeUserUpdateWithChanges:^(FIRGetAccountInfoResponseUser *user,
  1445. FIRSetAccountInfoRequest *request) {
  1446. if (photoURLWasSet) {
  1447. request.photoURL = photoURL;
  1448. }
  1449. if (displayNameWasSet) {
  1450. request.displayName = displayName;
  1451. }
  1452. }
  1453. callback:^(NSError *_Nullable error) {
  1454. if (error) {
  1455. callInMainThreadWithError(completion, error);
  1456. return;
  1457. }
  1458. if (displayNameWasSet) {
  1459. [self->_user setDisplayName:displayName];
  1460. }
  1461. if (photoURLWasSet) {
  1462. [self->_user setPhotoURL:photoURL];
  1463. }
  1464. if (![self->_user updateKeychain:&error]) {
  1465. callInMainThreadWithError(completion, error);
  1466. return;
  1467. }
  1468. callInMainThreadWithError(completion, nil);
  1469. }];
  1470. });
  1471. }
  1472. @end
  1473. NS_ASSUME_NONNULL_END