Harden sensitive account security verification
This commit is contained in:
1 parent
c816043537
commit
a2fd08fe7e
5 files changed
+147
-6
No files matched your search
@@ -0,0 +1,91 @@
|
||||
using System.Security.Claims;
|
||||
using Jiaowu.Api.Controllers;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Branding;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jiaowu.Api.Tests;
|
||||
|
||||
public sealed class AccountSecurityTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task PasswordFailuresLockAccountAndPreventEvenCorrectPassword()
|
||||
{
|
||||
await using var f = await Fixture.CreateAsync();
|
||||
for (var i = 0; i < 5; i++)
|
||||
Assert.IsType<UnauthorizedObjectResult>((await f.Controller.SetupTotp(new("wrong"))).Result);
|
||||
Assert.True(await f.Users.IsLockedOutAsync(f.User));
|
||||
Assert.IsType<UnauthorizedObjectResult>((await f.Controller.SetupTotp(new("LocalUser@123"))).Result);
|
||||
Assert.Null(await f.Users.GetAuthenticatorKeyAsync(f.User));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvalidTotpAttemptsLockAccount()
|
||||
{
|
||||
await using var f = await Fixture.CreateAsync();
|
||||
await f.Users.ResetAuthenticatorKeyAsync(f.User);
|
||||
for (var i = 0; i < 5; i++)
|
||||
Assert.IsAssignableFrom<ObjectResult>((await f.Controller.EnableTotp(new("not-a-code"))).Result);
|
||||
Assert.True(await f.Users.IsLockedOutAsync(f.User));
|
||||
Assert.IsAssignableFrom<ObjectResult>((await f.Controller.EnableTotp(new("123456"))).Result);
|
||||
Assert.False(f.User.TwoFactorEnabled);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnabledTotpCannotBeResetOrReenabled()
|
||||
{
|
||||
await using var f = await Fixture.CreateAsync();
|
||||
await f.Users.ResetAuthenticatorKeyAsync(f.User);
|
||||
var key = await f.Users.GetAuthenticatorKeyAsync(f.User);
|
||||
await f.Users.SetTwoFactorEnabledAsync(f.User, true);
|
||||
Assert.IsType<ConflictObjectResult>((await f.Controller.SetupTotp(new("LocalUser@123"))).Result);
|
||||
Assert.IsType<ConflictObjectResult>((await f.Controller.EnableTotp(new("123456"))).Result);
|
||||
Assert.Equal(key, await f.Users.GetAuthenticatorKeyAsync(f.User));
|
||||
}
|
||||
|
||||
private sealed class Fixture(SqliteConnection connection, ServiceProvider provider, ApplicationUser user, UserManager<ApplicationUser> users, AccountSecurityController controller) : IAsyncDisposable
|
||||
{
|
||||
public ApplicationUser User { get; } = user;
|
||||
public UserManager<ApplicationUser> Users { get; } = users;
|
||||
public AccountSecurityController Controller { get; } = controller;
|
||||
|
||||
public static async Task<Fixture> CreateAsync()
|
||||
{
|
||||
var connection = new SqliteConnection("Data Source=:memory:");
|
||||
await connection.OpenAsync();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddDbContext<AppDbContext>(options => options.UseSqlite(connection));
|
||||
services.AddIdentityCore<ApplicationUser>(options =>
|
||||
{
|
||||
options.Lockout.MaxFailedAccessAttempts = 5;
|
||||
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
|
||||
})
|
||||
.AddRoles<ApplicationRole>()
|
||||
.AddTokenProvider<AuthenticatorTokenProvider<ApplicationUser>>(TokenOptions.DefaultAuthenticatorProvider)
|
||||
.AddEntityFrameworkStores<AppDbContext>();
|
||||
var provider = services.BuildServiceProvider();
|
||||
await provider.GetRequiredService<AppDbContext>().Database.EnsureCreatedAsync();
|
||||
var users = provider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var user = new ApplicationUser { UserName = "user", DisplayName = "User", IsEnabled = true, LockoutEnabled = true };
|
||||
Assert.True((await users.CreateAsync(user, "LocalUser@123")).Succeeded);
|
||||
var controller = new AccountSecurityController(users, new BrandingOptions())
|
||||
{
|
||||
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())], "test")) } }
|
||||
};
|
||||
return new Fixture(connection, provider, user, users, controller);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await provider.DisposeAsync();
|
||||
await connection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user