GIDGoogleUser.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2022 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/GIDGoogleUser.h"
  15. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  17. #import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
  18. #import "GoogleSignIn/Sources/GIDProfileData_Private.h"
  19. #import "GoogleSignIn/Sources/GIDToken_Private.h"
  20. #ifdef SWIFT_PACKAGE
  21. @import AppAuth;
  22. #else
  23. #import <AppAuth/AppAuth.h>
  24. #endif
  25. // The ID Token claim key for the hosted domain value.
  26. static NSString *const kHostedDomainIDTokenClaimKey = @"hd";
  27. // Key constants used for encode and decode.
  28. static NSString *const kAuthenticationKey = @"authentication";
  29. static NSString *const kProfileDataKey = @"profileData";
  30. static NSString *const kAuthState = @"authState";
  31. // Parameters for the token exchange endpoint.
  32. static NSString *const kAudienceParameter = @"audience";
  33. static NSString *const kOpenIDRealmParameter = @"openid.realm";
  34. NS_ASSUME_NONNULL_BEGIN
  35. @interface GIDGoogleUser ()
  36. @property(nonatomic, readwrite) GIDToken *accessToken;
  37. @property(nonatomic, readwrite) GIDToken *refreshToken;
  38. @property(nonatomic, readwrite, nullable) GIDToken *idToken;
  39. @end
  40. @implementation GIDGoogleUser {
  41. OIDAuthState *_authState;
  42. GIDConfiguration *_cachedConfiguration;
  43. }
  44. - (nullable NSString *)userID {
  45. NSString *idTokenString = self.idToken.tokenString;
  46. if (idTokenString) {
  47. OIDIDToken *idTokenDecoded =
  48. [[OIDIDToken alloc] initWithIDTokenString:idTokenString];
  49. if (idTokenDecoded && idTokenDecoded.subject) {
  50. return [idTokenDecoded.subject copy];
  51. }
  52. }
  53. return nil;
  54. }
  55. - (nullable NSArray<NSString *> *)grantedScopes {
  56. NSArray<NSString *> *grantedScopes;
  57. NSString *grantedScopeString = _authState.lastTokenResponse.scope;
  58. if (grantedScopeString) {
  59. // If we have a 'scope' parameter from the backend, this is authoritative.
  60. // Remove leading and trailing whitespace.
  61. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  62. [NSCharacterSet whitespaceCharacterSet]];
  63. // Tokenize with space as a delimiter.
  64. NSMutableArray<NSString *> *parsedScopes =
  65. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  66. // Remove empty strings.
  67. [parsedScopes removeObject:@""];
  68. grantedScopes = [parsedScopes copy];
  69. }
  70. return grantedScopes;
  71. }
  72. - (GIDConfiguration *)configuration {
  73. @synchronized(self) {
  74. // Caches the configuration since it would not change for one GIDGoogleUser instance.
  75. if (!_cachedConfiguration) {
  76. NSString *clientID = _authState.lastAuthorizationResponse.request.clientID;
  77. NSString *serverClientID =
  78. _authState.lastTokenResponse.request.additionalParameters[kAudienceParameter];
  79. NSString *openIDRealm =
  80. _authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter];
  81. _cachedConfiguration = [[GIDConfiguration alloc] initWithClientID:clientID
  82. serverClientID:serverClientID
  83. hostedDomain:[self hostedDomain]
  84. openIDRealm:openIDRealm];
  85. };
  86. }
  87. return _cachedConfiguration;
  88. }
  89. #pragma mark - Private Methods
  90. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  91. profileData:(nullable GIDProfileData *)profileData {
  92. self = [super init];
  93. if (self) {
  94. [self updateAuthState:authState profileData:profileData];
  95. }
  96. return self;
  97. }
  98. - (void)updateAuthState:(OIDAuthState *)authState
  99. profileData:(nullable GIDProfileData *)profileData {
  100. @synchronized(self) {
  101. _authState = authState;
  102. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  103. _profile = profileData;
  104. [self updateTokensWithAuthState:authState];
  105. }
  106. }
  107. - (void)updateTokensWithAuthState:(OIDAuthState *)authState {
  108. GIDToken *accessToken =
  109. [[GIDToken alloc] initWithTokenString:authState.lastTokenResponse.accessToken
  110. expirationDate:authState.lastTokenResponse.accessTokenExpirationDate];
  111. if (![self.accessToken isEqualToToken:accessToken]) {
  112. self.accessToken = accessToken;
  113. }
  114. GIDToken *refreshToken = [[GIDToken alloc] initWithTokenString:authState.refreshToken
  115. expirationDate:nil];
  116. if (![self.refreshToken isEqualToToken:refreshToken]) {
  117. self.refreshToken = refreshToken;
  118. }
  119. GIDToken *idToken;
  120. NSString *idTokenString = authState.lastTokenResponse.idToken;
  121. if (idTokenString) {
  122. NSDate *idTokenExpirationDate =
  123. [[[OIDIDToken alloc] initWithIDTokenString:idTokenString] expiresAt];
  124. idToken = [[GIDToken alloc] initWithTokenString:idTokenString
  125. expirationDate:idTokenExpirationDate];
  126. } else {
  127. idToken = nil;
  128. }
  129. if ((self.idToken || idToken) && ![self.idToken isEqualToToken:idToken]) {
  130. self.idToken = idToken;
  131. }
  132. }
  133. #pragma mark - Helpers
  134. - (nullable NSString *)hostedDomain {
  135. NSString *idTokenString = self.idToken.tokenString;
  136. if (idTokenString) {
  137. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idTokenString];
  138. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  139. return idTokenDecoded.claims[kHostedDomainIDTokenClaimKey];
  140. }
  141. }
  142. return nil;
  143. }
  144. #pragma mark - NSSecureCoding
  145. + (BOOL)supportsSecureCoding {
  146. return YES;
  147. }
  148. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  149. self = [super init];
  150. if (self) {
  151. GIDProfileData *profileData =
  152. [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  153. OIDAuthState *authState;
  154. if ([decoder containsValueForKey:kAuthState]) { // Current encoding
  155. authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  156. } else { // Old encoding
  157. GIDAuthentication *authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  158. forKey:kAuthenticationKey];
  159. authState = authentication.authState;
  160. }
  161. [self updateAuthState:authState profileData:profileData];
  162. }
  163. return self;
  164. }
  165. - (void)encodeWithCoder:(NSCoder *)encoder {
  166. [encoder encodeObject:_profile forKey:kProfileDataKey];
  167. [encoder encodeObject:_authState forKey:kAuthState];
  168. }
  169. @end
  170. NS_ASSUME_NONNULL_END