GIDConfiguration.m 3.8 KB

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