GIDConfiguration.m 5.4 KB

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