Rolwijziging beëindigt het lopende access token van de betrokken gebruiker (security stamp), refresh blijft werken
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -174,6 +174,90 @@ public class UsersApiWithRealTokensTests : IAsyncLifetime
|
|||||||
(await RoleOfAsync(user.Id)).Should().Be("User");
|
(await RoleOfAsync(user.Id)).Should().Be("User");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- a role change ends the access token of the person concerned (and only theirs)
|
||||||
|
|
||||||
|
private async Task<(string Access, string Refresh)> LoginAsync(string email)
|
||||||
|
{
|
||||||
|
using var scope = _host.Services.CreateScope();
|
||||||
|
var r = await scope.ServiceProvider.GetRequiredService<IAuthService>().AuthenticateAsync(email, "Password1!");
|
||||||
|
return (r.AccessToken, r.RefreshToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<TokenResponse> RefreshAsync(string refreshToken)
|
||||||
|
{
|
||||||
|
using var scope = _host.Services.CreateScope();
|
||||||
|
return await scope.ServiceProvider.GetRequiredService<IAuthService>().RefreshTokenAsync(refreshToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string RoleClaimOf(string accessToken) =>
|
||||||
|
new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler().ReadJwtToken(accessToken).Claims
|
||||||
|
.First(c => c.Type is "role" or System.Security.Claims.ClaimTypes.Role).Value;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AfterARoleChange_TheOldTokenIsRefused_TheRefreshGivesTheNewRole_AndOthersKeepTheirs()
|
||||||
|
{
|
||||||
|
var andrea = await AddUserAsync("andrea@test.nl", "Owner");
|
||||||
|
var bryan = await AddUserAsync("bryan@test.nl", "Administrator");
|
||||||
|
await AddUserAsync("other@test.nl", "Administrator");
|
||||||
|
var owner = await LoginAsync("andrea@test.nl");
|
||||||
|
var changed = await LoginAsync("bryan@test.nl");
|
||||||
|
var other = await LoginAsync("other@test.nl");
|
||||||
|
(await SendAsync(changed.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
|
(await ChangeRoleAsync(owner.Access, bryan.Id, "User")).Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
|
(await SendAsync(changed.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.Unauthorized); // old token: refused at once
|
||||||
|
var renewed = await RefreshAsync(changed.Refresh); // refresh token still works
|
||||||
|
RoleClaimOf(renewed.AccessToken).Should().Be("User");
|
||||||
|
(await SendAsync(renewed.AccessToken, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.Forbidden); // new role, no longer allowed
|
||||||
|
(await SendAsync(owner.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK); // the one who changed it is not signed out
|
||||||
|
(await SendAsync(other.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK); // nor is anybody else
|
||||||
|
andrea.Id.Should().NotBe(Guid.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task APromotion_AlsoRenewsTheTokenWithoutAHardSignOut()
|
||||||
|
{
|
||||||
|
await AddUserAsync("andrea@test.nl", "Owner");
|
||||||
|
var user = await AddUserAsync("user@test.nl", "User");
|
||||||
|
var owner = await LoginAsync("andrea@test.nl");
|
||||||
|
var promoted = await LoginAsync("user@test.nl");
|
||||||
|
|
||||||
|
(await ChangeRoleAsync(owner.Access, user.Id, "Administrator")).Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
|
(await SendAsync(promoted.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.Unauthorized);
|
||||||
|
var renewed = await RefreshAsync(promoted.Refresh);
|
||||||
|
RoleClaimOf(renewed.AccessToken).Should().Be("Administrator");
|
||||||
|
(await SendAsync(renewed.AccessToken, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ChangingNothing_KeepsEveryTokenValid()
|
||||||
|
{
|
||||||
|
var andrea = await AddUserAsync("andrea@test.nl", "Owner");
|
||||||
|
var admin = await AddUserAsync("admin@test.nl", "Administrator");
|
||||||
|
var owner = await LoginAsync("andrea@test.nl");
|
||||||
|
var adminLogin = await LoginAsync("admin@test.nl");
|
||||||
|
|
||||||
|
(await ChangeRoleAsync(owner.Access, admin.Id, "Administrator")).Should().Be(HttpStatusCode.OK); // same role
|
||||||
|
(await ChangeRoleAsync(owner.Access, andrea.Id, "Owner")).Should().Be(HttpStatusCode.OK); // same role, self
|
||||||
|
|
||||||
|
(await SendAsync(adminLogin.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK);
|
||||||
|
(await SendAsync(owner.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ChangingYourOwnRole_EndsYourOwnToken_ButRefreshGivesYouTheNewOne()
|
||||||
|
{
|
||||||
|
var admin = await AddUserAsync("admin@test.nl", "Administrator");
|
||||||
|
var login = await LoginAsync("admin@test.nl");
|
||||||
|
|
||||||
|
(await ChangeRoleAsync(login.Access, admin.Id, "User")).Should().Be(HttpStatusCode.OK); // self-demotion is allowed
|
||||||
|
|
||||||
|
(await SendAsync(login.Access, HttpMethod.Get, "/Users")).Should().Be(HttpStatusCode.Unauthorized);
|
||||||
|
RoleClaimOf((await RefreshAsync(login.Refresh)).AccessToken).Should().Be("User");
|
||||||
|
}
|
||||||
|
|
||||||
// ---- regenerate-invite: the body is optional and the mail is only sent on request
|
// ---- regenerate-invite: the body is optional and the mail is only sent on request
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -258,7 +258,14 @@ public class UsersController : ControllerBase
|
|||||||
await _userManager.AddToRoleAsync(target, request.NewRole);
|
await _userManager.AddToRoleAsync(target, request.NewRole);
|
||||||
|
|
||||||
if (targetRole != request.NewRole)
|
if (targetRole != request.NewRole)
|
||||||
|
{
|
||||||
|
// A new security stamp ends the access tokens issued before the change (they carry the old role); the
|
||||||
|
// refresh token is deliberately left alone, so the client silently gets a new token with the new role.
|
||||||
|
// Only the stamp of the person whose role changed moves: everybody else, including the administrator
|
||||||
|
// who made the change, keeps their session.
|
||||||
|
await _userManager.UpdateSecurityStampAsync(target);
|
||||||
await _mail.SendRoleChangedAsync(target, request.NewRole, HttpContext.RequestAborted);
|
await _mail.SendRoleChangedAsync(target, request.NewRole, HttpContext.RequestAborted);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user