diff --git a/src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs b/src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs index 72fc2146..08c8df97 100644 --- a/src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs +++ b/src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs @@ -228,9 +228,9 @@ private async Task Invoke(HttpContext httpContext, bool retry) // // As a rule all image requests should contain valid commands only. // Key generation uses string.Create under the hood with very low allocation so should be good enough as a cache key. - hmac = await HMACTokenLru.GetOrAddAsync( + hmac = HMACTokenLru.GetOrAdd( httpContext.Request.GetEncodedUrl(), - _ => this.authorizationUtilities.ComputeHMACAsync(imageCommandContext)); + _ => this.authorizationUtilities.ComputeHMAC(imageCommandContext)); } await this.options.OnParseCommandsAsync.Invoke(imageCommandContext); diff --git a/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs b/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs index 82c71984..fa4c21e6 100644 --- a/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs +++ b/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Web.Middleware; /// public class ImageSharpMiddlewareOptions { - private Func> onComputeHMACAsync = (context, secret) => + private Func onComputeHMAC = (context, secret) => { string uri = CaseHandlingUriBuilder.BuildRelative( CaseHandlingUriBuilder.CaseHandling.LowerInvariant, @@ -23,7 +23,7 @@ public class ImageSharpMiddlewareOptions context.Context.Request.Path, QueryString.Create(context.Commands)); - return Task.FromResult(HMACUtilities.ComputeHMACSHA256(uri, secret)); + return HMACUtilities.ComputeHMACSHA256(uri, secret); }; private Func onParseCommandsAsync = _ => Task.CompletedTask; @@ -88,14 +88,14 @@ public class ImageSharpMiddlewareOptions /// Defaults to using an invariant lowercase relative Uri /// generated using . /// - public Func> OnComputeHMACAsync + public Func OnComputeHMAC { - get => this.onComputeHMACAsync; + get => this.onComputeHMAC; set { - Guard.NotNull(value, nameof(this.onComputeHMACAsync)); - this.onComputeHMACAsync = value; + Guard.NotNull(value, nameof(this.onComputeHMAC)); + this.onComputeHMAC = value; } } diff --git a/src/ImageSharp.Web/RequestAuthorizationUtilities.cs b/src/ImageSharp.Web/RequestAuthorizationUtilities.cs index 6c4849bc..970a2e89 100644 --- a/src/ImageSharp.Web/RequestAuthorizationUtilities.cs +++ b/src/ImageSharp.Web/RequestAuthorizationUtilities.cs @@ -98,15 +98,6 @@ public void StripUnknownCommands(CommandCollection commands) public string? ComputeHMAC(string uri, CommandHandling handling) => this.ComputeHMAC(new Uri(uri, UriKind.RelativeOrAbsolute), handling); - /// - /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. - /// - /// The uri to compute the code from. - /// The command collection handling. - /// The computed HMAC. - public Task ComputeHMACAsync(string uri, CommandHandling handling) - => this.ComputeHMACAsync(new Uri(uri, UriKind.RelativeOrAbsolute), handling); - /// /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. /// @@ -124,23 +115,6 @@ public void StripUnknownCommands(CommandCollection commands) return this.ComputeHMAC(host, path, queryString, handling); } - /// - /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. - /// - /// The uri to compute the code from. - /// The command collection handling. - /// The computed HMAC. - public Task ComputeHMACAsync(Uri uri, CommandHandling handling) - { - ToComponents( - uri, - out HostString host, - out PathString path, - out QueryString queryString); - - return this.ComputeHMACAsync(host, path, queryString, handling); - } - /// /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. /// @@ -152,17 +126,6 @@ public void StripUnknownCommands(CommandCollection commands) public string? ComputeHMAC(HostString host, PathString path, QueryString queryString, CommandHandling handling) => this.ComputeHMAC(host, path, queryString, new(QueryHelpers.ParseQuery(queryString.Value)), handling); - /// - /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. - /// - /// The host header. - /// The path or pathbase. - /// The querystring. - /// The command collection handling. - /// The computed HMAC. - public Task ComputeHMACAsync(HostString host, PathString path, QueryString queryString, CommandHandling handling) - => this.ComputeHMACAsync(host, path, queryString, new(QueryHelpers.ParseQuery(queryString.Value)), handling); - /// /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. /// @@ -175,18 +138,6 @@ public void StripUnknownCommands(CommandCollection commands) public string? ComputeHMAC(HostString host, PathString path, QueryString queryString, QueryCollection query, CommandHandling handling) => this.ComputeHMAC(this.ToHttpContext(host, path, queryString, query), handling); - /// - /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. - /// - /// The host header. - /// The path or pathbase. - /// The querystring. - /// The query collection. - /// The command collection handling. - /// The computed HMAC. - public Task ComputeHMACAsync(HostString host, PathString path, QueryString queryString, QueryCollection query, CommandHandling handling) - => this.ComputeHMACAsync(this.ToHttpContext(host, path, queryString, query), handling); - /// /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. /// @@ -194,15 +145,6 @@ public void StripUnknownCommands(CommandCollection commands) /// The command collection handling. /// The computed HMAC. public string? ComputeHMAC(HttpContext context, CommandHandling handling) - => AsyncHelper.RunSync(() => this.ComputeHMACAsync(context, handling)); - - /// - /// Compute a Hash-based Message Authentication Code (HMAC) for request authentication. - /// - /// The request HTTP context. - /// The command collection handling. - /// The computed HMAC. - public async Task ComputeHMACAsync(HttpContext context, CommandHandling handling) { byte[] secret = this.options.HMACSecretKey; if (secret is null || secret.Length == 0) @@ -222,7 +164,7 @@ public void StripUnknownCommands(CommandCollection commands) } ImageCommandContext imageCommandContext = new(context, commands, this.commandParser, this.parserCulture); - return await this.options.OnComputeHMACAsync(imageCommandContext, secret); + return this.options.OnComputeHMAC(imageCommandContext, secret); } /// @@ -234,14 +176,14 @@ public void StripUnknownCommands(CommandCollection commands) /// /// Contains information about the current image request and parsed commands. /// The computed HMAC. - internal async Task ComputeHMACAsync(ImageCommandContext context) + internal string? ComputeHMAC(ImageCommandContext context) { if (context.Commands.Count == 0) { return null; } - return await this.options.OnComputeHMACAsync(context, this.options.HMACSecretKey); + return this.options.OnComputeHMAC(context, this.options.HMACSecretKey); } private static void ToComponents( diff --git a/src/ImageSharp.Web/AsyncHelper.cs b/tests/ImageSharp.Web.Tests/TestUtilities/AsyncHelper.cs similarity index 91% rename from src/ImageSharp.Web/AsyncHelper.cs rename to tests/ImageSharp.Web.Tests/TestUtilities/AsyncHelper.cs index 3ba68fcb..3f62a600 100644 --- a/src/ImageSharp.Web/AsyncHelper.cs +++ b/tests/ImageSharp.Web.Tests/TestUtilities/AsyncHelper.cs @@ -3,7 +3,7 @@ using System.Globalization; -namespace SixLabors.ImageSharp.Web; +namespace SixLabors.ImageSharp.Web.Tests.TestUtilities; /// /// @@ -20,7 +20,7 @@ private static readonly TaskFactory TaskFactory /// /// Executes an async method synchronously. /// - /// The task to excecute. + /// The task to execute. public static void RunSync(Func task) { CultureInfo cultureUi = CultureInfo.CurrentUICulture; @@ -38,7 +38,7 @@ public static void RunSync(Func task) /// a return type synchronously. /// /// The type of result to return. - /// The task to excecute. + /// The task to execute. /// The . public static TResult RunSync(Func> task) { diff --git a/tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedServerTestBase.cs b/tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedServerTestBase.cs index 6ecc4ad6..46140b7d 100644 --- a/tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedServerTestBase.cs +++ b/tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedServerTestBase.cs @@ -11,7 +11,7 @@ public abstract class AuthenticatedServerTestBase : ServerTestBase(); - this.relativeImageSouce = this.ImageSource.Replace("http://localhost", string.Empty); + this.relativeImageSource = this.ImageSource.Replace("http://localhost", string.Empty); } [Fact] @@ -40,19 +40,17 @@ public async Task CanRejectUnauthorizedRequestAsync() Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } - protected override async Task AugmentCommandAsync(string command) + protected override string AugmentCommand(string command) { - string uri = this.relativeImageSouce + command; - string token = await this.GetTokenAsync(uri); + string uri = this.relativeImageSource + command; + string token = this.GetToken(uri); return command + "&" + RequestAuthorizationUtilities.TokenCommand + "=" + token; } - private async Task GetTokenAsync(string uri) + private string GetToken(string uri) { string tokenSync = this.authorizationUtilities.ComputeHMAC(uri, CommandHandling.Sanitize); - string tokenAsync = await this.authorizationUtilities.ComputeHMACAsync(uri, CommandHandling.Sanitize); - - Assert.Equal(tokenSync, tokenAsync); + Assert.NotNull(tokenSync); return tokenSync; } } diff --git a/tests/ImageSharp.Web.Tests/TestUtilities/ServerTestBase.cs b/tests/ImageSharp.Web.Tests/TestUtilities/ServerTestBase.cs index 6d8b5e83..e4e03512 100644 --- a/tests/ImageSharp.Web.Tests/TestUtilities/ServerTestBase.cs +++ b/tests/ImageSharp.Web.Tests/TestUtilities/ServerTestBase.cs @@ -45,7 +45,7 @@ public async Task CanProcessAndResolveImageAsync() Assert.True(Configuration.Default.ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format)); // First response - HttpResponseMessage response = await this.HttpClient.GetAsync(url + await this.AugmentCommandAsync(this.Fixture.Commands[0])); + HttpResponseMessage response = await this.HttpClient.GetAsync(url + this.AugmentCommand(this.Fixture.Commands[0])); Assert.NotNull(response); Assert.True(response.IsSuccessStatusCode); @@ -60,7 +60,7 @@ public async Task CanProcessAndResolveImageAsync() response.Dispose(); // Cached Response - response = await this.HttpClient.GetAsync(url + await this.AugmentCommandAsync(this.Fixture.Commands[0])); + response = await this.HttpClient.GetAsync(url + this.AugmentCommand(this.Fixture.Commands[0])); Assert.NotNull(response); Assert.True(response.IsSuccessStatusCode); @@ -77,7 +77,7 @@ public async Task CanProcessAndResolveImageAsync() // 304 response HttpRequestMessage request = new() { - RequestUri = new Uri(url + await this.AugmentCommandAsync(this.Fixture.Commands[0])), + RequestUri = new Uri(url + this.AugmentCommand(this.Fixture.Commands[0])), Method = HttpMethod.Get, }; @@ -100,7 +100,7 @@ public async Task CanProcessAndResolveImageAsync() // 412 response request = new HttpRequestMessage { - RequestUri = new Uri(url + await this.AugmentCommandAsync(this.Fixture.Commands[0])), + RequestUri = new Uri(url + this.AugmentCommand(this.Fixture.Commands[0])), Method = HttpMethod.Get, }; @@ -119,8 +119,8 @@ public async Task CanProcessAndResolveImageAsync() public async Task CanProcessMultipleIdenticalQueriesAsync() { string url = this.ImageSource; - string command1 = await this.AugmentCommandAsync(this.Fixture.Commands[0]); - string command2 = await this.AugmentCommandAsync(this.Fixture.Commands[1]); + string command1 = this.AugmentCommand(this.Fixture.Commands[0]); + string command2 = this.AugmentCommand(this.Fixture.Commands[1]); Task[] tasks = Enumerable.Range(0, 100).Select(i => Task.Run(async () => { @@ -136,5 +136,5 @@ public async Task CanProcessMultipleIdenticalQueriesAsync() Assert.True(all.IsCompletedSuccessfully); } - protected virtual Task AugmentCommandAsync(string command) => Task.FromResult(command); + protected virtual string AugmentCommand(string command) => command; }