GIDGoogleUser.m 4.1 KB

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