Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit e12838e

Browse files
committed
Auth: Always call prior handlers during Challenge
1 parent 22d2fe9 commit e12838e

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ protected virtual Task HandleSignOutAsync(SignOutContext context)
311311
return TaskCache.CompletedTask;
312312
}
313313

314+
/// <summary>
315+
/// Override this method to deal with a challenge that is forbidden.
316+
/// </summary>
317+
/// <param name="context"></param>
318+
/// <returns>The returned boolean is ignored.</returns>
314319
protected virtual Task<bool> HandleForbiddenAsync(ChallengeContext context)
315320
{
316321
Response.StatusCode = 403;
@@ -323,7 +328,7 @@ protected virtual Task<bool> HandleForbiddenAsync(ChallengeContext context)
323328
/// changing the 401 result to 302 of a login page or external sign-in location.)
324329
/// </summary>
325330
/// <param name="context"></param>
326-
/// <returns>True if no other handlers should be called</returns>
331+
/// <returns>The returned boolean is no longer used.</returns>
327332
protected virtual Task<bool> HandleUnauthorizedAsync(ChallengeContext context)
328333
{
329334
Response.StatusCode = 401;
@@ -333,7 +338,6 @@ protected virtual Task<bool> HandleUnauthorizedAsync(ChallengeContext context)
333338
public async Task ChallengeAsync(ChallengeContext context)
334339
{
335340
ChallengeCalled = true;
336-
var handled = false;
337341
if (ShouldHandleScheme(context.AuthenticationScheme, Options.AutomaticChallenge))
338342
{
339343
switch (context.Behavior)
@@ -347,18 +351,18 @@ public async Task ChallengeAsync(ChallengeContext context)
347351
}
348352
goto case ChallengeBehavior.Unauthorized;
349353
case ChallengeBehavior.Unauthorized:
350-
handled = await HandleUnauthorizedAsync(context);
354+
await HandleUnauthorizedAsync(context);
351355
Logger.AuthenticationSchemeChallenged(Options.AuthenticationScheme);
352356
break;
353357
case ChallengeBehavior.Forbidden:
354-
handled = await HandleForbiddenAsync(context);
358+
await HandleForbiddenAsync(context);
355359
Logger.AuthenticationSchemeForbidden(Options.AuthenticationScheme);
356360
break;
357361
}
358362
context.Accept();
359363
}
360364

361-
if (!handled && PriorHandler != null)
365+
if (PriorHandler != null)
362366
{
363367
await PriorHandler.ChallengeAsync(context);
364368
}

test/Microsoft.AspNetCore.Authentication.Test/AuthenticationHandlerFacts.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ public async Task AuthHandlerAuthenticateCachesTicket(string scheme)
7575
Assert.Equal(1, handler.AuthCount);
7676
}
7777

78+
// Prior to https://github.com/aspnet/Security/issues/930 we wouldn't call prior if handled
79+
[Fact]
80+
public async Task AuthHandlerChallengeAlwaysCallsPriorHandler()
81+
{
82+
var handler = await TestHandler.Create("Alpha");
83+
var previous = new PreviousHandler();
84+
85+
handler.PriorHandler = previous;
86+
await handler.ChallengeAsync(new ChallengeContext("Alpha"));
87+
Assert.True(previous.ChallengeCalled);
88+
}
89+
90+
private class PreviousHandler : IAuthenticationHandler
91+
{
92+
public bool ChallengeCalled = false;
93+
94+
public Task AuthenticateAsync(AuthenticateContext context)
95+
{
96+
throw new NotImplementedException();
97+
}
98+
99+
public Task ChallengeAsync(ChallengeContext context)
100+
{
101+
ChallengeCalled = true;
102+
return Task.FromResult(0);
103+
}
104+
105+
public void GetDescriptions(DescribeSchemesContext context)
106+
{
107+
throw new NotImplementedException();
108+
}
109+
110+
public Task SignInAsync(SignInContext context)
111+
{
112+
throw new NotImplementedException();
113+
}
114+
115+
public Task SignOutAsync(SignOutContext context)
116+
{
117+
throw new NotImplementedException();
118+
}
119+
}
120+
78121
private class CountOptions : AuthenticationOptions { }
79122

80123
private class CountHandler : AuthenticationHandler<CountOptions>
@@ -109,6 +152,8 @@ private class TestHandler : AuthenticationHandler<TestOptions>
109152
{
110153
private TestHandler() { }
111154

155+
public AuthenticateResult Result = AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(), new AuthenticationProperties(), "whatever"));
156+
112157
public static async Task<TestHandler> Create(string scheme)
113158
{
114159
var handler = new TestHandler();
@@ -124,7 +169,7 @@ await handler.InitializeAsync(
124169

125170
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
126171
{
127-
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(), new AuthenticationProperties(), "whatever")));
172+
return Task.FromResult(Result);
128173
}
129174
}
130175

@@ -220,7 +265,6 @@ public int StatusCode
220265

221266
set
222267
{
223-
throw new NotImplementedException();
224268
}
225269
}
226270

0 commit comments

Comments
 (0)