Skip to content

Commit

Permalink
Quickly review the code to fix the build, anticipating that there wil…
Browse files Browse the repository at this point in the history
…l be failed tests
  • Loading branch information
raman-m committed Nov 7, 2024
1 parent 96bc51c commit 34eb955
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
10 changes: 4 additions & 6 deletions src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Ocelot.Configuration;
using Ocelot.Logging;
using Ocelot.Middleware;
using System.Runtime.Remoting.Contexts;
using System.Threading.Tasks;

namespace Ocelot.Authentication.Middleware
{
Expand Down Expand Up @@ -38,7 +36,7 @@ public async Task Invoke(HttpContext httpContext)

if (result.Principal?.Identity == null)
{
await ChallengeAsync(httpContext, downstreamRoute);
await ChallengeAsync(httpContext, downstreamRoute, result);
SetUnauthenticatedError(httpContext, path, null);
return;
}
Expand All @@ -52,7 +50,7 @@ public async Task Invoke(HttpContext httpContext)
return;
}

await ChallengeAsync(httpContext, downstreamRoute);
await ChallengeAsync(httpContext, downstreamRoute, result);
SetUnauthenticatedError(httpContext, path, httpContext.User.Identity.Name);
}

Expand All @@ -63,10 +61,10 @@ private void SetUnauthenticatedError(HttpContext httpContext, string path, strin
httpContext.Items.SetError(error);
}

private async Task ChallengeAsync(HttpContext context, DownstreamRoute route)
private async Task ChallengeAsync(HttpContext context, DownstreamRoute route, AuthenticateResult status)
{
// Perform a challenge. This populates the WWW-Authenticate header on the response
await context.ChallengeAsync(route.AuthenticationOptions.AuthenticationProviderKey);
await context.ChallengeAsync(route.AuthenticationOptions.AuthenticationProviderKey); // TODO Read failed scheme from auth result

// Since the response gets re-created down the pipeline, we store the challenge in the Items, so we can re-apply it when sending the response
if (context.Response.Headers.TryGetValue("WWW-Authenticate", out var authenticateHeader))
Expand Down
5 changes: 1 addition & 4 deletions src/Ocelot/Responder/HttpContextResponder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Primitives;
using Ocelot.Headers;
using Ocelot.Middleware;
using System.Runtime.Remoting.Messaging;

namespace Ocelot.Responder;

Expand Down Expand Up @@ -79,9 +78,7 @@ public async Task SetErrorResponseOnContext(HttpContext context, DownstreamRespo
}

public void SetAuthChallengeOnContext(HttpContext context, string challenge)
{
AddHeaderIfDoesntExist(context, new Header("WWW-Authenticate", new[] { challenge }));
}
=> AddHeaderIfDoesntExist(context, new Header("WWW-Authenticate", new[] { challenge }));

private static void SetStatusCode(HttpContext context, int statusCode)
{
Expand Down
48 changes: 10 additions & 38 deletions test/Ocelot.AcceptanceTests/Authentication/AuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using IdentityServer4.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;

namespace Ocelot.AcceptanceTests.Authentication
{
Expand Down Expand Up @@ -129,48 +129,20 @@ public void Should_return_www_authenticate_header_on_401()
.And(x => ThenTheResponseShouldContainAuthChallenge())
.BDDfy();
}

public void GivenOcelotIsRunningWithJwtAuth(string authenticationProviderKey)
private void GivenOcelotIsRunningWithJwtAuth(string authenticationProviderKey)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile("ocelot.json", false, false)
.AddEnvironmentVariables();

var configuration = builder.Build();
_webHostBuilder = new WebHostBuilder();
_webHostBuilder.ConfigureServices(s =>
GivenOcelotIsRunningWithServices(WithJwtBearer);
void WithJwtBearer(IServiceCollection s)
{
s.AddSingleton(_webHostBuilder);
});

_ocelotServer = new TestServer(_webHostBuilder
.UseConfiguration(configuration)
.ConfigureServices(s =>
{
s.AddAuthentication().AddJwtBearer(authenticationProviderKey, options =>
{
});
s.AddOcelot(configuration);
})
.ConfigureLogging(l =>
{
l.AddConsole();
l.AddDebug();
})
.Configure(a =>
{
a.UseOcelot().Wait();
}));

_ocelotClient = _ocelotServer.CreateClient();
s.AddAuthentication().AddJwtBearer(authenticationProviderKey, options => { });
s.AddOcelot();
}
}
public void GivenIHaveNoTokenForMyRequest()
private void GivenIHaveNoTokenForMyRequest()
{
_ocelotClient.DefaultRequestHeaders.Authorization = null;
}
public void ThenTheResponseShouldContainAuthChallenge()
private void ThenTheResponseShouldContainAuthChallenge()
{
_response.Headers.TryGetValues("WWW-Authenticate", out var headerValue).ShouldBeTrue();
headerValue.ShouldNotBeEmpty();
Expand Down

0 comments on commit 34eb955

Please sign in to comment.