/src/Identity/ApiAuthorization.IdentityServer/test/Authentication/LocalApiJwtBearerConfigurationTest.cs

https://github.com/aspnet/AspNetCore · C# · 156 lines · 118 code · 24 blank · 14 comment · 0 complexity · 6bd6ff53ef83a17949e1720827880ea1 MD5 · raw file

  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using System.Threading.Tasks;
  6. using IdentityServer4.Configuration;
  7. using IdentityServer4.Stores;
  8. using Microsoft.AspNetCore.ApiAuthorization.IdentityServer.Configuration;
  9. using Microsoft.AspNetCore.Authentication;
  10. using Microsoft.AspNetCore.Authentication.JwtBearer;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.IdentityModel.Tokens;
  14. using Moq;
  15. using Xunit;
  16. namespace Microsoft.AspNetCore.ApiAuthorization.IdentityServer
  17. {
  18. public class IdentityServerJwtBearerOptionsConfigurationTest
  19. {
  20. [Fact]
  21. public void Configure_SetsUpBearerSchemeForTheLocalApi()
  22. {
  23. // Arrange
  24. var localApiDescriptor = new Mock<IIdentityServerJwtDescriptor>();
  25. localApiDescriptor.Setup(lad => lad.GetResourceDefinitions())
  26. .Returns(new Dictionary<string, ResourceDefinition>
  27. {
  28. ["TestAPI"] = new ResourceDefinition { Profile = ApplicationProfiles.IdentityServerJwt }
  29. });
  30. var bearerConfiguration = new IdentityServerJwtBearerOptionsConfiguration(
  31. "authScheme",
  32. "TestAPI",
  33. localApiDescriptor.Object);
  34. var options = new JwtBearerOptions();
  35. // Act
  36. bearerConfiguration.Configure("authScheme", options);
  37. // Assert
  38. Assert.Equal("name", options.TokenValidationParameters.NameClaimType);
  39. Assert.Equal("role", options.TokenValidationParameters.RoleClaimType);
  40. Assert.Equal("TestAPI", options.Audience);
  41. }
  42. [Fact]
  43. public async Task ResolveAuthorityAndKeysAsync_SetsUpAuthorityAndKeysOnTheTokenValidationParametersAsync()
  44. {
  45. // Arrange
  46. var localApiDescriptor = new Mock<IIdentityServerJwtDescriptor>();
  47. localApiDescriptor.Setup(lad => lad.GetResourceDefinitions())
  48. .Returns(new Dictionary<string, ResourceDefinition>
  49. {
  50. ["TestAPI"] = new ResourceDefinition { Profile = ApplicationProfiles.IdentityServerJwt }
  51. });
  52. var credentialsStore = new Mock<ISigningCredentialStore>();
  53. var key = new RsaSecurityKey(RSA.Create());
  54. credentialsStore.Setup(cs => cs.GetSigningCredentialsAsync())
  55. .ReturnsAsync(new SigningCredentials(key, "RS256"));
  56. var context = new DefaultHttpContext();
  57. context.Request.Scheme = "https";
  58. context.Request.Host = new HostString("localhost");
  59. context.RequestServices = new ServiceCollection()
  60. .AddSingleton(new IdentityServerOptions())
  61. .AddSingleton(credentialsStore.Object)
  62. .BuildServiceProvider();
  63. var options = new JwtBearerOptions();
  64. var args = new MessageReceivedContext(context, new AuthenticationScheme("TestAPI",null, Mock.Of<IAuthenticationHandler>().GetType()), options);
  65. // Act
  66. await IdentityServerJwtBearerOptionsConfiguration.ResolveAuthorityAndKeysAsync(args);
  67. // Assert
  68. Assert.Equal("https://localhost", options.TokenValidationParameters.ValidIssuer);
  69. Assert.Equal(key, options.TokenValidationParameters.IssuerSigningKey);
  70. }
  71. [Fact]
  72. public void Configure_IgnoresOptionsForDifferentSchemes()
  73. {
  74. // Arrange
  75. var localApiDescriptor = new Mock<IIdentityServerJwtDescriptor>();
  76. localApiDescriptor.Setup(lad => lad.GetResourceDefinitions())
  77. .Returns(new Dictionary<string, ResourceDefinition>
  78. {
  79. ["TestAPI"] = new ResourceDefinition { Profile = ApplicationProfiles.IdentityServerJwt }
  80. });
  81. var bearerConfiguration = new IdentityServerJwtBearerOptionsConfiguration(
  82. "authScheme",
  83. "TestAPI",
  84. localApiDescriptor.Object);
  85. var options = new JwtBearerOptions();
  86. // Act
  87. bearerConfiguration.Configure("otherScheme", options);
  88. // Assert
  89. Assert.NotEqual("name", options.TokenValidationParameters.NameClaimType);
  90. Assert.NotEqual("role", options.TokenValidationParameters.RoleClaimType);
  91. Assert.NotEqual("TestAPI", options.Audience);
  92. Assert.NotEqual("https://localhost", options.Authority);
  93. }
  94. [Fact]
  95. public void Configure_IgnoresOptionsForNonExistingAPIs()
  96. {
  97. // Arrange
  98. var contextAccessor = new Mock<IHttpContextAccessor>();
  99. var context = new DefaultHttpContext();
  100. context.Request.Scheme = "https";
  101. context.Request.Host = new HostString("localhost");
  102. context.RequestServices = new ServiceCollection()
  103. .AddSingleton(new IdentityServerOptions())
  104. .BuildServiceProvider();
  105. contextAccessor.SetupGet(ca => ca.HttpContext).Returns(
  106. context);
  107. var localApiDescriptor = new Mock<IIdentityServerJwtDescriptor>();
  108. localApiDescriptor.Setup(lad => lad.GetResourceDefinitions())
  109. .Returns(new Dictionary<string, ResourceDefinition>
  110. {
  111. ["TestAPI"] = new ResourceDefinition { Profile = ApplicationProfiles.IdentityServerJwt }
  112. });
  113. var credentialsStore = new Mock<ISigningCredentialStore>();
  114. var key = new RsaSecurityKey(RSA.Create());
  115. credentialsStore.Setup(cs => cs.GetSigningCredentialsAsync())
  116. .ReturnsAsync(new SigningCredentials(key, "RS256"));
  117. var bearerConfiguration = new IdentityServerJwtBearerOptionsConfiguration(
  118. "authScheme",
  119. "NonExistingApi",
  120. localApiDescriptor.Object);
  121. var options = new JwtBearerOptions();
  122. // Act
  123. bearerConfiguration.Configure("authScheme", options);
  124. // Assert
  125. Assert.NotEqual("name", options.TokenValidationParameters.NameClaimType);
  126. Assert.NotEqual("role", options.TokenValidationParameters.RoleClaimType);
  127. Assert.NotEqual(key, options.TokenValidationParameters.IssuerSigningKey);
  128. Assert.NotEqual("TestAPI", options.Audience);
  129. Assert.NotEqual("https://localhost", options.Authority);
  130. }
  131. }
  132. }