GIDProfileData.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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:(nullable NSString *)givenName
  30. familyName:(nullable NSString *)familyName
  31. imageURL:(nullable 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. - (nullable NSURL *)imageURLWithDimension:(NSUInteger)dimension {
  46. if (!_imageURL) {
  47. return nil;
  48. }
  49. NSURLComponents *url = [NSURLComponents componentsWithURL:_imageURL
  50. resolvingAgainstBaseURL:YES];
  51. if ([self isFIFEAvatarURL:_imageURL]) {
  52. // Remove any preexisting FIFE Avatar URL options
  53. NSError *error;
  54. NSRegularExpression *regex =
  55. [NSRegularExpression regularExpressionWithPattern:@"=.*"
  56. options:0
  57. error:&error];
  58. url.path = [regex stringByReplacingMatchesInString:url.path
  59. options:0
  60. range:NSMakeRange(0, url.path.length)
  61. withTemplate:@""];
  62. // Append our own FIFE Avatar URL options to the path
  63. url.path = [NSString stringWithFormat:@"%@=s%@", url.path, @(dimension)];
  64. return url.URL;
  65. } else {
  66. // Append our own FIFE image URL options query string, replacing any existing query string.
  67. NSURLQueryItem *queryItem =
  68. [NSURLQueryItem queryItemWithName:@"sz"
  69. value:[NSString stringWithFormat:@"%@", @(dimension)]];
  70. url.queryItems = @[ queryItem ];
  71. return url.URL;
  72. }
  73. }
  74. - (BOOL)isFIFEAvatarURL:(NSURL *)url {
  75. static NSString *const AvatarURLPattern =
  76. @"lh[3-6](-tt|-d[a-g,z]|-testonly)?\\.(google|googleusercontent)\\.[a-z]+\\/(a|a-)\\/";
  77. NSError *error;
  78. NSRegularExpression *regex =
  79. [NSRegularExpression regularExpressionWithPattern:AvatarURLPattern
  80. options:0
  81. error:&error];
  82. if (!regex) {
  83. return NO;
  84. }
  85. NSUInteger matches = [regex numberOfMatchesInString:url.absoluteString
  86. options:0
  87. range:NSMakeRange(0, url.absoluteString.length)];
  88. if (matches) {
  89. return YES;
  90. }
  91. return NO;
  92. }
  93. #pragma mark - NSSecureCoding
  94. + (BOOL)supportsSecureCoding {
  95. return YES;
  96. }
  97. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  98. self = [super init];
  99. if (self) {
  100. _email = [decoder decodeObjectOfClass:[NSString class] forKey:kEmailKey];
  101. _name = [decoder decodeObjectOfClass:[NSString class] forKey:kNameKey];
  102. _givenName = [decoder decodeObjectOfClass:[NSString class] forKey:kGivenNameKey];
  103. _familyName = [decoder decodeObjectOfClass:[NSString class] forKey:kFamilyNameKey];
  104. _imageURL = [decoder decodeObjectOfClass:[NSURL class] forKey:kImageURLKey];
  105. // Check to see if this is an old archive, if so, try decoding the old image URL string key.
  106. if ([decoder containsValueForKey:kOldImageURLStringKey]) {
  107. _imageURL = [NSURL URLWithString:[decoder decodeObjectOfClass:[NSString class]
  108. forKey:kOldImageURLStringKey]];
  109. }
  110. }
  111. return self;
  112. }
  113. - (void)encodeWithCoder:(NSCoder *)encoder {
  114. [encoder encodeObject:_email forKey:kEmailKey];
  115. [encoder encodeObject:_name forKey:kNameKey];
  116. [encoder encodeObject:_givenName forKey:kGivenNameKey];
  117. [encoder encodeObject:_familyName forKey:kFamilyNameKey];
  118. [encoder encodeObject:_imageURL forKey:kImageURLKey];
  119. }
  120. #pragma mark - NSCopying
  121. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  122. // Instances of this class are immutable so we'll return self per NSCopying docs guidance.
  123. return self;
  124. }
  125. @end
  126. NS_ASSUME_NONNULL_END