mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-12 21:35:55 +01:00
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
144 lines
4.6 KiB
C#
144 lines
4.6 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using Microsoft.IdentityModel.Protocols;
|
|
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Moq;
|
|
using WireMock.Authentication;
|
|
|
|
namespace WireMock.Net.Tests.Authentication;
|
|
|
|
public class AzureADAuthenticationMatcherTests
|
|
{
|
|
public enum AzureADTokenVersion
|
|
{
|
|
V1,
|
|
V2
|
|
}
|
|
|
|
private const string Tenant = "test-tenant-id";
|
|
private const string Audience = "test-audience";
|
|
private static readonly Dictionary<AzureADTokenVersion, string> IssuerUrlTemplates = new()
|
|
{
|
|
{ AzureADTokenVersion.V1, "https://sts.windows.net/{0}/" },
|
|
{ AzureADTokenVersion.V2, "https://login.microsoftonline.com/{0}/v2.0" }
|
|
};
|
|
private readonly Mock<IConfigurationManager<OpenIdConnectConfiguration>> _openIdConnectConfigurationManagerMock = new();
|
|
|
|
private readonly AzureADAuthenticationMatcher _sut;
|
|
|
|
public AzureADAuthenticationMatcherTests()
|
|
{
|
|
var jwtSecurityTokenHandler = new MockJwtSecurityTokenHandler();
|
|
_openIdConnectConfigurationManagerMock.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(new OpenIdConnectConfiguration());
|
|
|
|
_sut = new(jwtSecurityTokenHandler, _openIdConnectConfigurationManagerMock.Object, Tenant, Audience);
|
|
}
|
|
|
|
[Fact]
|
|
public void AzureADAuthenticationMatcher_Name_ShouldReturnCorrectName()
|
|
{
|
|
// Act
|
|
var name = _sut.Name;
|
|
|
|
// Assert
|
|
Assert.Equal("AzureADAuthenticationMatcher", name);
|
|
}
|
|
|
|
[Fact]
|
|
public void AzureADAuthenticationMatcher_GetPatterns_ShouldReturnEmptyPatterns()
|
|
{
|
|
// Act
|
|
var patterns = _sut.GetPatterns();
|
|
|
|
// Assert
|
|
Assert.NotNull(patterns);
|
|
Assert.Empty(patterns);
|
|
}
|
|
|
|
[Fact]
|
|
public void AzureADAuthenticationMatcher_IsMatch_ShouldReturnMismatch_WhenTokenIsInvalid()
|
|
{
|
|
// Arrange
|
|
var sut = new AzureADAuthenticationMatcher(new JwtSecurityTokenHandler(), _openIdConnectConfigurationManagerMock.Object, Tenant, Audience);
|
|
var invalidToken = "invalid-token";
|
|
|
|
// Act
|
|
var result = sut.IsMatch($"Bearer {invalidToken}");
|
|
|
|
// Assert
|
|
Assert.Equal(0.0, result.Score);
|
|
Assert.NotNull(result.Exception);
|
|
}
|
|
|
|
[Fact]
|
|
public void AzureADAuthenticationMatcher_IsMatch_ShouldReturnMismatch_WhenTokenIsNullOrEmpty()
|
|
{
|
|
// Act
|
|
var result = _sut.IsMatch(null);
|
|
|
|
// Assert
|
|
Assert.Equal(0.0, result.Score);
|
|
Assert.Null(result.Exception);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(AzureADTokenVersion.V1)]
|
|
[InlineData(AzureADTokenVersion.V2)]
|
|
public void AzureADAuthenticationMatcher_IsMatch_ShouldReturnPerfect_WhenTokenIsValid(AzureADTokenVersion version)
|
|
{
|
|
// Arrange
|
|
var token = GenerateValidToken(Tenant, Audience, version);
|
|
|
|
// Act
|
|
var result = _sut.IsMatch($"Bearer {token}");
|
|
|
|
// Assert
|
|
Assert.Equal(1.0, result.Score);
|
|
Assert.Null(result.Exception);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(AzureADTokenVersion.V1)]
|
|
[InlineData(AzureADTokenVersion.V2)]
|
|
public void AzureADAuthenticationMatcher_IsMatch_ShouldReturnMismatch_WhenTenantMismatch(AzureADTokenVersion version)
|
|
{
|
|
// Arrange
|
|
var sut = new AzureADAuthenticationMatcher(new JwtSecurityTokenHandler(), _openIdConnectConfigurationManagerMock.Object, Tenant, Audience);
|
|
var token = GenerateValidToken("different-tenant", Audience, version);
|
|
|
|
// Act
|
|
var result = sut.IsMatch($"Bearer {token}");
|
|
|
|
// Assert
|
|
Assert.Equal(0.0, result.Score);
|
|
Assert.NotNull(result.Exception);
|
|
}
|
|
|
|
private static string GenerateValidToken(string tenant, string audience, AzureADTokenVersion version)
|
|
{
|
|
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes($"test-signing-key-{Guid.NewGuid()}"));
|
|
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
|
|
|
var claims = new[]
|
|
{
|
|
new Claim(JwtRegisteredClaimNames.Sub, "test-user"),
|
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
|
new Claim("tid", tenant)
|
|
};
|
|
|
|
var issuer = string.Format(IssuerUrlTemplates[version], tenant);
|
|
|
|
var token = new JwtSecurityToken(
|
|
issuer: issuer,
|
|
audience: audience,
|
|
claims: claims,
|
|
expires: DateTime.UtcNow.AddMinutes(30),
|
|
signingCredentials: credentials);
|
|
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
} |