FIRUser.m 72 KB

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