You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.8 KiB

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