GIDGoogleUser.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. @implementation GIDGoogleUser {
  36. OIDAuthState *_authState;
  37. GIDConfiguration *_cachedConfiguration;
  38. GIDToken *_cachedAccessToken;
  39. GIDToken *_cachedRefreshToken;
  40. GIDToken *_cachedIdToken;
  41. }
  42. - (nullable NSString *)userID {
  43. NSString *idTokenString = self.idToken.tokenString;
  44. if (idTokenString) {
  45. OIDIDToken *idTokenDecoded =
  46. [[OIDIDToken alloc] initWithIDTokenString:idTokenString];
  47. if (idTokenDecoded && idTokenDecoded.subject) {
  48. return [idTokenDecoded.subject copy];
  49. }
  50. }
  51. return nil;
  52. }
  53. - (nullable NSArray<NSString *> *)grantedScopes {
  54. NSArray<NSString *> *grantedScopes;
  55. NSString *grantedScopeString = _authState.lastTokenResponse.scope;
  56. if (grantedScopeString) {
  57. // If we have a 'scope' parameter from the backend, this is authoritative.
  58. // Remove leading and trailing whitespace.
  59. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  60. [NSCharacterSet whitespaceCharacterSet]];
  61. // Tokenize with space as a delimiter.
  62. NSMutableArray<NSString *> *parsedScopes =
  63. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  64. // Remove empty strings.
  65. [parsedScopes removeObject:@""];
  66. grantedScopes = [parsedScopes copy];
  67. }
  68. return grantedScopes;
  69. }
  70. - (GIDConfiguration *)configuration {
  71. @synchronized(self) {
  72. // Caches the configuration since it would not change for one GIDGoogleUser instance.
  73. if (!_cachedConfiguration) {
  74. NSString *clientID = _authState.lastAuthorizationResponse.request.clientID;
  75. NSString *serverClientID =
  76. _authState.lastTokenResponse.request.additionalParameters[kAudienceParameter];
  77. NSString *openIDRealm =
  78. _authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter];
  79. _cachedConfiguration = [[GIDConfiguration alloc] initWithClientID:clientID
  80. serverClientID:serverClientID
  81. hostedDomain:[self hostedDomain]
  82. openIDRealm:openIDRealm];
  83. };
  84. }
  85. return _cachedConfiguration;
  86. }
  87. - (GIDToken *)accessToken {
  88. @synchronized(self) {
  89. if (!_cachedAccessToken) {
  90. _cachedAccessToken = [[GIDToken alloc] initWithTokenString:_authState.lastTokenResponse.accessToken
  91. expirationDate:_authState.lastTokenResponse.
  92. accessTokenExpirationDate];
  93. }
  94. }
  95. return _cachedAccessToken;
  96. }
  97. - (GIDToken *)refreshToken {
  98. @synchronized(self) {
  99. if (!_cachedRefreshToken) {
  100. _cachedRefreshToken = [[GIDToken alloc] initWithTokenString:_authState.refreshToken
  101. expirationDate:nil];
  102. }
  103. }
  104. return _cachedRefreshToken;
  105. }
  106. - (nullable GIDToken *)idToken {
  107. @synchronized(self) {
  108. NSString *idTokenString = _authState.lastTokenResponse.idToken;
  109. if (!_cachedIdToken && idTokenString) {
  110. NSDate *idTokenExpirationDate = [[[OIDIDToken alloc]
  111. initWithIDTokenString:idTokenString] expiresAt];
  112. _cachedIdToken = [[GIDToken alloc] initWithTokenString:idTokenString
  113. expirationDate:idTokenExpirationDate];
  114. }
  115. }
  116. return _cachedIdToken;
  117. }
  118. #pragma mark - Private Methods
  119. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  120. profileData:(nullable GIDProfileData *)profileData {
  121. self = [super init];
  122. if (self) {
  123. [self updateAuthState:authState profileData:profileData];
  124. }
  125. return self;
  126. }
  127. - (void)updateAuthState:(OIDAuthState *)authState
  128. profileData:(nullable GIDProfileData *)profileData {
  129. @synchronized(self) {
  130. _authState = authState;
  131. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  132. _profile = profileData;
  133. // These three tokens will be generated in the getter and cached .
  134. _cachedAccessToken = nil;
  135. _cachedRefreshToken = nil;
  136. _cachedIdToken = nil;
  137. }
  138. }
  139. #pragma mark - Helpers
  140. - (nullable NSString *)hostedDomain {
  141. NSString *idTokenString = self.idToken.tokenString;
  142. if (idTokenString) {
  143. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idTokenString];
  144. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  145. return idTokenDecoded.claims[kHostedDomainIDTokenClaimKey];
  146. }
  147. }
  148. return nil;
  149. }
  150. #pragma mark - NSSecureCoding
  151. + (BOOL)supportsSecureCoding {
  152. return YES;
  153. }
  154. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  155. self = [super init];
  156. if (self) {
  157. GIDProfileData *profileData =
  158. [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  159. OIDAuthState *authState;
  160. if ([decoder containsValueForKey:kAuthState]) { // Current encoding
  161. authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  162. } else { // Old encoding
  163. GIDAuthentication *authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  164. forKey:kAuthenticationKey];
  165. authState = authentication.authState;
  166. }
  167. [self updateAuthState:authState profileData:profileData];
  168. }
  169. return self;
  170. }
  171. - (void)encodeWithCoder:(NSCoder *)encoder {
  172. [encoder encodeObject:_profile forKey:kProfileDataKey];
  173. [encoder encodeObject:_authState forKey:kAuthState];
  174. }
  175. @end
  176. NS_ASSUME_NONNULL_END