Skip to content

Commit ebcc58b

Browse files
authored
Respect TVP.RequireAudience when set to false (#3055)
* Fix AudienceValidationTheoryData to include testId * respect TVP.RequireAudience if false, unit test * add more details to TVP.RequireAudience flag * add test cases * specify TVP.RequireAudiences is used for SAML or JWT tokens
1 parent 2ecd35b commit ebcc58b

File tree

5 files changed

+73
-63
lines changed

5 files changed

+73
-63
lines changed

src/Microsoft.IdentityModel.Tokens/InternalAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Microsoft.IdentityModel.Tokens.LogMessages.IDX10273 = "IDX10273: Algorithm
88
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10274 = "IDX10274: IssuerSigningKeyValidationDelegate threw an exception, see inner exception." -> string
99
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10275 = "IDX10275: TokenTypeValidationDelegate threw an exception, see inner exception." -> string
1010
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10276 = "IDX10276: TokenReplayValidationDelegate threw an exception, see inner exception." -> string
11+
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10277 = "IDX10277: RequireAudience property on ValidationParameters is set to false. Exiting without validating the audience." -> string
1112
Microsoft.IdentityModel.Tokens.AlgorithmValidationError
1213
Microsoft.IdentityModel.Tokens.AlgorithmValidationError.AlgorithmValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType validationFailureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidAlgorithm, System.Exception innerException = null) -> void
1314
Microsoft.IdentityModel.Tokens.AlgorithmValidationError.InvalidAlgorithm.get -> string

src/Microsoft.IdentityModel.Tokens/LogMessages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ internal static class LogMessages
9595
public const string IDX10274 = "IDX10274: IssuerSigningKeyValidationDelegate threw an exception, see inner exception.";
9696
public const string IDX10275 = "IDX10275: TokenTypeValidationDelegate threw an exception, see inner exception.";
9797
public const string IDX10276 = "IDX10276: TokenReplayValidationDelegate threw an exception, see inner exception.";
98+
public const string IDX10277 = "IDX10277: RequireAudience property on ValidationParameters is set to false. Exiting without validating the audience.";
9899

99100
// 10500 - SignatureValidation
100101
public const string IDX10500 = "IDX10500: Signature validation failed. No security keys were provided to validate the signature.";

src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,13 @@ public string NameClaimType
459459
public bool RefreshBeforeValidation { get; set; }
460460

461461
/// <summary>
462-
/// Gets or sets a value indicating whether SAML tokens must have at least one AudienceRestriction.
462+
/// Gets or sets a value indicating whether SAML or JWT tokens must have at least one AudienceRestriction.
463463
/// The default is <c>true</c>.
464464
/// </summary>
465+
/// <remarks>
466+
/// If set to false and the Audience is null, Audience validation will be skipped.
467+
/// If set to false and the Audience is not null, the Audience will still be validated.
468+
/// </remarks>
465469
[DefaultValue(true)]
466470
public bool RequireAudience { get; set; }
467471

src/Microsoft.IdentityModel.Tokens/Validators.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public static void ValidateAudience(IEnumerable<string> audiences, SecurityToken
8787
return;
8888
}
8989

90+
if (!validationParameters.RequireAudience && !audiences.Any())
91+
{
92+
LogHelper.LogWarning(LogMessages.IDX10277);
93+
return;
94+
}
95+
9096
if (audiences == null)
9197
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidAudienceException(LogMessages.IDX10207) { InvalidAudience = null });
9298

0 commit comments

Comments
 (0)