-
Notifications
You must be signed in to change notification settings - Fork 11
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
Step up v2 #135
Merged
+403
−105
Merged
Step up v2 #135
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5a99f13
Step up v1 with multiple contexts
jrmccannon d582fca
Consolidated requirements. Added consts for the policy names.
jrmccannon 65c1802
Switched to the v2 of step up.
jrmccannon f78c017
Allow for strings to be sent for signin purpose.
jrmccannon 1c8e6d6
Merge branch 'refs/heads/main' into step-up-v2
jrmccannon 345fa05
Using code from updated client library
jrmccannon 08869f8
Rename to step up purpose
jrmccannon 14530f9
Passing through purpose and adding it to the step up text
jrmccannon b114e55
Added Purpose to verified user.
jrmccannon dd16c43
Updating recovery to use Magic Links
jrmccannon 603c15d
Added way to validate what token is being validated by showing claims
jrmccannon 1a150f2
formatting. updating step up method.
jrmccannon 53d8f5e
Added new testing ability for manually generated tokens and magic lin…
jrmccannon 491982c
Merge branch 'main' into step-up-v2
jrmccannon 117ba31
Removed copy pasted code and using cdn mjs.
jrmccannon 16a83a0
Merge remote-tracking branch 'origin/step-up-v2' into step-up-v2
jrmccannon bed9f98
Registered generic VerifiedUser to use in tests
jrmccannon 71bf3a6
formatting.
jrmccannon 118e9bd
Removed testing items and changed auth policy to use default step up …
jrmccannon 9d296b2
Used TimeProvider
jrmccannon 3979bf2
Removed nullability
jrmccannon e1dd0ad
formatting
jrmccannon 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
50 changes: 50 additions & 0 deletions
50
examples/Passwordless.AspNetIdentity.Example/Authorization/StepUpAuthorizationHandler.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,50 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Passwordless.AspNetIdentity.Example.Authorization; | ||
|
||
public interface IStepUpAuthorizationRequirement : IAuthorizationRequirement | ||
{ | ||
public string Name { get; } | ||
} | ||
|
||
public class StepUpAuthorizationHandler(StepUpPurpose stepUpPurpose, TimeProvider timeProvider) : AuthorizationHandler<IStepUpAuthorizationRequirement> | ||
{ | ||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IStepUpAuthorizationRequirement requirement) | ||
{ | ||
if (context.User.Identity is not { IsAuthenticated: true }) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
if (context.User.HasClaim(MatchesClaim(requirement)) | ||
&& IsExpired(GetExpiration(context.User.FindFirst(MatchesClaim(requirement))!))) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
else | ||
{ | ||
stepUpPurpose.Purpose = requirement.Name; | ||
context.Fail(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private static Predicate<Claim> MatchesClaim(IStepUpAuthorizationRequirement requirement) => claim => claim.Type == requirement.Name; | ||
|
||
private bool IsExpired(DateTime expiration) | ||
{ | ||
return expiration > timeProvider.GetUtcNow().DateTime; | ||
} | ||
|
||
private static DateTime GetExpiration(Claim claim) | ||
{ | ||
var expiration = DateTime.Parse(claim.Value, null, DateTimeStyles.RoundtripKind); | ||
|
||
return expiration; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/Passwordless.AspNetIdentity.Example/Authorization/StepUpPurpose.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,6 @@ | ||
namespace Passwordless.AspNetIdentity.Example.Authorization; | ||
|
||
public class StepUpPurpose | ||
{ | ||
public string Purpose { get; set; } = string.Empty; | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/Passwordless.AspNetIdentity.Example/Authorization/StepUpPurposes.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,6 @@ | ||
namespace Passwordless.AspNetIdentity.Example.Authorization; | ||
|
||
public static class StepUpPurposes | ||
{ | ||
public const string StepUp = "step-up"; | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/Passwordless.AspNetIdentity.Example/Authorization/StepUpRequirement.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,6 @@ | ||
namespace Passwordless.AspNetIdentity.Example.Authorization; | ||
|
||
public class StepUpRequirement(string policyName) : IStepUpAuthorizationRequirement | ||
{ | ||
public string Name => policyName; | ||
} |
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
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
9 changes: 9 additions & 0 deletions
9
examples/Passwordless.AspNetIdentity.Example/Pages/Authorized/ElevatedAuthentication.cshtml
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,9 @@ | ||
@page | ||
@model Passwordless.AspNetIdentity.Example.Pages.Authorized.ElevatedAuthentication | ||
|
||
@{ | ||
ViewData["Title"] = "Elevated Auth"; | ||
} | ||
<h1>@ViewData["Title"]</h1> | ||
|
||
<p>Step up authentication successful.</p> |
13 changes: 13 additions & 0 deletions
13
...les/Passwordless.AspNetIdentity.Example/Pages/Authorized/ElevatedAuthentication.cshtml.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,13 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Passwordless.AspNetIdentity.Example.Authorization; | ||
|
||
namespace Passwordless.AspNetIdentity.Example.Pages.Authorized; | ||
|
||
[Authorize(Policy = StepUpPurposes.StepUp)] | ||
public class ElevatedAuthentication : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Might not be correct, usually you want a specific permission here that is required, and do the step up procedure separately to add the claim to your token which then validates afterwards if you have a specific claim I think.