GIDGoogleUser.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/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. #ifdef SWIFT_PACKAGE
  20. @import AppAuth;
  21. #else
  22. #import <AppAuth/AppAuth.h>
  23. #endif
  24. NS_ASSUME_NONNULL_BEGIN
  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. @implementation GIDGoogleUser {
  35. OIDAuthState *_authState;
  36. GIDConfiguration *_cachedConfiguration;
  37. }
  38. - (nullable NSString *)userID {
  39. NSString *idToken = [self idToken];
  40. if (idToken) {
  41. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  42. if (idTokenDecoded && idTokenDecoded.subject) {
  43. return [idTokenDecoded.subject copy];
  44. }
  45. }
  46. return nil;
  47. }
  48. - (nullable NSArray<NSString *> *)grantedScopes {
  49. NSArray<NSString *> *grantedScopes;
  50. NSString *grantedScopeString = _authState.lastTokenResponse.scope;
  51. if (grantedScopeString) {
  52. // If we have a 'scope' parameter from the backend, this is authoritative.
  53. // Remove leading and trailing whitespace.
  54. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  55. [NSCharacterSet whitespaceCharacterSet]];
  56. // Tokenize with space as a delimiter.
  57. NSMutableArray<NSString *> *parsedScopes =
  58. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  59. // Remove empty strings.
  60. [parsedScopes removeObject:@""];
  61. grantedScopes = [parsedScopes copy];
  62. }
  63. return grantedScopes;
  64. }
  65. - (GIDConfiguration *)configuration {
  66. @synchronized(self) {
  67. // Caches the configuration since it would not change for one GIDGoogleUser instance.
  68. if (!_cachedConfiguration) {
  69. _cachedConfiguration = [[GIDConfiguration alloc] initWithClientID:[self clientID]
  70. serverClientID:[self serverClientID]
  71. hostedDomain:[self hostedDomain]
  72. openIDRealm:[self openIDRealm]];
  73. };
  74. }
  75. return _cachedConfiguration;
  76. }
  77. #pragma mark - Private Methods
  78. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  79. profileData:(nullable GIDProfileData *)profileData {
  80. self = [super init];
  81. if (self) {
  82. [self updateAuthState:authState profileData:profileData];
  83. }
  84. return self;
  85. }
  86. - (void)updateAuthState:(OIDAuthState *)authState
  87. profileData:(nullable GIDProfileData *)profileData {
  88. _authState = authState;
  89. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  90. _profile = profileData;
  91. }
  92. #pragma mark - Helpers
  93. - (NSString *)clientID {
  94. return _authState.lastAuthorizationResponse.request.clientID;
  95. }
  96. - (nullable NSString *)hostedDomain {
  97. NSString *idToken = [self idToken];
  98. if (idToken) {
  99. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  100. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  101. return [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
  102. }
  103. }
  104. return nil;
  105. }
  106. - (NSString *)idToken {
  107. return _authState ? _authState.lastTokenResponse.idToken : nil;
  108. }
  109. - (nullable NSString *)serverClientID {
  110. return [_authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
  111. }
  112. - (nullable NSString *)openIDRealm {
  113. return [_authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
  114. }
  115. #pragma mark - NSSecureCoding
  116. + (BOOL)supportsSecureCoding {
  117. return YES;
  118. }
  119. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  120. self = [super init];
  121. if (self) {
  122. _profile = [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  123. if ([decoder containsValueForKey:kAuthState]) { // Current encoding
  124. _authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  125. } else { // Old encoding
  126. GIDAuthentication *authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  127. forKey:kAuthenticationKey];
  128. _authState = authentication.authState;
  129. }
  130. _authentication = [[GIDAuthentication alloc] initWithAuthState:_authState];
  131. }
  132. return self;
  133. }
  134. - (void)encodeWithCoder:(NSCoder *)encoder {
  135. [encoder encodeObject:_profile forKey:kProfileDataKey];
  136. [encoder encodeObject:_authState forKey:kAuthState];
  137. }
  138. @end
  139. NS_ASSUME_NONNULL_END