GIDConfiguration.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Info.plist config keys
  24. static NSString *const kConfigClientIDKey = @"GIDClientID";
  25. static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
  26. static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
  27. static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
  28. NS_ASSUME_NONNULL_BEGIN
  29. @implementation GIDConfiguration
  30. - (instancetype)initWithClientID:(NSString *)clientID {
  31. return [self initWithClientID:clientID
  32. serverClientID:nil
  33. hostedDomain:nil
  34. openIDRealm:nil];
  35. }
  36. - (instancetype)initWithClientID:(NSString *)clientID
  37. serverClientID:(nullable NSString *)serverClientID {
  38. return [self initWithClientID:clientID
  39. serverClientID:serverClientID
  40. hostedDomain:nil
  41. openIDRealm:nil];
  42. }
  43. - (instancetype)initWithClientID:(NSString *)clientID
  44. serverClientID:(nullable NSString *)serverClientID
  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. _hostedDomain = [hostedDomain copy];
  52. _openIDRealm = [openIDRealm copy];
  53. }
  54. return self;
  55. }
  56. // Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
  57. + (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
  58. NSString *value;
  59. id configValue = [bundle objectForInfoDictionaryKey:key];
  60. if ([configValue isKindOfClass:[NSString class]]) {
  61. value = configValue;
  62. }
  63. return value;
  64. }
  65. + (nullable instancetype)configurationFromBundle:(NSBundle *)bundle {
  66. // Retrieve any valid config parameters from the bundle's Info.plist.
  67. NSString *clientID = [self configValueFromBundle:bundle forKey:kConfigClientIDKey];
  68. NSString *serverClientID = [self configValueFromBundle:bundle
  69. forKey:kConfigServerClientIDKey];
  70. NSString *hostedDomain = [self configValueFromBundle:bundle forKey:kConfigHostedDomainKey];
  71. NSString *openIDRealm = [self configValueFromBundle:bundle forKey:kConfigOpenIDRealmKey];
  72. // If we have at least a client ID, try to construct a configuration.
  73. if (clientID) {
  74. return [[self alloc] initWithClientID:clientID
  75. serverClientID:serverClientID
  76. hostedDomain:hostedDomain
  77. openIDRealm:openIDRealm];
  78. }
  79. return nil;
  80. }
  81. // Extend NSObject's default description for easier debugging.
  82. - (NSString *)description {
  83. return [NSString stringWithFormat:
  84. @"<%@: %p, clientID: %@, serverClientID: %@, hostedDomain: %@, openIDRealm: %@>",
  85. NSStringFromClass([self class]),
  86. self,
  87. _clientID,
  88. _serverClientID,
  89. _hostedDomain,
  90. _openIDRealm];
  91. }
  92. #pragma mark - NSCopying
  93. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  94. // Instances of this class are immutable so return a reference to the original per NSCopying docs.
  95. return self;
  96. }
  97. #pragma mark - NSSecureCoding
  98. + (BOOL)supportsSecureCoding {
  99. return YES;
  100. }
  101. - (nullable instancetype)initWithCoder:(NSCoder *)coder {
  102. NSString *clientID = [coder decodeObjectOfClass:[NSString class] forKey:kClientIDKey];
  103. NSString *serverClientID = [coder decodeObjectOfClass:[NSString class] forKey:kServerClientIDKey];
  104. NSString *hostedDomain = [coder decodeObjectOfClass:[NSString class] forKey:kHostedDomainKey];
  105. NSString *openIDRealm = [coder decodeObjectOfClass:[NSString class] forKey:kOpenIDRealmKey];
  106. // We must have a client ID.
  107. if (!clientID) {
  108. return nil;
  109. }
  110. return [self initWithClientID:clientID
  111. serverClientID:serverClientID
  112. hostedDomain:hostedDomain
  113. openIDRealm:openIDRealm];
  114. }
  115. - (void)encodeWithCoder:(NSCoder *)coder {
  116. [coder encodeObject:_clientID forKey:kClientIDKey];
  117. [coder encodeObject:_serverClientID forKey:kServerClientIDKey];
  118. [coder encodeObject:_hostedDomain forKey:kHostedDomainKey];
  119. [coder encodeObject:_openIDRealm forKey:kOpenIDRealmKey];
  120. }
  121. @end
  122. NS_ASSUME_NONNULL_END