GIDConfiguration.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/GIDConfiguration.h"
  15. // The key for the clientID property to be used with NSSecureCoding.
  16. static NSString *const kClientIDKey = @"clientID";
  17. // The key for the serverClientID property to be used with NSSecureCoding.
  18. static NSString *const kServerClientIDKey = @"serverClientID";
  19. // The key for the loginHint property to be used with NSSecureCoding.
  20. static NSString *const kLoginHintKey = @"loginHint";
  21. // The key for the hostedDomain property to be used with NSSecureCoding.
  22. static NSString *const kHostedDomainKey = @"hostedDomain";
  23. // The key for the openIDRealm property to be used with NSSecureCoding.
  24. static NSString *const kOpenIDRealmKey = @"openIDRealm";
  25. NS_ASSUME_NONNULL_BEGIN
  26. @implementation GIDConfiguration
  27. - (instancetype)initWithClientID:(NSString *)clientID {
  28. return [self initWithClientID:clientID
  29. serverClientID:nil
  30. loginHint:nil
  31. hostedDomain:nil
  32. openIDRealm:nil];
  33. }
  34. - (instancetype)initWithClientID:(NSString *)clientID
  35. serverClientID:(nullable NSString *)serverClientID {
  36. return [self initWithClientID:clientID
  37. serverClientID:serverClientID
  38. loginHint:nil
  39. hostedDomain:nil
  40. openIDRealm:nil];
  41. }
  42. - (instancetype)initWithClientID:(NSString *)clientID
  43. serverClientID:(nullable NSString *)serverClientID
  44. loginHint:(nullable NSString *)loginHint
  45. hostedDomain:(nullable NSString *)hostedDomain
  46. openIDRealm:(nullable NSString *)openIDRealm {
  47. self = [super init];
  48. if (self) {
  49. _clientID = [clientID copy];
  50. _serverClientID = [serverClientID copy];
  51. _loginHint = [loginHint copy];
  52. _hostedDomain = [hostedDomain copy];
  53. _openIDRealm = [openIDRealm copy];
  54. }
  55. return self;
  56. }
  57. // Extend NSObject's default description for easier debugging.
  58. - (NSString *)description {
  59. return [NSString stringWithFormat:
  60. @"<%@: %p, clientID: %@, serverClientID: %@, loginHint: %@, hostedDomain: %@, "
  61. "openIDRealm: %@>",
  62. NSStringFromClass([self class]),
  63. self,
  64. _clientID,
  65. _serverClientID,
  66. _loginHint,
  67. _hostedDomain,
  68. _openIDRealm];
  69. }
  70. #pragma mark - NSCopying
  71. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  72. // Instances of this class are immutable so return a reference to the original per NSCopying docs.
  73. return self;
  74. }
  75. #pragma mark - NSSecureCoding
  76. + (BOOL)supportsSecureCoding {
  77. return YES;
  78. }
  79. - (nullable instancetype)initWithCoder:(NSCoder *)coder {
  80. NSString *clientID = [coder decodeObjectOfClass:[NSString class] forKey:kClientIDKey];
  81. NSString *serverClientID = [coder decodeObjectOfClass:[NSString class] forKey:kServerClientIDKey];
  82. NSString *loginHint = [coder decodeObjectOfClass:[NSString class] forKey:kLoginHintKey];
  83. NSString *hostedDomain = [coder decodeObjectOfClass:[NSString class] forKey:kHostedDomainKey];
  84. NSString *openIDRealm = [coder decodeObjectOfClass:[NSString class] forKey:kOpenIDRealmKey];
  85. // We must have a client ID.
  86. if (!clientID) {
  87. return nil;
  88. }
  89. return [self initWithClientID:clientID
  90. serverClientID:serverClientID
  91. loginHint:loginHint
  92. hostedDomain:hostedDomain
  93. openIDRealm:openIDRealm];
  94. }
  95. - (void)encodeWithCoder:(NSCoder *)coder {
  96. [coder encodeObject:_clientID forKey:kClientIDKey];
  97. [coder encodeObject:_serverClientID forKey:kServerClientIDKey];
  98. [coder encodeObject:_loginHint forKey:kLoginHintKey];
  99. [coder encodeObject:_hostedDomain forKey:kHostedDomainKey];
  100. [coder encodeObject:_openIDRealm forKey:kOpenIDRealmKey];
  101. }
  102. @end
  103. NS_ASSUME_NONNULL_END