Skip to content

Commit 34eb955

Browse files
committed
Quickly review the code to fix the build, anticipating that there will be failed tests
1 parent 96bc51c commit 34eb955

File tree

3 files changed

+15
-48
lines changed

3 files changed

+15
-48
lines changed

src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using Ocelot.Configuration;
44
using Ocelot.Logging;
55
using Ocelot.Middleware;
6-
using System.Runtime.Remoting.Contexts;
7-
using System.Threading.Tasks;
86

97
namespace Ocelot.Authentication.Middleware
108
{
@@ -38,7 +36,7 @@ public async Task Invoke(HttpContext httpContext)
3836

3937
if (result.Principal?.Identity == null)
4038
{
41-
await ChallengeAsync(httpContext, downstreamRoute);
39+
await ChallengeAsync(httpContext, downstreamRoute, result);
4240
SetUnauthenticatedError(httpContext, path, null);
4341
return;
4442
}
@@ -52,7 +50,7 @@ public async Task Invoke(HttpContext httpContext)
5250
return;
5351
}
5452

55-
await ChallengeAsync(httpContext, downstreamRoute);
53+
await ChallengeAsync(httpContext, downstreamRoute, result);
5654
SetUnauthenticatedError(httpContext, path, httpContext.User.Identity.Name);
5755
}
5856

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

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

7169
// 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
7270
if (context.Response.Headers.TryGetValue("WWW-Authenticate", out var authenticateHeader))

src/Ocelot/Responder/HttpContextResponder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Microsoft.Extensions.Primitives;
44
using Ocelot.Headers;
55
using Ocelot.Middleware;
6-
using System.Runtime.Remoting.Messaging;
76

87
namespace Ocelot.Responder;
98

@@ -79,9 +78,7 @@ public async Task SetErrorResponseOnContext(HttpContext context, DownstreamRespo
7978
}
8079

8180
public void SetAuthChallengeOnContext(HttpContext context, string challenge)
82-
{
83-
AddHeaderIfDoesntExist(context, new Header("WWW-Authenticate", new[] { challenge }));
84-
}
81+
=> AddHeaderIfDoesntExist(context, new Header("WWW-Authenticate", new[] { challenge }));
8582

8683
private static void SetStatusCode(HttpContext context, int statusCode)
8784
{

test/Ocelot.AcceptanceTests/Authentication/AuthenticationTests.cs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using IdentityServer4.Models;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.Http;
5-
using Ocelot.Configuration.File;
6-
using System.Net.Http;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Ocelot.DependencyInjection;
77

88
namespace Ocelot.AcceptanceTests.Authentication
99
{
@@ -129,48 +129,20 @@ public void Should_return_www_authenticate_header_on_401()
129129
.And(x => ThenTheResponseShouldContainAuthChallenge())
130130
.BDDfy();
131131
}
132-
133-
public void GivenOcelotIsRunningWithJwtAuth(string authenticationProviderKey)
132+
private void GivenOcelotIsRunningWithJwtAuth(string authenticationProviderKey)
134133
{
135-
var builder = new ConfigurationBuilder()
136-
.SetBasePath(Directory.GetCurrentDirectory())
137-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
138-
.AddJsonFile("ocelot.json", false, false)
139-
.AddEnvironmentVariables();
140-
141-
var configuration = builder.Build();
142-
_webHostBuilder = new WebHostBuilder();
143-
_webHostBuilder.ConfigureServices(s =>
134+
GivenOcelotIsRunningWithServices(WithJwtBearer);
135+
void WithJwtBearer(IServiceCollection s)
144136
{
145-
s.AddSingleton(_webHostBuilder);
146-
});
147-
148-
_ocelotServer = new TestServer(_webHostBuilder
149-
.UseConfiguration(configuration)
150-
.ConfigureServices(s =>
151-
{
152-
s.AddAuthentication().AddJwtBearer(authenticationProviderKey, options =>
153-
{
154-
});
155-
s.AddOcelot(configuration);
156-
})
157-
.ConfigureLogging(l =>
158-
{
159-
l.AddConsole();
160-
l.AddDebug();
161-
})
162-
.Configure(a =>
163-
{
164-
a.UseOcelot().Wait();
165-
}));
166-
167-
_ocelotClient = _ocelotServer.CreateClient();
137+
s.AddAuthentication().AddJwtBearer(authenticationProviderKey, options => { });
138+
s.AddOcelot();
139+
}
168140
}
169-
public void GivenIHaveNoTokenForMyRequest()
141+
private void GivenIHaveNoTokenForMyRequest()
170142
{
171143
_ocelotClient.DefaultRequestHeaders.Authorization = null;
172144
}
173-
public void ThenTheResponseShouldContainAuthChallenge()
145+
private void ThenTheResponseShouldContainAuthChallenge()
174146
{
175147
_response.Headers.TryGetValues("WWW-Authenticate", out var headerValue).ShouldBeTrue();
176148
headerValue.ShouldNotBeEmpty();

0 commit comments

Comments
 (0)