Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the special common/organizations/consumers Azure AD tenants #714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/AppAuthCore/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,16 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
// OpenID Connect Core Section 3.1.3.7. rule #2
// Validates that the issuer in the ID Token matches that of the discovery document.
NSURL *issuer = tokenResponse.request.configuration.issuer;
if (issuer && [issuer.path containsString:@"{tenantid}"]) {
// The Azure AD discovery document's "issuer" value contains the special placeholder
// "{tenantid}". This needs to be replaced with the actual tenant ID of the authenticated
// user, from the "tid" claim of the ID token, before validation.
NSString *tid = idToken.claims[@"tid"];
if (tid) {
issuer = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@%@", issuer.scheme, issuer.host,
[issuer.path stringByReplacingOccurrencesOfString:@"{tenantid}" withString:tid]]];
}
}
if (issuer && ![idToken.issuer isEqual:issuer]) {
NSError *invalidIDToken =
[OIDErrorUtilities errorWithCode:OIDErrorCodeIDTokenFailedValidationError
Expand Down
10 changes: 10 additions & 0 deletions Sources/AppAuthCore/OIDServiceDiscovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ - (nullable instancetype)initWithJSONData:(NSData *)serviceDiscoveryJSONData
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];
}

Expand Down