feat(system): add admin password reset with session revocation
Let system administrators reset another user's local password from the settings UI, revoke all active sessions, and record the action in the audit log.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
@@ -41,7 +42,8 @@ func (s *stubAuthTokenRepo) RevokeTokensByUserID(_ context.Context, userID strin
|
||||
}
|
||||
|
||||
type stubUserRepoForAuth struct {
|
||||
users map[string]*types.User
|
||||
users map[string]*types.User
|
||||
updateCalls int
|
||||
}
|
||||
|
||||
func (s *stubUserRepoForAuth) CreateUser(context.Context, *types.User) error { return nil }
|
||||
@@ -64,8 +66,11 @@ func (s *stubUserRepoForAuth) GetUserByUsername(context.Context, string) (*types
|
||||
func (s *stubUserRepoForAuth) GetUserByTenantID(context.Context, uint64) (*types.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *stubUserRepoForAuth) UpdateUser(context.Context, *types.User) error { return nil }
|
||||
func (s *stubUserRepoForAuth) DeleteUser(context.Context, string) error { return nil }
|
||||
func (s *stubUserRepoForAuth) UpdateUser(context.Context, *types.User) error {
|
||||
s.updateCalls++
|
||||
return nil
|
||||
}
|
||||
func (s *stubUserRepoForAuth) DeleteUser(context.Context, string) error { return nil }
|
||||
func (s *stubUserRepoForAuth) ListUsers(context.Context, int, int) ([]*types.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -177,6 +182,44 @@ func TestLogoutRevokesAllUserTokens(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminResetPasswordHashesPasswordAndRevokesSessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tokenRepo := &stubAuthTokenRepo{tokens: map[string]*types.AuthToken{}}
|
||||
svc := newAuthTestUserService(tokenRepo)
|
||||
repo := svc.userRepo.(*stubUserRepoForAuth)
|
||||
|
||||
if err := svc.AdminResetPassword(ctx, "user-1", "NewSecure9"); err != nil {
|
||||
t.Fatalf("AdminResetPassword() err = %v", err)
|
||||
}
|
||||
if repo.updateCalls != 1 {
|
||||
t.Fatalf("UpdateUser calls = %d, want 1", repo.updateCalls)
|
||||
}
|
||||
user := repo.users["user-1"]
|
||||
if user.PasswordHash == "NewSecure9" || user.PasswordHash == "" {
|
||||
t.Fatalf("password was not stored as a hash")
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte("NewSecure9")); err != nil {
|
||||
t.Fatalf("stored hash does not match new password: %v", err)
|
||||
}
|
||||
if len(tokenRepo.revokedUserIDs) != 1 || tokenRepo.revokedUserIDs[0] != "user-1" {
|
||||
t.Fatalf("RevokeTokensByUserID calls = %v, want [user-1]", tokenRepo.revokedUserIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminResetPasswordRejectsWeakPasswordBeforeWrite(t *testing.T) {
|
||||
tokenRepo := &stubAuthTokenRepo{tokens: map[string]*types.AuthToken{}}
|
||||
svc := newAuthTestUserService(tokenRepo)
|
||||
repo := svc.userRepo.(*stubUserRepoForAuth)
|
||||
|
||||
err := svc.AdminResetPassword(context.Background(), "user-1", "password")
|
||||
if !errors.Is(err, ErrPasswordPolicy) {
|
||||
t.Fatalf("AdminResetPassword() err = %v, want ErrPasswordPolicy", err)
|
||||
}
|
||||
if repo.updateCalls != 0 || len(tokenRepo.revokedUserIDs) != 0 {
|
||||
t.Fatalf("weak password caused side effects: updates=%d revocations=%v", repo.updateCalls, tokenRepo.revokedUserIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserIDFromSignedTokenAcceptsExpiredToken(t *testing.T) {
|
||||
expired := signTestJWT(jwt.MapClaims{
|
||||
"user_id": "user-1",
|
||||
|
||||
Reference in New Issue
Block a user