Skip to content

Commit

Permalink
Revert "fix bug where tokens generated from LexAuthService would cont…
Browse files Browse the repository at this point in the history
…ain 2 audiences and then be unable to deserialize into a LexAuthUser"

This reverts commit 319af51.
  • Loading branch information
myieye committed Oct 27, 2023
1 parent 7a7d781 commit cb0a5b0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
2 changes: 1 addition & 1 deletion backend/LexBoxApi/Auth/LexAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ await context.SignInAsync(jwtUser.GetPrincipal("Refresh"),
var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
var id = Guid.NewGuid().ToString().GetHashCode().ToString("x", CultureInfo.InvariantCulture);
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Jti, id));
identity.AddClaims(user.GetClaims().Where(c => c.Type != JwtRegisteredClaimNames.Aud));
identity.AddClaims(user.GetClaims());
var handler = new JwtSecurityTokenHandler();
var jwt = handler.CreateJwtSecurityToken(
audience: audience.ToString(),
Expand Down
23 changes: 4 additions & 19 deletions backend/Testing/LexCore/LexAuthUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@
using LexCore.Entities;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Shouldly;

namespace Testing.LexCore;

public class LexAuthUserTests
{
private readonly LexAuthService _lexAuthService = new LexAuthService(
new OptionsWrapper<JwtOptions>(JwtOptions.TestingOptions),
null,
null,
null);

private readonly LexAuthUser _user = new()
{
Id = Guid.NewGuid(),
Email = "[email protected]",
Role = UserRole.user,
Name = "test",
Projects = new[] { new AuthUserProject("test-flex", ProjectRole.Manager, Guid.NewGuid()) }
Projects = new[]
{
new AuthUserProject("test-flex", ProjectRole.Manager, Guid.NewGuid())
}
};

[Fact]
Expand Down Expand Up @@ -86,15 +82,4 @@ public void CanRoundTripClaimsWhenUsingSecurityTokenDescriptor()
var newUser = JsonSerializer.Deserialize<LexAuthUser>(Base64UrlEncoder.Decode(token.RawPayload));
_user.ShouldBeEquivalentTo(newUser);
}

[Fact]
public void CanRoundTripJwtFromUserThroughLexAuthService()
{
var (jwt, _) = _lexAuthService.GenerateJwt(_user);
var tokenHandler = new JwtSecurityTokenHandler();
var outputJwt = tokenHandler.ReadJwtToken(jwt);
var principal = new ClaimsPrincipal(new ClaimsIdentity(outputJwt.Claims, "Testing"));
var newUser = LexAuthUser.FromClaimsPrincipal(principal);
_user.ShouldBeEquivalentTo(newUser);
}
}

0 comments on commit cb0a5b0

Please sign in to comment.