This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
DefaultForbid/SignOutSchemes + tests #870
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationSchemeProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Authentication | ||
{ | ||
public class AuthenticationSchemeProviderTests | ||
{ | ||
[Fact] | ||
public async Task DefaultSignOutFallsbackToSignIn() | ||
{ | ||
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o => | ||
{ | ||
o.AddScheme<Handler>("signin", "whatever"); | ||
o.AddScheme<Handler>("foobly", "whatever"); | ||
o.DefaultSignInScheme = "signin"; | ||
}).BuildServiceProvider(); | ||
|
||
var provider = services.GetRequiredService<IAuthenticationSchemeProvider>(); | ||
var scheme = await provider.GetDefaultSignOutSchemeAsync(); | ||
Assert.NotNull(scheme); | ||
Assert.Equal("signin", scheme.Name); | ||
} | ||
|
||
[Fact] | ||
public async Task DefaultForbidFallsbackToChallenge() | ||
{ | ||
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o => | ||
{ | ||
o.AddScheme<Handler>("challenge", "whatever"); | ||
o.AddScheme<Handler>("foobly", "whatever"); | ||
o.DefaultChallengeScheme = "challenge"; | ||
}).BuildServiceProvider(); | ||
|
||
var provider = services.GetRequiredService<IAuthenticationSchemeProvider>(); | ||
var scheme = await provider.GetDefaultForbidSchemeAsync(); | ||
Assert.NotNull(scheme); | ||
Assert.Equal("challenge", scheme.Name); | ||
} | ||
|
||
[Fact] | ||
public async Task DefaultSchemesFallbackToOnlyScheme() | ||
{ | ||
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o => | ||
{ | ||
o.AddScheme<Handler>("single", "whatever"); | ||
}).BuildServiceProvider(); | ||
|
||
var provider = services.GetRequiredService<IAuthenticationSchemeProvider>(); | ||
Assert.Equal("single", (await provider.GetDefaultForbidSchemeAsync()).Name); | ||
Assert.Equal("single", (await provider.GetDefaultAuthenticateSchemeAsync()).Name); | ||
Assert.Equal("single", (await provider.GetDefaultChallengeSchemeAsync()).Name); | ||
Assert.Equal("single", (await provider.GetDefaultSignInSchemeAsync()).Name); | ||
Assert.Equal("single", (await provider.GetDefaultSignOutSchemeAsync()).Name); | ||
} | ||
|
||
[Fact] | ||
public async Task DefaultSchemesAreSet() | ||
{ | ||
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o => | ||
{ | ||
o.AddScheme<Handler>("A", "whatever"); | ||
o.AddScheme<Handler>("B", "whatever"); | ||
o.AddScheme<Handler>("C", "whatever"); | ||
o.DefaultChallengeScheme = "A"; | ||
o.DefaultForbidScheme = "B"; | ||
o.DefaultSignInScheme = "C"; | ||
o.DefaultSignOutScheme = "A"; | ||
o.DefaultAuthenticateScheme = "C"; | ||
}).BuildServiceProvider(); | ||
|
||
var provider = services.GetRequiredService<IAuthenticationSchemeProvider>(); | ||
Assert.Equal("B", (await provider.GetDefaultForbidSchemeAsync()).Name); | ||
Assert.Equal("C", (await provider.GetDefaultAuthenticateSchemeAsync()).Name); | ||
Assert.Equal("A", (await provider.GetDefaultChallengeSchemeAsync()).Name); | ||
Assert.Equal("C", (await provider.GetDefaultSignInSchemeAsync()).Name); | ||
Assert.Equal("A", (await provider.GetDefaultSignOutSchemeAsync()).Name); | ||
} | ||
|
||
private class Handler : IAuthenticationHandler | ||
{ | ||
public Task<AuthenticateResult> AuthenticateAsync() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task ChallengeAsync(AuthenticationProperties properties) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task ForbidAsync(AuthenticationProperties properties) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task SignOutAsync(AuthenticationProperties properties) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't look like this will be used outside of tests. No?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure it will, it's what end users could call to add handlers, its analogous to what hosting does: https://github.com/aspnet/IISIntegration/blob/dev/src/Microsoft.AspNetCore.Server.IISIntegration/IISMiddleware.cs#L53
except that's adding directly to the scheme provider rather than during startup/configure services.
Keep in mind, there's NOTHING on the builder, so the question I'm asking now is if we should nuke all of the
Action<AuthenticationSchemeBuilder>
... It was more useful when we had a bag on the builder that could be configured.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its just Name + DisplayName + Type, I guess its slightly more future proof with the action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But mainly I just wanted the sugar, we had a bug for this that got punted to 2.1, but since I'm here and adding tests :) aspnet/Security#1186