Skip to content

Commit

Permalink
Respect TVP.RequireAudience when set to false (#3055)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kllysng authored Dec 14, 2024
1 parent 2ecd35b commit ebcc58b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Microsoft.IdentityModel.Tokens.LogMessages.IDX10273 = "IDX10273: Algorithm
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10274 = "IDX10274: IssuerSigningKeyValidationDelegate threw an exception, see inner exception." -> string
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10275 = "IDX10275: TokenTypeValidationDelegate threw an exception, see inner exception." -> string
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10276 = "IDX10276: TokenReplayValidationDelegate threw an exception, see inner exception." -> string
const Microsoft.IdentityModel.Tokens.LogMessages.IDX10277 = "IDX10277: RequireAudience property on ValidationParameters is set to false. Exiting without validating the audience." -> string
Microsoft.IdentityModel.Tokens.AlgorithmValidationError
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
Microsoft.IdentityModel.Tokens.AlgorithmValidationError.InvalidAlgorithm.get -> string
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.IdentityModel.Tokens/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal static class LogMessages
public const string IDX10274 = "IDX10274: IssuerSigningKeyValidationDelegate threw an exception, see inner exception.";
public const string IDX10275 = "IDX10275: TokenTypeValidationDelegate threw an exception, see inner exception.";
public const string IDX10276 = "IDX10276: TokenReplayValidationDelegate threw an exception, see inner exception.";
public const string IDX10277 = "IDX10277: RequireAudience property on ValidationParameters is set to false. Exiting without validating the audience.";

// 10500 - SignatureValidation
public const string IDX10500 = "IDX10500: Signature validation failed. No security keys were provided to validate the signature.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,13 @@ public string NameClaimType
public bool RefreshBeforeValidation { get; set; }

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

Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/Validators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public static void ValidateAudience(IEnumerable<string> audiences, SecurityToken
return;
}

if (!validationParameters.RequireAudience && !audiences.Any())
{
LogHelper.LogWarning(LogMessages.IDX10277);
return;
}

if (audiences == null)
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidAudienceException(LogMessages.IDX10207) { InvalidAudience = null });

Expand Down
Loading

0 comments on commit ebcc58b

Please sign in to comment.