OIDRegistrationResponse.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*! @file OIDRegistrationResponse.m
  2. @brief AppAuth iOS SDK
  3. @copyright
  4. Copyright 2016 The AppAuth for iOS Authors. 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 "OIDRegistrationResponse.h"
  17. #import "OIDClientMetadataParameters.h"
  18. #import "OIDDefines.h"
  19. #import "OIDFieldMapping.h"
  20. #import "OIDRegistrationRequest.h"
  21. #import "OIDTokenUtilities.h"
  22. NSString *const OIDClientIDParam = @"client_id";
  23. NSString *const OIDClientIDIssuedAtParam = @"client_id_issued_at";
  24. NSString *const OIDClientSecretParam = @"client_secret";
  25. NSString *const OIDClientSecretExpirestAtParam = @"client_secret_expires_at";
  26. NSString *const OIDRegistrationAccessTokenParam = @"registration_access_token";
  27. NSString *const OIDRegistrationClientURIParam = @"registration_client_uri";
  28. /*! @brief Key used to encode the @c request property for @c NSSecureCoding
  29. */
  30. static NSString *const kRequestKey = @"request";
  31. /*! @brief Key used to encode the @c additionalParameters property for @c NSSecureCoding
  32. */
  33. static NSString *const kAdditionalParametersKey = @"additionalParameters";
  34. @implementation OIDRegistrationResponse
  35. /*! @brief Returns a mapping of incoming parameters to instance variables.
  36. @return A mapping of incoming parameters to instance variables.
  37. */
  38. + (NSDictionary<NSString *, OIDFieldMapping *> *)fieldMap {
  39. static NSMutableDictionary<NSString *, OIDFieldMapping *> *fieldMap;
  40. static dispatch_once_t onceToken;
  41. dispatch_once(&onceToken, ^{
  42. fieldMap = [NSMutableDictionary dictionary];
  43. fieldMap[OIDClientIDParam] = [[OIDFieldMapping alloc] initWithName:@"_clientID"
  44. type:[NSString class]];
  45. fieldMap[OIDClientIDIssuedAtParam] =
  46. [[OIDFieldMapping alloc] initWithName:@"_clientIDIssuedAt"
  47. type:[NSDate class]
  48. conversion:[OIDFieldMapping dateEpochConversion]];
  49. fieldMap[OIDClientSecretParam] =
  50. [[OIDFieldMapping alloc] initWithName:@"_clientSecret"
  51. type:[NSString class]];
  52. fieldMap[OIDClientSecretExpirestAtParam] =
  53. [[OIDFieldMapping alloc] initWithName:@"_clientSecretExpiresAt"
  54. type:[NSDate class]
  55. conversion:[OIDFieldMapping dateEpochConversion]];
  56. fieldMap[OIDRegistrationAccessTokenParam] =
  57. [[OIDFieldMapping alloc] initWithName:@"_registrationAccessToken"
  58. type:[NSString class]];
  59. fieldMap[OIDRegistrationClientURIParam] =
  60. [[OIDFieldMapping alloc] initWithName:@"_registrationClientURI"
  61. type:[NSURL class]
  62. conversion:[OIDFieldMapping URLConversion]];
  63. fieldMap[OIDTokenEndpointAuthenticationMethodParam] =
  64. [[OIDFieldMapping alloc] initWithName:@"_tokenEndpointAuthenticationMethod"
  65. type:[NSString class]];
  66. });
  67. return fieldMap;
  68. }
  69. #pragma mark - Initializers
  70. - (nonnull instancetype)init
  71. OID_UNAVAILABLE_USE_INITIALIZER(@selector(initWithRequest:parameters:))
  72. - (instancetype)initWithRequest:(OIDRegistrationRequest *)request
  73. parameters:(NSDictionary<NSString *, NSObject <NSCopying> *> *)parameters {
  74. self = [super init];
  75. if (self) {
  76. _request = [request copy];
  77. NSDictionary<NSString *, NSObject <NSCopying> *> *additionalParameters =
  78. [OIDFieldMapping remainingParametersWithMap:[[self class] fieldMap]
  79. parameters:parameters
  80. instance:self];
  81. _additionalParameters = additionalParameters;
  82. if ((_clientSecret && !_clientSecretExpiresAt)
  83. || (!!_registrationClientURI != !!_registrationAccessToken)) {
  84. // If client_secret is issued, client_secret_expires_at is REQUIRED,
  85. // and the response MUST contain "[...] both a Client Configuration Endpoint
  86. // and a Registration Access Token or neither of them"
  87. return nil;
  88. }
  89. }
  90. return self;
  91. }
  92. #pragma mark - NSCopying
  93. - (instancetype)copyWithZone:(nullable NSZone *)zone {
  94. // The documentation for NSCopying specifically advises us to return a reference to the original
  95. // instance in the case where instances are immutable (as ours is):
  96. // "Implement NSCopying by retaining the original instead of creating a new copy when the class
  97. // and its contents are immutable."
  98. return self;
  99. }
  100. #pragma mark - NSSecureCoding
  101. + (BOOL)supportsSecureCoding {
  102. return YES;
  103. }
  104. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
  105. OIDRegistrationRequest *request = [aDecoder decodeObjectOfClass:[OIDRegistrationRequest class]
  106. forKey:kRequestKey];
  107. self = [self initWithRequest:request
  108. parameters:@{}];
  109. if (self) {
  110. [OIDFieldMapping decodeWithCoder:aDecoder
  111. map:[[self class] fieldMap]
  112. instance:self];
  113. _additionalParameters = [aDecoder decodeObjectOfClasses:[OIDFieldMapping JSONTypes]
  114. forKey:kAdditionalParametersKey];
  115. }
  116. return self;
  117. }
  118. - (void)encodeWithCoder:(NSCoder *)aCoder {
  119. [OIDFieldMapping encodeWithCoder:aCoder map:[[self class] fieldMap] instance:self];
  120. [aCoder encodeObject:_request forKey:kRequestKey];
  121. [aCoder encodeObject:_additionalParameters forKey:kAdditionalParametersKey];
  122. }
  123. #pragma mark - NSObject overrides
  124. - (NSString *)description {
  125. return [NSString stringWithFormat:@"<%@: %p, clientID: \"%@\", clientIDIssuedAt: %@, "
  126. "clientSecret: %@, clientSecretExpiresAt: \"%@\", "
  127. "registrationAccessToken: \"%@\", "
  128. "registrationClientURI: \"%@\", "
  129. "additionalParameters: %@, request: %@>",
  130. NSStringFromClass([self class]),
  131. (void *)self,
  132. _clientID,
  133. _clientIDIssuedAt,
  134. [OIDTokenUtilities redact:_clientSecret],
  135. _clientSecretExpiresAt,
  136. [OIDTokenUtilities redact:_registrationAccessToken],
  137. _registrationClientURI,
  138. _additionalParameters,
  139. _request];
  140. }
  141. @end