FIRGetAccountInfoResponse.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/Backend/RPC/FIRGetAccountInfoResponse.h"
  17. #import "FirebaseAuth/Sources/Backend/RPC/Proto/FIRPasskeyInfo_Internal.h"
  18. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPasskeyInfo.h"
  19. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. /** @var kErrorKey
  22. @brief The key for the "error" value in JSON responses from the server.
  23. */
  24. static NSString *const kErrorKey = @"error";
  25. @implementation FIRGetAccountInfoResponseProviderUserInfo
  26. - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
  27. self = [super init];
  28. if (self) {
  29. _providerID = [dictionary[@"providerId"] copy];
  30. _displayName = [dictionary[@"displayName"] copy];
  31. NSString *photoURL = dictionary[@"photoUrl"];
  32. if (photoURL) {
  33. _photoURL = [NSURL URLWithString:photoURL];
  34. }
  35. _federatedID = [dictionary[@"federatedId"] copy];
  36. _email = [dictionary[@"email"] copy];
  37. _phoneNumber = [dictionary[@"phoneNumber"] copy];
  38. }
  39. return self;
  40. }
  41. @end
  42. @implementation FIRGetAccountInfoResponseUser
  43. - (instancetype)initWithDictionary:(NSDictionary *)dictionary {
  44. self = [super init];
  45. if (self) {
  46. NSArray<NSDictionary *> *providerUserInfoData = dictionary[@"providerUserInfo"];
  47. if (providerUserInfoData) {
  48. NSMutableArray<FIRGetAccountInfoResponseProviderUserInfo *> *providerUserInfoArray =
  49. [NSMutableArray arrayWithCapacity:providerUserInfoData.count];
  50. for (NSDictionary *dictionary in providerUserInfoData) {
  51. [providerUserInfoArray addObject:[[FIRGetAccountInfoResponseProviderUserInfo alloc]
  52. initWithDictionary:dictionary]];
  53. }
  54. _providerUserInfo = [providerUserInfoArray copy];
  55. }
  56. _localID = [dictionary[@"localId"] copy];
  57. _displayName = [dictionary[@"displayName"] copy];
  58. _email = [dictionary[@"email"] copy];
  59. NSString *photoURL = dictionary[@"photoUrl"];
  60. if (photoURL) {
  61. _photoURL = [NSURL URLWithString:photoURL];
  62. }
  63. if ([dictionary[@"createdAt"] isKindOfClass:[NSString class]]) {
  64. // Divide by 1000 in order to convert miliseconds to seconds.
  65. NSTimeInterval creationDateTimeInterval = [dictionary[@"createdAt"] doubleValue] / 1000;
  66. _creationDate = [NSDate dateWithTimeIntervalSince1970:creationDateTimeInterval];
  67. }
  68. if ([dictionary[@"lastLoginAt"] isKindOfClass:[NSString class]]) {
  69. // Divide by 1000 in order to convert miliseconds to seconds
  70. NSTimeInterval creationDateTimeInterval = [dictionary[@"lastLoginAt"] doubleValue] / 1000;
  71. _lastLoginDate = [NSDate dateWithTimeIntervalSince1970:creationDateTimeInterval];
  72. }
  73. _emailVerified = [dictionary[@"emailVerified"] boolValue];
  74. _passwordHash = [dictionary[@"passwordHash"] copy];
  75. _phoneNumber = [dictionary[@"phoneNumber"] copy];
  76. NSArray<NSDictionary *> *MFAEnrollmentData = dictionary[@"mfaInfo"];
  77. if (MFAEnrollmentData) {
  78. NSMutableArray<FIRAuthProtoMFAEnrollment *> *MFAEnrollments =
  79. [NSMutableArray arrayWithCapacity:MFAEnrollmentData.count];
  80. for (NSDictionary *dictionary in MFAEnrollmentData) {
  81. [MFAEnrollments
  82. addObject:[[FIRAuthProtoMFAEnrollment alloc] initWithDictionary:dictionary]];
  83. }
  84. _MFAEnrollments = [MFAEnrollments copy];
  85. }
  86. // Get enrolled passkey list
  87. NSArray<NSDictionary *> *passkeyEnrollmentData = dictionary[@"passkeyInfo"];
  88. if (passkeyEnrollmentData) {
  89. NSMutableArray<FIRPasskeyInfo *> *enrolledPasskeys =
  90. [NSMutableArray arrayWithCapacity:passkeyEnrollmentData.count];
  91. for (NSDictionary *dictionary in passkeyEnrollmentData) {
  92. [enrolledPasskeys addObject:[[FIRPasskeyInfo alloc] initWithDictionary:dictionary]];
  93. }
  94. _enrolledPasskeys = [enrolledPasskeys copy];
  95. }
  96. }
  97. return self;
  98. }
  99. @end
  100. @implementation FIRGetAccountInfoResponse
  101. - (BOOL)setWithDictionary:(NSDictionary *)dictionary error:(NSError *_Nullable *_Nullable)error {
  102. NSArray<NSDictionary *> *usersData = dictionary[@"users"];
  103. // The client side never sends a getAccountInfo request with multiple localID, so only one user
  104. // data is expected in the response.
  105. if (![usersData isKindOfClass:[NSArray class]] || usersData.count != 1) {
  106. if (error) {
  107. *error = [FIRAuthErrorUtils unexpectedResponseWithDeserializedResponse:dictionary];
  108. }
  109. return NO;
  110. }
  111. _users = @[ [[FIRGetAccountInfoResponseUser alloc] initWithDictionary:usersData.firstObject] ];
  112. return YES;
  113. }
  114. @end
  115. NS_ASSUME_NONNULL_END