feat(authctl): support password reset

This commit is contained in:
QiuSW
2026-07-29 08:43:35 +08:00
parent c69879650e
commit d62a4af401
13 changed files with 205 additions and 16 deletions
@@ -213,6 +213,41 @@ func TestAuthServiceProvisionsHashedCredentials(t *testing.T) {
}
}
func TestAuthServiceResetsExistingUserPassword(t *testing.T) {
fixture := newAuthServiceFixture(t)
fixture.repository.users["admin"] = domain.User{
ID: "user-admin",
Username: "admin",
PasswordHash: "hashed:old-password",
Role: domain.UserRoleAdmin,
IsActive: true,
}
err := fixture.service.ResetUserPassword(
context.Background(),
ResetUserPasswordCommand{
Username: " ADMIN ",
Password: "admin",
},
)
if err != nil {
t.Fatalf("ResetUserPassword() error = %v", err)
}
if fixture.repository.users["admin"].PasswordHash != "hashed:admin" {
t.Fatalf(
"password hash = %q",
fixture.repository.users["admin"].PasswordHash,
)
}
err = fixture.service.ResetUserPassword(
context.Background(),
ResetUserPasswordCommand{Username: "missing", Password: "admin"},
)
assertAuthCode(t, err, "RESOURCE_NOT_FOUND")
}
func TestAuthServiceChangesUserAndDeviceStatus(t *testing.T) {
fixture := newAuthServiceFixture(t)
if err := fixture.service.SetUserActive(
@@ -409,6 +444,21 @@ func (repository *fakeAuthRepository) ProvisionDevice(
return device, nil
}
func (repository *fakeAuthRepository) ResetUserPassword(
_ context.Context,
username string,
passwordHash string,
_ time.Time,
) error {
user, found := repository.users[username]
if !found {
return ErrRepositoryNotFound
}
user.PasswordHash = passwordHash
repository.users[username] = user
return nil
}
func (repository *fakeAuthRepository) SetUserActive(
_ context.Context,
username string,