Skip to content

Commit

Permalink
Changes per latest PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLozensky committed Jun 21, 2024
1 parent 3df1cc9 commit 40edbce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ internal static void WriteJwsPayload(

writer.WriteStartObject();

if (!tokenDescriptor.Audiences.IsNullOrEmpty())
if (tokenDescriptor.Audiences.Count > 0)
{
if (!tokenDescriptor.Audience.IsNullOrEmpty())
JsonPrimitives.WriteStrings(ref writer, JwtPayloadUtf8Bytes.Aud, tokenDescriptor.Audiences, tokenDescriptor.Audience);
Expand All @@ -725,6 +725,7 @@ internal static void WriteJwsPayload(
writer.WriteStringValue(tokenDescriptor.Audience);
audienceSet = true;
}

if (!string.IsNullOrEmpty(tokenDescriptor.Issuer))
{
issuerSet = true;
Expand Down Expand Up @@ -769,7 +770,7 @@ internal static void WriteJwsPayload(
if (LogHelper.IsEnabled(EventLogLevel.Informational))
{
string descriptorMemberName = null;
if (!tokenDescriptor.Audiences.IsNullOrEmpty())
if (tokenDescriptor.Audiences.Count > 0)
descriptorMemberName = nameof(tokenDescriptor.Audiences);
else if (!string.IsNullOrEmpty(tokenDescriptor.Audience))
descriptorMemberName = nameof(tokenDescriptor.Audience);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,40 +367,37 @@ protected virtual SamlConditions CreateConditions(SecurityTokenDescriptor tokenD
else if (SetDefaultTimesOnTokenCreation)
conditions.NotOnOrAfter = DateTime.UtcNow + TimeSpan.FromMinutes(TokenLifetimeInMinutes);

if (!tokenDescriptor.Audiences.IsNullOrEmpty())
if (tokenDescriptor.Audiences.Count > 0)
{
if (!tokenDescriptor.Audience.IsNullOrEmpty())
conditions.Conditions.Add(CreateAudienceRestrictionCondition(tokenDescriptor.Audience, tokenDescriptor.Audiences));
else
conditions.Conditions.Add(CreateAudienceRestrictionCondition(tokenDescriptor.Audiences));

else if(!tokenDescriptor.Audience.IsNullOrEmpty())
conditions.Conditions.Add(CreateAudienceRestrictionCondition(tokenDescriptor.Audience));
}
else if (!tokenDescriptor.Audience.IsNullOrEmpty())
{
conditions.Conditions.Add(new SamlAudienceRestrictionCondition(new Uri(tokenDescriptor.Audience)));
}

return conditions;
}
private static SamlAudienceRestrictionCondition CreateAudienceRestrictionCondition(string audience)
{
SamlAudienceRestrictionCondition audRestrictionCondition = new ();
audRestrictionCondition.Audiences.Add(new Uri(audience));
return audRestrictionCondition;
}


private static SamlAudienceRestrictionCondition CreateAudienceRestrictionCondition(IList<string> audiences)
{
SamlAudienceRestrictionCondition audRestrictionCondition = new();
foreach (var audience in audiences)
audRestrictionCondition.Audiences.Add(new Uri(audience));
for (int i = 0; i < audiences.Count; i++)
audRestrictionCondition.Audiences.Add(new Uri(audiences[i]));

return audRestrictionCondition;
}

private static SamlCondition CreateAudienceRestrictionCondition(string audience, IList<string> audiences)
{
SamlAudienceRestrictionCondition audRestrictionCondition = new();
audRestrictionCondition.Audiences.Add(new Uri(audience));
SamlAudienceRestrictionCondition audRestrictionCondition = new(new Uri(audience));
for (int i = 0; i < audiences.Count; i++)
audRestrictionCondition.Audiences.Add(new Uri(audiences[i]));

return audRestrictionCondition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,14 @@ protected virtual Saml2Conditions CreateConditions(SecurityTokenDescriptor token
else if (SetDefaultTimesOnTokenCreation)
conditions.NotOnOrAfter = DateTime.UtcNow + TimeSpan.FromMinutes(TokenLifetimeInMinutes);

var audienceRestriction = new Saml2AudienceRestriction(tokenDescriptor.Audiences);

if (!string.IsNullOrEmpty(tokenDescriptor.Audience))
audienceRestriction.Audiences.Add(tokenDescriptor.Audience);

conditions.AudienceRestrictions.Add(audienceRestriction);
if (tokenDescriptor.Audiences.Count > 0)
{
conditions.AudienceRestrictions.Add(new Saml2AudienceRestriction(tokenDescriptor.Audiences));
if (!string.IsNullOrEmpty(tokenDescriptor.Audience))
conditions.AudienceRestrictions.Add(new Saml2AudienceRestriction(tokenDescriptor.Audience));
}
else if (!string.IsNullOrEmpty(tokenDescriptor.Audience))
conditions.AudienceRestrictions.Add(new Saml2AudienceRestriction(tokenDescriptor.Audience));

return conditions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
if (tokenDescriptor.Claims != null && tokenDescriptor.Claims.Count > 0)
payload.Merge(JObject.FromObject(tokenDescriptor.Claims), new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Replace });

if (!tokenDescriptor.Audiences.IsNullOrEmpty())
if (tokenDescriptor.Audiences.Count > 0)
{
if (payload.ContainsKey(JwtRegisteredClaimNames.Aud))
LogDuplicatedClaim(nameof(tokenDescriptor.Audiences));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void CheckExpectedDifferenceInAudClaimUsingSecurityTokenDescriptor(Create

if (audMemberSet)
{
if (!theoryData.TokenDescriptor.Audiences.IsNullOrEmpty())
if (theoryData.TokenDescriptor.Audiences.Count > 0)
expectedAudClaimCount += theoryData.TokenDescriptor.Audiences.Count;

if (!theoryData.TokenDescriptor.Audience.IsNullOrEmpty())
Expand Down

0 comments on commit 40edbce

Please sign in to comment.