GIDProfileData.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
  15. #import "GoogleSignIn/Sources/GIDProfileData_Private.h"
  16. NS_ASSUME_NONNULL_BEGIN
  17. // Key constants used for encode and decode.
  18. static NSString *const kEmailKey = @"email";
  19. static NSString *const kNameKey = @"name";
  20. static NSString *const kGivenNameKey = @"given_name";
  21. static NSString *const kFamilyNameKey = @"family_name";
  22. static NSString *const kImageURLKey = @"image_url";
  23. static NSString *const kOldImageURLStringKey = @"picture";
  24. @implementation GIDProfileData {
  25. NSURL *_imageURL;
  26. }
  27. - (instancetype)initWithEmail:(NSString *)email
  28. name:(NSString *)name
  29. givenName:(NSString *)givenName
  30. familyName:(NSString *)familyName
  31. imageURL:(NSURL *)imageURL {
  32. self = [super init];
  33. if (self) {
  34. _email = [email copy];
  35. _name = [name copy];
  36. _givenName = [givenName copy];
  37. _familyName = [familyName copy];
  38. _imageURL = [imageURL copy];
  39. }
  40. return self;
  41. }
  42. - (BOOL)hasImage {
  43. return _imageURL != nil;
  44. }
  45. - (NSURL *)imageURLWithDimension:(NSUInteger)dimension {
  46. if (!_imageURL) {
  47. return nil;
  48. }
  49. if ([self isFIFEAvatarURL:_imageURL]) {
  50. return [NSURL URLWithString:
  51. [NSString stringWithFormat:@"%@=s%lu", _imageURL, (unsigned long)dimension]];
  52. } else {
  53. return [NSURL URLWithString:
  54. [NSString stringWithFormat:@"%@?sz=%lu", _imageURL, (unsigned long)dimension]];
  55. }
  56. }
  57. - (BOOL)isFIFEAvatarURL:(NSURL *)url {
  58. static NSString *const AvatarURLPattern =
  59. @"lh[3-6](-tt|-d[a-g,z]|-testonly)?\\.(google|googleusercontent)\\.[a-z]+\\/(a|a-)\\/";
  60. NSError *error = NULL;
  61. NSRegularExpression *regex =
  62. [NSRegularExpression regularExpressionWithPattern:AvatarURLPattern
  63. options:0
  64. error:&error];
  65. if (!regex) {
  66. return NO;
  67. }
  68. NSUInteger matches = [regex numberOfMatchesInString:url.absoluteString
  69. options:0
  70. range:NSMakeRange(0, url.absoluteString.length)];
  71. if (matches) {
  72. return YES;
  73. }
  74. return NO;
  75. }
  76. #pragma mark - NSSecureCoding
  77. + (BOOL)supportsSecureCoding {
  78. return YES;
  79. }
  80. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  81. self = [super init];
  82. if (self) {
  83. _email = [decoder decodeObjectOfClass:[NSString class] forKey:kEmailKey];
  84. _name = [decoder decodeObjectOfClass:[NSString class] forKey:kNameKey];
  85. _givenName = [decoder decodeObjectOfClass:[NSString class] forKey:kGivenNameKey];
  86. _familyName = [decoder decodeObjectOfClass:[NSString class] forKey:kFamilyNameKey];
  87. _imageURL = [decoder decodeObjectOfClass:[NSURL class] forKey:kImageURLKey];
  88. // Check to see if this is an old archive, if so, try decoding the old image URL string key.
  89. if ([decoder containsValueForKey:kOldImageURLStringKey]) {
  90. _imageURL = [NSURL URLWithString:[decoder decodeObjectOfClass:[NSString class]
  91. forKey:kOldImageURLStringKey]];
  92. }
  93. }
  94. return self;
  95. }
  96. - (void)encodeWithCoder:(NSCoder *)encoder {
  97. [encoder encodeObject:_email forKey:kEmailKey];
  98. [encoder encodeObject:_name forKey:kNameKey];
  99. [encoder encodeObject:_givenName forKey:kGivenNameKey];
  100. [encoder encodeObject:_familyName forKey:kFamilyNameKey];
  101. [encoder encodeObject:_imageURL forKey:kImageURLKey];
  102. }
  103. #pragma mark - NSCopying
  104. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  105. // Instances of this class are immutable so we'll return self per NSCopying docs guidance.
  106. return self;
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END