Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
ForbidAsync now uses correct Schemes method (#918)
Browse files Browse the repository at this point in the history
* ForbidAsync now uses correct Schemes method

* comment

* adds tests
  • Loading branch information
jkotalik committed Sep 15, 2017
1 parent 7a1f70d commit ec3da99
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ public virtual async Task ForbidAsync(HttpContext context, string scheme, Authen
{
if (scheme == null)
{
var defaultChallengeScheme = await Schemes.GetDefaultChallengeSchemeAsync();
scheme = defaultChallengeScheme?.Name;
var defaultForbidScheme = await Schemes.GetDefaultForbidSchemeAsync();
scheme = defaultForbidScheme?.Name;
if (scheme == null)
{
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultChallengeScheme found.");
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultForbidScheme found.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ public async Task ServicesWithDefaultSignOutMethodsTest()
await Assert.ThrowsAsync<InvalidOperationException>(() => context.SignInAsync(new ClaimsPrincipal()));
}

[Fact]
public async Task ServicesWithDefaultForbidMethod_CallsForbidMethod()
{
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o =>
{
o.AddScheme<ForbidHandler>("forbid", "whatever");
o.DefaultForbidScheme = "forbid";
}).BuildServiceProvider();
var context = new DefaultHttpContext();
context.RequestServices = services;

await context.ForbidAsync();
}


private class BaseHandler : IAuthenticationHandler
{
Expand Down Expand Up @@ -245,5 +259,43 @@ public Task SignOutAsync(AuthenticationProperties properties)
}
}

private class ForbidHandler : IAuthenticationHandler, IAuthenticationRequestHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler
{
public Task<AuthenticateResult> AuthenticateAsync()
{
throw new NotImplementedException();
}

public Task ChallengeAsync(AuthenticationProperties properties)
{
throw new NotImplementedException();
}

public Task ForbidAsync(AuthenticationProperties properties)
{
return Task.FromResult(0);
}

public Task<bool> HandleRequestAsync()
{
throw new NotImplementedException();
}

public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
return Task.FromResult(0);
}

public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
{
throw new NotImplementedException();
}

public Task SignOutAsync(AuthenticationProperties properties)
{
throw new NotImplementedException();
}
}

}
}

0 comments on commit ec3da99

Please sign in to comment.