GIDGoogleUser.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }
  39. @synthesize accessToken = _accessToken;
  40. @synthesize refreshToken = _refreshToken;
  41. @synthesize idToken = _idToken;
  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. #pragma mark - Private Methods
  88. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  89. profileData:(nullable GIDProfileData *)profileData {
  90. self = [super init];
  91. if (self) {
  92. [self updateAuthState:authState profileData:profileData];
  93. }
  94. return self;
  95. }
  96. - (void)updateAuthState:(OIDAuthState *)authState
  97. profileData:(nullable GIDProfileData *)profileData {
  98. @synchronized(self) {
  99. _authState = authState;
  100. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  101. _profile = profileData;
  102. [self sendKVONotificationsBeforeChanges];
  103. [self updateTokensWithAuthState:authState];
  104. [self sendKVONotificationAfterChanges];
  105. }
  106. }
  107. - (void)updateTokensWithAuthState:(OIDAuthState *)authState {
  108. _accessToken = [[GIDToken alloc] initWithTokenString:authState.lastTokenResponse.accessToken
  109. expirationDate:authState.lastTokenResponse.
  110. accessTokenExpirationDate];
  111. _refreshToken = [[GIDToken alloc] initWithTokenString:authState.refreshToken
  112. expirationDate:nil];
  113. _idToken = nil;
  114. NSString *idTokenString = authState.lastTokenResponse.idToken;
  115. if (idTokenString) {
  116. NSDate *idTokenExpirationDate = [[[OIDIDToken alloc]
  117. initWithIDTokenString:idTokenString] expiresAt];
  118. _idToken = [[GIDToken alloc] initWithTokenString:idTokenString
  119. expirationDate:idTokenExpirationDate];
  120. }
  121. }
  122. #pragma mark - Helpers
  123. - (nullable NSString *)hostedDomain {
  124. NSString *idTokenString = self.idToken.tokenString;
  125. if (idTokenString) {
  126. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idTokenString];
  127. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  128. return idTokenDecoded.claims[kHostedDomainIDTokenClaimKey];
  129. }
  130. }
  131. return nil;
  132. }
  133. - (void)sendKVONotificationsBeforeChanges {
  134. [self willChangeValueForKey:NSStringFromSelector(@selector(accessToken))];
  135. [self willChangeValueForKey:NSStringFromSelector(@selector(refreshToken))];
  136. [self willChangeValueForKey:NSStringFromSelector(@selector(idToken))];
  137. }
  138. - (void)sendKVONotificationAfterChanges {
  139. [self didChangeValueForKey:NSStringFromSelector(@selector(accessToken))];
  140. [self didChangeValueForKey:NSStringFromSelector(@selector(refreshToken))];
  141. [self didChangeValueForKey:NSStringFromSelector(@selector(idToken))];
  142. }
  143. #pragma mark - NSSecureCoding
  144. + (BOOL)supportsSecureCoding {
  145. return YES;
  146. }
  147. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  148. self = [super init];
  149. if (self) {
  150. GIDProfileData *profileData =
  151. [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  152. OIDAuthState *authState;
  153. if ([decoder containsValueForKey:kAuthState]) { // Current encoding
  154. authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  155. } else { // Old encoding
  156. GIDAuthentication *authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  157. forKey:kAuthenticationKey];
  158. authState = authentication.authState;
  159. }
  160. [self updateAuthState:authState profileData:profileData];
  161. }
  162. return self;
  163. }
  164. - (void)encodeWithCoder:(NSCoder *)encoder {
  165. [encoder encodeObject:_profile forKey:kProfileDataKey];
  166. [encoder encodeObject:_authState forKey:kAuthState];
  167. }
  168. @end
  169. NS_ASSUME_NONNULL_END