OIDServiceConfiguration.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*! @file OIDServiceConfiguration.m
  2. @brief AppAuth iOS SDK
  3. @copyright
  4. Copyright 2015 Google Inc. All Rights Reserved.
  5. @copydetails
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. #import "OIDServiceConfiguration.h"
  17. #import "OIDDefines.h"
  18. #import "OIDErrorUtilities.h"
  19. #import "OIDServiceDiscovery.h"
  20. /*! @brief The key for the @c authorizationEndpoint property.
  21. */
  22. static NSString *const kAuthorizationEndpointKey = @"authorizationEndpoint";
  23. /*! @brief The key for the @c tokenEndpoint property.
  24. */
  25. static NSString *const kTokenEndpointKey = @"tokenEndpoint";
  26. /*! @brief The key for the @c issuer property.
  27. */
  28. static NSString *const kIssuerKey = @"issuer";
  29. /*! @brief The key for the @c registrationEndpoint property.
  30. */
  31. static NSString *const kRegistrationEndpointKey = @"registrationEndpoint";
  32. /*! @brief The key for the @c endSessionEndpoint property.
  33. */
  34. static NSString *const kEndSessionEndpointKey = @"endSessionEndpoint";
  35. /*! @brief The key for the @c discoveryDocument property.
  36. */
  37. static NSString *const kDiscoveryDocumentKey = @"discoveryDocument";
  38. NS_ASSUME_NONNULL_BEGIN
  39. @interface OIDServiceConfiguration ()
  40. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  41. tokenEndpoint:(NSURL *)tokenEndpoint
  42. issuer:(nullable NSURL *)issuer
  43. registrationEndpoint:(nullable NSURL *)registrationEndpoint
  44. endSessionEndpoint:(nullable NSURL *)endSessionEndpoint
  45. discoveryDocument:(nullable OIDServiceDiscovery *)discoveryDocument
  46. NS_DESIGNATED_INITIALIZER;
  47. @end
  48. @implementation OIDServiceConfiguration
  49. - (instancetype)init
  50. OID_UNAVAILABLE_USE_INITIALIZER(@selector(
  51. initWithAuthorizationEndpoint:
  52. tokenEndpoint:)
  53. )
  54. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  55. tokenEndpoint:(NSURL *)tokenEndpoint
  56. issuer:(nullable NSURL *)issuer
  57. registrationEndpoint:(nullable NSURL *)registrationEndpoint
  58. endSessionEndpoint:(nullable OIDServiceDiscovery *)endSessionEndpoint
  59. discoveryDocument:(nullable OIDServiceDiscovery *)discoveryDocument {
  60. self = [super init];
  61. if (self) {
  62. _authorizationEndpoint = [authorizationEndpoint copy];
  63. _tokenEndpoint = [tokenEndpoint copy];
  64. _issuer = [issuer copy];
  65. _registrationEndpoint = [registrationEndpoint copy];
  66. _endSessionEndpoint = [endSessionEndpoint copy];
  67. _discoveryDocument = [discoveryDocument copy];
  68. }
  69. return self;
  70. }
  71. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  72. tokenEndpoint:(NSURL *)tokenEndpoint {
  73. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  74. tokenEndpoint:tokenEndpoint
  75. issuer:nil
  76. registrationEndpoint:nil
  77. endSessionEndpoint:nil
  78. discoveryDocument:nil];
  79. }
  80. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  81. tokenEndpoint:(NSURL *)tokenEndpoint
  82. registrationEndpoint:(nullable NSURL *)registrationEndpoint {
  83. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  84. tokenEndpoint:tokenEndpoint
  85. issuer:nil
  86. registrationEndpoint:registrationEndpoint
  87. endSessionEndpoint:nil
  88. discoveryDocument:nil];
  89. }
  90. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  91. tokenEndpoint:(NSURL *)tokenEndpoint
  92. issuer:(nullable NSURL *)issuer {
  93. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  94. tokenEndpoint:tokenEndpoint
  95. issuer:issuer
  96. registrationEndpoint:nil
  97. endSessionEndpoint:nil
  98. discoveryDocument:nil];
  99. }
  100. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  101. tokenEndpoint:(NSURL *)tokenEndpoint
  102. issuer:(nullable NSURL *)issuer
  103. registrationEndpoint:(nullable NSURL *)registrationEndpoint {
  104. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  105. tokenEndpoint:tokenEndpoint
  106. issuer:issuer
  107. registrationEndpoint:registrationEndpoint
  108. endSessionEndpoint:nil
  109. discoveryDocument:nil];
  110. }
  111. - (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
  112. tokenEndpoint:(NSURL *)tokenEndpoint
  113. issuer:(nullable NSURL *)issuer
  114. registrationEndpoint:(nullable NSURL *)registrationEndpoint
  115. endSessionEndpoint:(nullable NSURL *)endSessionEndpoint {
  116. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  117. tokenEndpoint:tokenEndpoint
  118. issuer:issuer
  119. registrationEndpoint:registrationEndpoint
  120. endSessionEndpoint:endSessionEndpoint
  121. discoveryDocument:nil];
  122. }
  123. - (instancetype)initWithDiscoveryDocument:(OIDServiceDiscovery *) discoveryDocument {
  124. return [self initWithAuthorizationEndpoint:discoveryDocument.authorizationEndpoint
  125. tokenEndpoint:discoveryDocument.tokenEndpoint
  126. issuer:discoveryDocument.issuer
  127. registrationEndpoint:discoveryDocument.registrationEndpoint
  128. endSessionEndpoint:discoveryDocument.endSessionEndpoint
  129. discoveryDocument:discoveryDocument];
  130. }
  131. #pragma mark - NSCopying
  132. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  133. // The documentation for NSCopying specifically advises us to return a reference to the original
  134. // instance in the case where instances are immutable (as ours is):
  135. // "Implement NSCopying by retaining the original instead of creating a new copy when the class
  136. // and its contents are immutable."
  137. return self;
  138. }
  139. #pragma mark - NSSecureCoding
  140. + (BOOL)supportsSecureCoding {
  141. return YES;
  142. }
  143. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  144. NSURL *authorizationEndpoint = [aDecoder decodeObjectOfClass:[NSURL class]
  145. forKey:kAuthorizationEndpointKey];
  146. NSURL *tokenEndpoint = [aDecoder decodeObjectOfClass:[NSURL class]
  147. forKey:kTokenEndpointKey];
  148. NSURL *issuer = [aDecoder decodeObjectOfClass:[NSURL class]
  149. forKey:kIssuerKey];
  150. NSURL *registrationEndpoint = [aDecoder decodeObjectOfClass:[NSURL class]
  151. forKey:kRegistrationEndpointKey];
  152. NSURL *endSessionEndpoint = [aDecoder decodeObjectOfClass:[NSURL class]
  153. forKey:kEndSessionEndpointKey];
  154. // We don't accept nil authorizationEndpoints or tokenEndpoints.
  155. if (!authorizationEndpoint || !tokenEndpoint) {
  156. return nil;
  157. }
  158. OIDServiceDiscovery *discoveryDocument = [aDecoder decodeObjectOfClass:[OIDServiceDiscovery class]
  159. forKey:kDiscoveryDocumentKey];
  160. return [self initWithAuthorizationEndpoint:authorizationEndpoint
  161. tokenEndpoint:tokenEndpoint
  162. issuer:issuer
  163. registrationEndpoint:registrationEndpoint
  164. endSessionEndpoint:endSessionEndpoint
  165. discoveryDocument:discoveryDocument];
  166. }
  167. - (void)encodeWithCoder:(NSCoder *)aCoder {
  168. [aCoder encodeObject:_authorizationEndpoint forKey:kAuthorizationEndpointKey];
  169. [aCoder encodeObject:_tokenEndpoint forKey:kTokenEndpointKey];
  170. [aCoder encodeObject:_issuer forKey:kIssuerKey];
  171. [aCoder encodeObject:_registrationEndpoint forKey:kRegistrationEndpointKey];
  172. [aCoder encodeObject:_discoveryDocument forKey:kDiscoveryDocumentKey];
  173. [aCoder encodeObject:_endSessionEndpoint forKey:kEndSessionEndpointKey];
  174. }
  175. #pragma mark - description
  176. - (NSString *)description {
  177. return [NSString stringWithFormat:
  178. @"OIDServiceConfiguration authorizationEndpoint: %@, tokenEndpoint: %@, "
  179. "registrationEndpoint: %@, endSessionEndpoint: %@, discoveryDocument: [%@]",
  180. _authorizationEndpoint,
  181. _tokenEndpoint,
  182. _registrationEndpoint,
  183. _endSessionEndpoint,
  184. _discoveryDocument];
  185. }
  186. @end
  187. NS_ASSUME_NONNULL_END