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

- Enhanced Token Validation Parameter Helpers to allow multiple audie… #23

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace AzureFunctions.Extensions.OpenIDConnect.InProcess.Tests
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using NUnit.Framework;
using System.Collections.Generic;

public class TokenValidationShould
{
Expand Down Expand Up @@ -46,5 +47,42 @@ public void BeSecure_When_Using_Audience_And_Issuer()
// Assert
tokenValidationParameters.Should().BeEquivalentTo(expected);
}

[Test]
public void BeSecure_When_Using_Audiences_And_Issuer()
{
// Arrange
var collection = ServiceCollectionFixture.MinimalAzFunctionsServices();

var audiences = new List<string> { "my_audience_1", "my_audience_2" };
var issuer = "https://me.secure.com";

collection.AddOpenIDConnect(builder =>
{
builder.SetIssuerBaseUrlConfiguration("http://anyurl.com");
builder.SetTokenValidation(audiences, issuer);
});

var provider = collection.BuildServiceProvider();

var expected = new TokenValidationParameters
{
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,

ValidateAudience = true,
ValidAudiences = audiences,

ValidateIssuer = true,
ValidIssuer = issuer
};

// Act
var tokenValidationParameters = provider.GetService<TokenValidationParameters>();

// Assert
tokenValidationParameters.Should().BeEquivalentTo(expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using NUnit.Framework;
using System.Collections.Generic;

namespace AzureFunctions.Extensions.OpenIDConnect.Isolated.Tests
{
Expand Down Expand Up @@ -45,5 +46,42 @@ public void BeSecure_When_Using_Audience_And_Issuer()
// Assert
tokenValidationParameters.Should().BeEquivalentTo(expected);
}

[Test]
public void BeSecure_When_Using_Audiences_And_Issuer()
{
// Arrange
var collection = ServiceCollectionFixture.MinimalAzFunctionsServices();

var audiences = new List<string> { "my_audience_1", "my_audience_2" };
var issuer = "https://me.secure.com";

collection.AddOpenIDConnect(builder =>
{
builder.SetIssuerBaseUrlConfiguration("http://anyurl.com");
builder.SetTokenValidation(audiences, issuer);
});

var provider = collection.BuildServiceProvider();

var expected = new TokenValidationParameters
{
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,

ValidateAudience = true,
ValidAudiences = audiences,

ValidateIssuer = true,
ValidIssuer = issuer
};

// Act
var tokenValidationParameters = provider.GetService<TokenValidationParameters>();

// Assert
tokenValidationParameters.Should().BeEquivalentTo(expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public void SetTokenValidation(string audience, string issuer)
SetTokenValidation(TokenValidationParametersHelpers.Default(audience, issuer));
}

public void SetTokenValidation(IEnumerable<string> audiences, string issuer)
{
SetTokenValidation(TokenValidationParametersHelpers.Default(audiences, issuer));
}

public void SetTokenValidation(TokenValidationParameters settings)
{
_services.AddSingleton(settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.IdentityModel.Tokens;
using System.Collections.Generic;

namespace AzureFunctions.Extensions.OpenIDConnect.Configuration
{
Expand All @@ -19,5 +20,21 @@ public static TokenValidationParameters Default(string audience, string issuer)
ValidIssuer = issuer
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind using your newly added overload?
something like :
return Default(new [] {audience}, issuer);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, I'll take a look into it

}

public static TokenValidationParameters Default(IEnumerable<string> audiences, string issuer)
{
return new TokenValidationParameters
{
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,

ValidateAudience = true,
ValidAudiences = audiences,

ValidateIssuer = true,
ValidIssuer = issuer
};
}
}
}