-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add default value for audience enum. LexAuthUser audience property is…
… now an enum instead of string. set audience requirement to include default audience unless it's exclusive. Fixes password reset api only being available for reset tokens and not normally logged in users.
- Loading branch information
Showing
8 changed files
with
53 additions
and
16 deletions.
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
17 changes: 10 additions & 7 deletions
17
backend/LexBoxApi/Auth/Requirements/AudienceRequirementHandler.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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using LexCore.Auth; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
|
||
namespace LexBoxApi.Auth.Requirements; | ||
|
||
public class AudienceRequirement : IAuthorizationRequirement | ||
{ | ||
public LexboxAudience Audience { get; } | ||
public LexboxAudience[] ValidAudiences { get; } | ||
|
||
public AudienceRequirement(LexboxAudience audience) | ||
public AudienceRequirement(params LexboxAudience[] validAudiences) | ||
{ | ||
Audience = audience; | ||
ValidAudiences = validAudiences; | ||
} | ||
} | ||
|
||
public class AudienceRequirementHandler : AuthorizationHandler<AudienceRequirement> | ||
{ | ||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AudienceRequirement requirement) | ||
{ | ||
var claim = context.User.FindFirst(JwtRegisteredClaimNames.Aud); | ||
if (claim?.Value == requirement.Audience.ToString()) | ||
var claim = context.User.FindFirst(LexAuthConstants.AudienceClaimType); | ||
if (Enum.TryParse<LexboxAudience>(claim?.Value, out var audience) && | ||
requirement.ValidAudiences.Contains(audience)) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
else | ||
{ | ||
context.Fail(new AuthorizationFailureReason(this, | ||
$"Token does not have the required audience: {requirement.Audience}")); | ||
$"Token does not have the required audience: {requirement.ValidAudiences}")); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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
10 changes: 8 additions & 2 deletions
10
backend/LexBoxApi/Auth/LexboxAudience.cs → backend/LexCore/Auth/LexboxAudience.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 |
---|---|---|
@@ -1,16 +1,22 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace LexBoxApi.Auth; | ||
namespace LexCore.Auth; | ||
|
||
[JsonConverter(typeof(JsonStringEnumConverter))] | ||
public enum LexboxAudience | ||
{ | ||
//default value of the enum, not a valid audience | ||
Unknown, | ||
//these names are converted to strings and are used in jwt tokens, if the name is changed that will invalidate all existing tokens | ||
LexboxApi, | ||
ForgotPassword, | ||
SendAndReceive, | ||
} | ||
|
||
public static class LexboxAudienceHelper | ||
{ | ||
public static string PolicyName(this LexboxAudience audience) => $"RequireAudience{audience}Policy"; | ||
public static string PolicyName(this LexboxAudience audience, bool exclusive) | ||
{ | ||
return $"RequireAudience{audience}{(exclusive ? "Exclusive" : "")}Policy"; | ||
} | ||
} |
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