-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add requirement for denying guest users #2179
Open
h3rmanj
wants to merge
5
commits into
AzureAD:master
Choose a base branch
from
h3rmanj:deny-guests-requirement
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc60ed6
feat: add requirement for denying guest users
h3rmanj 8032da7
docs: add comments to handler
h3rmanj 69238c5
fix: don't succeed if iss is not present
h3rmanj 56ca8d6
docs: add xml comments to private and internal values
h3rmanj 1ae9fe3
tests: add deny guest authorization tests
loekensgard 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
53 changes: 53 additions & 0 deletions
53
src/Microsoft.Identity.Web/Policy/DenyGuestsAuthorizationHandler.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,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
internal class DenyGuestsAuthorizationsHandler : AuthorizationHandler<DenyGuestsAuthorizationRequirement> | ||
{ | ||
private const string Acct = "acct"; | ||
private const string TenantMember = "0"; | ||
|
||
private const string Iss = "iss"; | ||
|
||
protected override Task HandleRequirementAsync( | ||
AuthorizationHandlerContext context, | ||
DenyGuestsAuthorizationRequirement requirement) | ||
{ | ||
_ = Throws.IfNull(context); | ||
|
||
_ = Throws.IfNull(requirement); | ||
|
||
// acct is an optional claim | ||
// if it is present, it dictates wether the user is a guest or not | ||
var acct = context.User.FindFirstValue(Acct); | ||
|
||
if (!string.IsNullOrEmpty(acct)) | ||
{ | ||
if (acct == TenantMember) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
// if acct is not present | ||
// we can use the iss and idp claim to determine if the user is a guest | ||
var iss = context.User.FindFirstValue(Iss); | ||
var idp = context.User.GetIdentityProvider(); | ||
|
||
if (!string.IsNullOrEmpty(iss) && iss == idp) | ||
{ | ||
context.Succeed(requirement); | ||
return Task.CompletedTask; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Identity.Web/Policy/DenyGuestsAuthorizationRequirement.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,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Implements an <see cref="IAuthorizationRequirement"/> | ||
/// which requires the current user to be a member of the tenant. | ||
/// </summary> | ||
public class DenyGuestsAuthorizationRequirement : IAuthorizationRequirement | ||
{ | ||
} | ||
} |
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,29 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Extensions for building the deny guest requirement during application startup. | ||
/// </summary> | ||
public static class DenyGuestsExtensions | ||
{ | ||
/// <summary> | ||
/// This method adds support for the deny guests requirement. | ||
/// </summary> | ||
/// <param name="services">The services being configured.</param> | ||
/// <returns>Services.</returns> | ||
public static IServiceCollection AddDenyGuestsAuthorization(this IServiceCollection services) | ||
{ | ||
services.AddAuthorization(); | ||
|
||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IAuthorizationHandler, DenyGuestsAuthorizationsHandler>()); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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
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.
Not locked on the naming of this. I see many requirements are prefixed with
Require
, butRequireTenantMembers
orRequireNonGuests
sounds weird and isn't as explicit in what it tries to achieve.