diff --git a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs index 07cb927..c27b78d 100644 --- a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs +++ b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs @@ -174,6 +174,90 @@ public class UsersApiWithRealTokensTests : IAsyncLifetime (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().AuthenticateAsync(email, "Password1!"); + return (r.AccessToken, r.RefreshToken); + } + + private async Task RefreshAsync(string refreshToken) + { + using var scope = _host.Services.CreateScope(); + return await scope.ServiceProvider.GetRequiredService().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 [Fact] diff --git a/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs b/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs index b9d6c70..0c9eea6 100644 --- a/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs +++ b/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs @@ -258,7 +258,14 @@ public class UsersController : ControllerBase await _userManager.AddToRoleAsync(target, 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); + } return Ok(); }