GIDProfileData.m 5.4 KB

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