Skip to content

Commit d67da66

Browse files
committed
Add support for the special common/organizations/consumers Azure AD tenants
When not targeting a specific Azure AD tenant (specified by a tenant GUID in the discovery document URL) but rather one of the "common", "organizations" or "consumers" multi-tenant aliases (see 1), discovery document parsing and ID token validation require a few extra steps: * The discovery document's "issuer" value contains the special placeholder "{tenantid}". As '{' and '}' are invalid characters in URLs, AppAuth has to URL encode these characters before the issuer URL can be parsed by NSURL in OIDServiceDiscovery.m. * The same "{tenantid}" placeholder needs to be replaced with the actual tenant ID of the authenticated user, from the "tid" claim (see 2) of the ID token, before ID token validation is performed in OIDAuthorizationService.m. 1: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc#fetch-the-openid-connect-metadata-document 2: https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims
1 parent 8582608 commit d67da66

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Source/AppAuthCore/OIDAuthorizationService.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,16 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
563563
// OpenID Connect Core Section 3.1.3.7. rule #2
564564
// Validates that the issuer in the ID Token matches that of the discovery document.
565565
NSURL *issuer = tokenResponse.request.configuration.issuer;
566+
if (issuer && [issuer.path containsString:@"{tenantid}"]) {
567+
// The Azure AD discovery document's "issuer" value contains the special placeholder
568+
// "{tenantid}". This needs to be replaced with the actual tenant ID of the authenticated
569+
// user, from the "tid" claim of the ID token, before validation.
570+
NSString *tid = idToken.claims[@"tid"];
571+
if (tid) {
572+
issuer = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@%@", issuer.scheme, issuer.host,
573+
[issuer.path stringByReplacingOccurrencesOfString:@"{tenantid}" withString:tid]]];
574+
}
575+
}
566576
if (issuer && ![idToken.issuer isEqual:issuer]) {
567577
NSError *invalidIDToken =
568578
[OIDErrorUtilities errorWithCode:OIDErrorCodeIDTokenFailedValidationError

Source/AppAuthCore/OIDServiceDiscovery.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ - (nullable instancetype)initWithJSONData:(NSData *)serviceDiscoveryJSONData
102102
return nil;
103103
}
104104

105+
NSString *issuer = (NSString *) json[@"issuer"];
106+
if (issuer && [issuer containsString:@"{tenantid}"]) {
107+
// The Azure AD discovery document's "issuer" value contains the special placeholder
108+
// "{tenantid}". '{' and '}' are invalid characters in URLs and have to be URL encoded before
109+
// the issuer URL can be parsed by NSURL.
110+
NSMutableDictionary *newJson = [NSMutableDictionary dictionaryWithDictionary:json];
111+
newJson[@"issuer"] = [issuer stringByReplacingOccurrencesOfString:@"{tenantid}" withString:@"%7Btenantid%7D"];
112+
json = newJson;
113+
}
114+
105115
return [self initWithDictionary:json error:error];
106116
}
107117

0 commit comments

Comments
 (0)