mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
Several entry points change users, credentials or 2FA without writing an audit event, while the equivalent web or service path does. This records the existing actions on those paths: - `user:create` for self sign-up, OAuth2 auto-registration, link-account registration and `gitea admin user create` - `user:accesstoken:add` for `gitea admin user generate-access-token` and `user create --access-token`; `user:accesstoken:remove` for `DELETE /api/v1/token` - `user:twofactor:disable` for the admin "reset 2FA" option and `gitea admin user disable-2fa` - `user:key:ssh:add`, `user:key:gpg:add/remove` for the API - `user:email:primary`, `user:email:activate`, `user:status:active` for settings, the activation page/links and OAuth2 source sync deactivation - `user:oauth2application:grant` when a grant's scope changes, `user:externallogin:add` on registration via an external login - `user:authentication:fail:twofactor` for wrong recovery codes, failed WebAuthn assertions and 2FA during password reset Should be backported to v28. --------- Co-authored-by: silverwind <me@silverwind.io>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
audit_model "gitea.dev/models/audit"
|
|
auth_model "gitea.dev/models/auth"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/services/audit"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func microcmdUserDisableTwoFactor() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "disable-2fa",
|
|
Usage: "Disable two-factor authentication for a user",
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "username",
|
|
Aliases: []string{"u"},
|
|
Usage: "Username of the user to disable 2FA for",
|
|
},
|
|
&cli.Int64Flag{
|
|
Name: "id",
|
|
Usage: "ID of the user to disable 2FA for",
|
|
},
|
|
},
|
|
Action: runDisableTwoFactor,
|
|
}
|
|
}
|
|
|
|
func runDisableTwoFactor(ctx context.Context, c *cli.Command) error {
|
|
if !c.IsSet("id") && !c.IsSet("username") {
|
|
return errors.New("either --id or --username must be provided")
|
|
}
|
|
|
|
if !setting.IsInTesting {
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var user *user_model.User
|
|
var err error
|
|
if c.IsSet("id") {
|
|
user, err = user_model.GetUserByID(ctx, c.Int64("id"))
|
|
} else {
|
|
user, err = user_model.GetUserByName(ctx, c.String("username"))
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// When both selectors are given, make sure they refer to the same user.
|
|
if c.IsSet("id") && c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
|
|
return fmt.Errorf("the user with id %d is %q, which does not match the provided username %q", user.ID, user.Name, c.String("username"))
|
|
}
|
|
|
|
totp, webAuthn, err := auth_model.DisableTwoFactor(ctx, user.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if totp+webAuthn > 0 {
|
|
audit.Record(ctx, audit_model.UserTwoFactorDisable, user)
|
|
}
|
|
|
|
cprintf(c, "Disabled 2FA for user %q (removed %d TOTP and %d WebAuthn credential(s))\n", user.Name, totp, webAuthn)
|
|
return nil
|
|
}
|