GIDGoogleUser.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  36. profileData:(nullable GIDProfileData *)profileData {
  37. self = [super init];
  38. if (self) {
  39. [self updateAuthState:authState profileData:profileData];
  40. }
  41. return self;
  42. }
  43. - (nullable NSString *)userID {
  44. NSString *idToken = [self idToken];
  45. if (idToken) {
  46. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  47. if (idTokenDecoded && idTokenDecoded.subject) {
  48. return [idTokenDecoded.subject copy];
  49. }
  50. }
  51. return nil;
  52. }
  53. - (nullable NSString *)hostedDomain {
  54. NSString *idToken = [self idToken];
  55. if (idToken) {
  56. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  57. if (idTokenDecoded && idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  58. return [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
  59. }
  60. }
  61. return nil;
  62. }
  63. - (nullable NSString *)serverAuthCode {
  64. return [_authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
  65. }
  66. - (nullable NSString *)serverClientID {
  67. return [_authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
  68. }
  69. - (nullable NSString *)openIDRealm {
  70. return [_authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
  71. }
  72. - (nullable NSArray<NSString *> *)grantedScopes {
  73. NSArray<NSString *> *grantedScopes;
  74. NSString *grantedScopeString = _authState.lastTokenResponse.scope;
  75. if (grantedScopeString) {
  76. // If we have a 'scope' parameter from the backend, this is authoritative.
  77. // Remove leading and trailing whitespace.
  78. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  79. [NSCharacterSet whitespaceCharacterSet]];
  80. // Tokenize with space as a delimiter.
  81. NSMutableArray<NSString *> *parsedScopes =
  82. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  83. // Remove empty strings.
  84. [parsedScopes removeObject:@""];
  85. grantedScopes = [parsedScopes copy];
  86. }
  87. return grantedScopes;
  88. }
  89. #pragma mark - Private Methods
  90. - (void)updateAuthState:(OIDAuthState *)authState
  91. profileData:(nullable GIDProfileData *)profileData {
  92. _authState = authState;
  93. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  94. _profile = profileData;
  95. }
  96. - (NSString *)idToken {
  97. return _authState ? _authState.lastTokenResponse.idToken : nil;
  98. }
  99. #pragma mark - NSSecureCoding
  100. + (BOOL)supportsSecureCoding {
  101. return YES;
  102. }
  103. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  104. self = [super init];
  105. if (self) {
  106. _authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  107. forKey:kAuthenticationKey];
  108. _profile = [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  109. _authState = [decoder decodeObjectOfClass:[OIDAuthState class] forKey:kAuthState];
  110. }
  111. return self;
  112. }
  113. - (void)encodeWithCoder:(NSCoder *)encoder {
  114. [encoder encodeObject:_authentication forKey:kAuthenticationKey];
  115. [encoder encodeObject:_profile forKey:kProfileDataKey];
  116. [encoder encodeObject:_authState forKey:kAuthState];
  117. }
  118. @end
  119. NS_ASSUME_NONNULL_END