GIDProfileData.m 6.3 KB

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