GIDGoogleUser.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/GIDGoogleUser_Private.h"
  15. #import "GoogleSignIn/Sources/GIDAuthentication_Private.h"
  16. #import "GoogleSignIn/Sources/GIDProfileData_Private.h"
  17. #ifdef SWIFT_PACKAGE
  18. @import AppAuth;
  19. #else
  20. #import <AppAuth/AppAuth.h>
  21. #endif
  22. NS_ASSUME_NONNULL_BEGIN
  23. // The ID Token claim key for the hosted domain value.
  24. static NSString *const kHostedDomainIDTokenClaimKey = @"hd";
  25. // Key constants used for encode and decode.
  26. static NSString *const kAuthenticationKey = @"authentication";
  27. static NSString *const kProfileDataKey = @"profileData";
  28. static NSString *const kAuthState = @"authState";
  29. // Parameters for the token exchange endpoint.
  30. static NSString *const kAudienceParameter = @"audience";
  31. static NSString *const kOpenIDRealmParameter = @"openid.realm";
  32. @implementation GIDGoogleUser {
  33. OIDAuthState *_authState;
  34. }
  35. - (nullable NSString *)userID {
  36. NSString *idToken = [self idToken];
  37. if (idToken) {
  38. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  39. if (idTokenDecoded && idTokenDecoded.subject) {
  40. return [idTokenDecoded.subject copy];
  41. }
  42. }
  43. return nil;
  44. }
  45. - (nullable NSString *)hostedDomain {
  46. NSString *idToken = [self idToken];
  47. if (idToken) {
  48. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  49. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  50. return [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
  51. }
  52. }
  53. return nil;
  54. }
  55. - (nullable NSString *)serverAuthCode {
  56. return [_authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
  57. }
  58. - (nullable NSString *)serverClientID {
  59. return [_authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
  60. }
  61. - (nullable NSString *)openIDRealm {
  62. return [_authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
  63. }
  64. - (nullable NSArray<NSString *> *)grantedScopes {
  65. NSArray<NSString *> *grantedScopes;
  66. NSString *grantedScopeString = _authState.lastTokenResponse.scope;
  67. if (grantedScopeString) {
  68. // If we have a 'scope' parameter from the backend, this is authoritative.
  69. // Remove leading and trailing whitespace.
  70. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  71. [NSCharacterSet whitespaceCharacterSet]];
  72. // Tokenize with space as a delimiter.
  73. NSMutableArray<NSString *> *parsedScopes =
  74. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  75. // Remove empty strings.
  76. [parsedScopes removeObject:@""];
  77. grantedScopes = [parsedScopes copy];
  78. }
  79. return grantedScopes;
  80. }
  81. #pragma mark - Private Methods
  82. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  83. profileData:(nullable GIDProfileData *)profileData {
  84. self = [super init];
  85. if (self) {
  86. [self updateAuthState:authState profileData:profileData];
  87. }
  88. return self;
  89. }
  90. - (void)updateAuthState:(OIDAuthState *)authState
  91. profileData:(nullable GIDProfileData *)profileData {
  92. _authState = authState;
  93. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  94. _profile = profileData;
  95. }
  96. #pragma mark - Helpers
  97. - (NSString *)idToken {
  98. return _authState ? _authState.lastTokenResponse.idToken : nil;
  99. }
  100. #pragma mark - NSSecureCoding
  101. + (BOOL)supportsSecureCoding {
  102. return YES;
  103. }
  104. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  105. self = [super init];
  106. if (self) {
  107. _profile = [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  108. if ([decoder containsValueForKey:kAuthState]) { // Current encoding
  109. _authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  110. } else { // Old encoding
  111. GIDAuthentication *authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  112. forKey:kAuthenticationKey];
  113. _authState = authentication.authState;
  114. }
  115. _authentication = [[GIDAuthentication alloc] initWithAuthState:_authState];
  116. }
  117. return self;
  118. }
  119. - (void)encodeWithCoder:(NSCoder *)encoder {
  120. [encoder encodeObject:_profile forKey:kProfileDataKey];
  121. [encoder encodeObject:_authState forKey:kAuthState];
  122. }
  123. @end
  124. NS_ASSUME_NONNULL_END