-
Notifications
You must be signed in to change notification settings - Fork 843
Expand file tree
/
Copy pathOIDServiceDiscovery.m
More file actions
403 lines (335 loc) · 15.3 KB
/
OIDServiceDiscovery.m
File metadata and controls
403 lines (335 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*! @file OIDServiceDiscovery.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "OIDServiceDiscovery.h"
#import "OIDDefines.h"
#import "OIDErrorUtilities.h"
NS_ASSUME_NONNULL_BEGIN
/*! @brief The key for the @c discoveryDictionary property.
*/
static NSString *const kDiscoveryDictionaryKey = @"discoveryDictionary";
/*! Field keys associated with an OpenID Connect Discovery Document. */
static NSString *const kIssuerKey = @"issuer";
static NSString *const kAuthorizationEndpointKey = @"authorization_endpoint";
static NSString *const kDeviceAuthorizationEndpointKey = @"device_authorization_endpoint";
static NSString *const kTokenEndpointKey = @"token_endpoint";
static NSString *const kUserinfoEndpointKey = @"userinfo_endpoint";
static NSString *const kJWKSURLKey = @"jwks_uri";
static NSString *const kRegistrationEndpointKey = @"registration_endpoint";
static NSString *const kEndSessionEndpointKey = @"end_session_endpoint";
static NSString *const kScopesSupportedKey = @"scopes_supported";
static NSString *const kResponseTypesSupportedKey = @"response_types_supported";
static NSString *const kResponseModesSupportedKey = @"response_modes_supported";
static NSString *const kGrantTypesSupportedKey = @"grant_types_supported";
static NSString *const kACRValuesSupportedKey = @"acr_values_supported";
static NSString *const kSubjectTypesSupportedKey = @"subject_types_supported";
static NSString *const kIDTokenSigningAlgorithmValuesSupportedKey =
@"id_token_signing_alg_values_supported";
static NSString *const kIDTokenEncryptionAlgorithmValuesSupportedKey =
@"id_token_encryption_alg_values_supported";
static NSString *const kIDTokenEncryptionEncodingValuesSupportedKey =
@"id_token_encryption_enc_values_supported";
static NSString *const kUserinfoSigningAlgorithmValuesSupportedKey =
@"userinfo_signing_alg_values_supported";
static NSString *const kUserinfoEncryptionAlgorithmValuesSupportedKey =
@"userinfo_encryption_alg_values_supported";
static NSString *const kUserinfoEncryptionEncodingValuesSupportedKey =
@"userinfo_encryption_enc_values_supported";
static NSString *const kRequestObjectSigningAlgorithmValuesSupportedKey =
@"request_object_signing_alg_values_supported";
static NSString *const kRequestObjectEncryptionAlgorithmValuesSupportedKey =
@"request_object_encryption_alg_values_supported";
static NSString *const kRequestObjectEncryptionEncodingValuesSupported =
@"request_object_encryption_enc_values_supported";
static NSString *const kTokenEndpointAuthMethodsSupportedKey =
@"token_endpoint_auth_methods_supported";
static NSString *const kTokenEndpointAuthSigningAlgorithmValuesSupportedKey =
@"token_endpoint_auth_signing_alg_values_supported";
static NSString *const kDisplayValuesSupportedKey = @"display_values_supported";
static NSString *const kClaimTypesSupportedKey = @"claim_types_supported";
static NSString *const kClaimsSupportedKey = @"claims_supported";
static NSString *const kServiceDocumentationKey = @"service_documentation";
static NSString *const kClaimsLocalesSupportedKey = @"claims_locales_supported";
static NSString *const kUILocalesSupportedKey = @"ui_locales_supported";
static NSString *const kClaimsParameterSupportedKey = @"claims_parameter_supported";
static NSString *const kRequestParameterSupportedKey = @"request_parameter_supported";
static NSString *const kRequestURIParameterSupportedKey = @"request_uri_parameter_supported";
static NSString *const kRequireRequestURIRegistrationKey = @"require_request_uri_registration";
static NSString *const kOPPolicyURIKey = @"op_policy_uri";
static NSString *const kOPTosURIKey = @"op_tos_uri";
@implementation OIDServiceDiscovery {
NSDictionary *_discoveryDictionary;
}
- (nonnull instancetype)init OID_UNAVAILABLE_USE_INITIALIZER(@selector(initWithDictionary:error:))
- (nullable instancetype)initWithJSON:(NSString *)serviceDiscoveryJSON error:(NSError **)error {
NSData *jsonData = [serviceDiscoveryJSON dataUsingEncoding:NSUTF8StringEncoding];
return [self initWithJSONData:jsonData error:error];
}
- (nullable instancetype)initWithJSONData:(NSData *)serviceDiscoveryJSONData
error:(NSError **_Nullable)error {
NSError *jsonError;
NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:serviceDiscoveryJSONData options:0 error:&jsonError];
if (!json || jsonError) {
*error = [OIDErrorUtilities errorWithCode:OIDErrorCodeJSONDeserializationError
underlyingError:jsonError
description:jsonError.localizedDescription];
return nil;
}
if (![json isKindOfClass:[NSDictionary class]]) {
*error = [OIDErrorUtilities errorWithCode:OIDErrorCodeInvalidDiscoveryDocument
underlyingError:nil
description:@"Discovery document isn't a dictionary"];
return nil;
}
NSString *issuer = (NSString *) json[@"issuer"];
if (issuer && [issuer containsString:@"{tenantid}"]) {
// The Azure AD discovery document's "issuer" value contains the special placeholder
// "{tenantid}". '{' and '}' are invalid characters in URLs and have to be URL encoded before
// the issuer URL can be parsed by NSURL.
NSMutableDictionary *newJson = [NSMutableDictionary dictionaryWithDictionary:json];
newJson[@"issuer"] = [issuer stringByReplacingOccurrencesOfString:@"{tenantid}" withString:@"%7Btenantid%7D"];
json = newJson;
}
return [self initWithDictionary:json error:error];
}
- (nullable instancetype)initWithDictionary:(NSDictionary *)serviceDiscoveryDictionary
error:(NSError **_Nullable)error {
if (![[self class] dictionaryHasRequiredFields:serviceDiscoveryDictionary error:error]) {
return nil;
}
self = [super init];
if (self) {
_discoveryDictionary = [serviceDiscoveryDictionary copy];
}
return self;
}
#pragma mark -
/*! @brief Checks to see if the specified dictionary contains the required fields.
@discussion This test is not meant to provide semantic analysis of the document (eg. fields
where the value @c none is not an allowed option would not cause this method to fail if
their value was @c none.) We are just testing to make sure we can meet the nullability
contract we promised in the header.
*/
+ (BOOL)dictionaryHasRequiredFields:(NSDictionary<NSString *, id> *)dictionary
error:(NSError **_Nullable)error {
static NSString *const kMissingFieldErrorText = @"Missing field: %@";
static NSString *const kInvalidURLFieldErrorText = @"Invalid URL: %@";
NSArray *requiredFields = @[
kIssuerKey,
kAuthorizationEndpointKey,
kTokenEndpointKey,
kJWKSURLKey,
kResponseTypesSupportedKey,
kSubjectTypesSupportedKey,
kIDTokenSigningAlgorithmValuesSupportedKey
];
for (NSString *field in requiredFields) {
if (!dictionary[field]) {
if (error) {
NSString *errorText = [NSString stringWithFormat:kMissingFieldErrorText, field];
*error = [OIDErrorUtilities errorWithCode:OIDErrorCodeInvalidDiscoveryDocument
underlyingError:nil
description:errorText];
}
return NO;
}
}
// Check required URL fields are valid URLs.
NSArray *requiredURLFields = @[
kIssuerKey,
kTokenEndpointKey,
kJWKSURLKey
];
for (NSString *field in requiredURLFields) {
if (![NSURL URLWithString:dictionary[field]]) {
if (error) {
NSString *errorText = [NSString stringWithFormat:kInvalidURLFieldErrorText, field];
*error = [OIDErrorUtilities errorWithCode:OIDErrorCodeInvalidDiscoveryDocument
underlyingError:nil
description:errorText];
}
return NO;
}
}
return YES;
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(nullable NSZone *)zone {
// The documentation for NSCopying specifically advises us to return a reference to the original
// instance in the case where instances are immutable (as ours is):
// "Implement NSCopying by retaining the original instead of creating a new copy when the class
// and its contents are immutable."
return self;
}
#pragma mark - NSSecureCoding
+ (BOOL)supportsSecureCoding {
return YES;
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
NSError *error;
NSDictionary *dictionary;
if ([aDecoder containsValueForKey:kDiscoveryDictionaryKey]) {
// We're decoding a collection type (NSDictionary) from NSJSONSerialization's
// +JSONObjectWithData, so we need to include all classes that could potentially be contained
// within.
NSSet<Class> *allowedClasses = [NSSet setWithArray:@[[NSDictionary class],
[NSArray class],
[NSString class],
[NSNumber class],
[NSNull class]]];
dictionary = [aDecoder decodeObjectOfClasses:allowedClasses
forKey:kDiscoveryDictionaryKey];
} else {
// Decode using the old encoding which delegated to NSDictionary's encodeWithCoder:
// implementation:
//
// - (void)encodeWithCoder:(NSCoder *)aCoder {
// [_discoveryDictionary encodeWithCoder:aCoder];
// }
dictionary = [[NSDictionary alloc] initWithCoder:aDecoder];
}
self = [self initWithDictionary:dictionary error:&error];
if (error) {
return nil;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_discoveryDictionary forKey:kDiscoveryDictionaryKey];
// Provide forward compatibilty by continuing to add the old encoding.
[_discoveryDictionary encodeWithCoder:aCoder];
}
#pragma mark - Properties
- (NSDictionary<NSString *, NSString *> *)discoveryDictionary {
return _discoveryDictionary;
}
- (NSURL *)issuer {
return [NSURL URLWithString:_discoveryDictionary[kIssuerKey]];
}
- (NSURL *)authorizationEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kAuthorizationEndpointKey]];
}
- (nullable NSURL *)deviceAuthorizationEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kDeviceAuthorizationEndpointKey]];
}
- (NSURL *)tokenEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kTokenEndpointKey]];
}
- (nullable NSURL *)userinfoEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kUserinfoEndpointKey]];
}
- (NSURL *)jwksURL {
return [NSURL URLWithString:_discoveryDictionary[kJWKSURLKey]];
}
- (nullable NSURL *)registrationEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kRegistrationEndpointKey]];
}
- (nullable NSURL *)endSessionEndpoint {
return [NSURL URLWithString:_discoveryDictionary[kEndSessionEndpointKey]];
}
- (nullable NSArray<NSString *> *)scopesSupported {
return _discoveryDictionary[kScopesSupportedKey];
}
- (NSArray<NSString *> *)responseTypesSupported {
return _discoveryDictionary[kResponseTypesSupportedKey];
}
- (nullable NSArray<NSString *> *)responseModesSupported {
return _discoveryDictionary[kResponseModesSupportedKey];
}
- (nullable NSArray<NSString *> *)grantTypesSupported {
return _discoveryDictionary[kGrantTypesSupportedKey];
}
- (nullable NSArray<NSString *> *)acrValuesSupported {
return _discoveryDictionary[kACRValuesSupportedKey];
}
- (NSArray<NSString *> *)subjectTypesSupported {
return _discoveryDictionary[kSubjectTypesSupportedKey];
}
- (NSArray<NSString *> *) IDTokenSigningAlgorithmValuesSupported {
return _discoveryDictionary[kIDTokenSigningAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)IDTokenEncryptionAlgorithmValuesSupported {
return _discoveryDictionary[kIDTokenEncryptionAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)IDTokenEncryptionEncodingValuesSupported {
return _discoveryDictionary[kIDTokenEncryptionEncodingValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)userinfoSigningAlgorithmValuesSupported {
return _discoveryDictionary[kUserinfoSigningAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)userinfoEncryptionAlgorithmValuesSupported {
return _discoveryDictionary[kUserinfoEncryptionAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)userinfoEncryptionEncodingValuesSupported {
return _discoveryDictionary[kUserinfoEncryptionEncodingValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)requestObjectSigningAlgorithmValuesSupported {
return _discoveryDictionary[kRequestObjectSigningAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *) requestObjectEncryptionAlgorithmValuesSupported {
return _discoveryDictionary[kRequestObjectEncryptionAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *) requestObjectEncryptionEncodingValuesSupported {
return _discoveryDictionary[kRequestObjectEncryptionEncodingValuesSupported];
}
- (nullable NSArray<NSString *> *)tokenEndpointAuthMethodsSupported {
return _discoveryDictionary[kTokenEndpointAuthMethodsSupportedKey];
}
- (nullable NSArray<NSString *> *)tokenEndpointAuthSigningAlgorithmValuesSupported {
return _discoveryDictionary[kTokenEndpointAuthSigningAlgorithmValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)displayValuesSupported {
return _discoveryDictionary[kDisplayValuesSupportedKey];
}
- (nullable NSArray<NSString *> *)claimTypesSupported {
return _discoveryDictionary[kClaimTypesSupportedKey];
}
- (nullable NSArray<NSString *> *)claimsSupported {
return _discoveryDictionary[kClaimsSupportedKey];
}
- (nullable NSURL *)serviceDocumentation {
return [NSURL URLWithString:_discoveryDictionary[kServiceDocumentationKey]];
}
- (nullable NSArray<NSString *> *)claimsLocalesSupported {
return _discoveryDictionary[kClaimsLocalesSupportedKey];
}
- (nullable NSArray<NSString *> *)UILocalesSupported {
return _discoveryDictionary[kUILocalesSupportedKey];
}
- (BOOL)claimsParameterSupported {
return [_discoveryDictionary[kClaimsParameterSupportedKey] boolValue];
}
- (BOOL)requestParameterSupported {
return [_discoveryDictionary[kRequestParameterSupportedKey] boolValue];
}
- (BOOL)requestURIParameterSupported {
// Default is true/YES.
if (!_discoveryDictionary[kRequestURIParameterSupportedKey]) {
return YES;
}
return [_discoveryDictionary[kRequestURIParameterSupportedKey] boolValue];
}
- (BOOL)requireRequestURIRegistration {
return [_discoveryDictionary[kRequireRequestURIRegistrationKey] boolValue];
}
- (nullable NSURL *)OPPolicyURI {
return [NSURL URLWithString:_discoveryDictionary[kOPPolicyURIKey]];
}
- (nullable NSURL *)OPTosURI {
return [NSURL URLWithString:_discoveryDictionary[kOPTosURIKey]];
}
@end
NS_ASSUME_NONNULL_END