GIDGoogleUser.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 kGrantedScopesKey = @"grantedScopes";
  28. static NSString *const kUserIDKey = @"userID";
  29. static NSString *const kServerAuthCodeKey = @"serverAuthCode";
  30. static NSString *const kProfileDataKey = @"profileData";
  31. static NSString *const kHostedDomainKey = @"hostedDomain";
  32. // Parameters for the token exchange endpoint.
  33. static NSString *const kAudienceParameter = @"audience";
  34. static NSString *const kOpenIDRealmParameter = @"openid.realm";
  35. @implementation GIDGoogleUser
  36. - (instancetype)initWithAuthState:(OIDAuthState *)authState
  37. profileData:(nullable GIDProfileData *)profileData {
  38. self = [super init];
  39. if (self) {
  40. _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
  41. NSArray<NSString *> *grantedScopes;
  42. NSString *grantedScopeString = authState.lastTokenResponse.scope;
  43. if (grantedScopeString) {
  44. // If we have a 'scope' parameter from the backend, this is authoritative.
  45. // Remove leading and trailing whitespace.
  46. grantedScopeString = [grantedScopeString stringByTrimmingCharactersInSet:
  47. [NSCharacterSet whitespaceCharacterSet]];
  48. // Tokenize with space as a delimiter.
  49. NSMutableArray<NSString *> *parsedScopes =
  50. [[grantedScopeString componentsSeparatedByString:@" "] mutableCopy];
  51. // Remove empty strings.
  52. [parsedScopes removeObject:@""];
  53. grantedScopes = [parsedScopes copy];
  54. }
  55. _grantedScopes = grantedScopes;
  56. _serverAuthCode = [authState.lastTokenResponse.additionalParameters[@"server_code"] copy];
  57. _profile = [profileData copy];
  58. NSString *idToken = authState.lastTokenResponse.idToken;
  59. if (idToken) {
  60. OIDIDToken *idTokenDecoded = [[OIDIDToken alloc] initWithIDTokenString:idToken];
  61. if (idTokenDecoded.subject) {
  62. _userID = [idTokenDecoded.subject copy];
  63. }
  64. if (idTokenDecoded.claims[kHostedDomainIDTokenClaimKey]) {
  65. _hostedDomain = [idTokenDecoded.claims[kHostedDomainIDTokenClaimKey] copy];
  66. }
  67. }
  68. _serverClientID =
  69. [authState.lastTokenResponse.request.additionalParameters[kAudienceParameter] copy];
  70. _openIDRealm =
  71. [authState.lastTokenResponse.request.additionalParameters[kOpenIDRealmParameter] copy];
  72. }
  73. return self;
  74. }
  75. #pragma mark - NSSecureCoding
  76. + (BOOL)supportsSecureCoding {
  77. return YES;
  78. }
  79. - (nullable instancetype)initWithCoder:(NSCoder *)decoder {
  80. self = [super init];
  81. if (self) {
  82. _authentication = [decoder decodeObjectOfClass:[GIDAuthentication class]
  83. forKey:kAuthenticationKey];
  84. _grantedScopes = [decoder decodeObjectOfClass:[NSArray class] forKey:kGrantedScopesKey];
  85. _userID = [decoder decodeObjectOfClass:[NSString class] forKey:kUserIDKey];
  86. _serverAuthCode = [decoder decodeObjectOfClass:[NSString class] forKey:kServerAuthCodeKey];
  87. _profile = [decoder decodeObjectOfClass:[GIDProfileData class] forKey:kProfileDataKey];
  88. _hostedDomain = [decoder decodeObjectOfClass:[NSString class] forKey:kHostedDomainKey];
  89. }
  90. return self;
  91. }
  92. - (void)encodeWithCoder:(NSCoder *)encoder {
  93. [encoder encodeObject:_authentication forKey:kAuthenticationKey];
  94. [encoder encodeObject:_grantedScopes forKey:kGrantedScopesKey];
  95. [encoder encodeObject:_userID forKey:kUserIDKey];
  96. [encoder encodeObject:_serverAuthCode forKey:kServerAuthCodeKey];
  97. [encoder encodeObject:_profile forKey:kProfileDataKey];
  98. [encoder encodeObject:_hostedDomain forKey:kHostedDomainKey];
  99. }
  100. @end
  101. NS_ASSUME_NONNULL_END