-
-
Notifications
You must be signed in to change notification settings - Fork 7k
feat(auth): add disable-2fa command
#38275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
518cac5
add disable-2fa
Zettat123 2669f67
Merge branch 'main' into disable-2fa-cmd
Zettat123 8b1033e
Merge branch 'main' into disable-2fa-cmd
Zettat123 a8c71bc
Merge branch 'main' into disable-2fa-cmd
Zettat123 a61e4e5
Merge branch 'main' into disable-2fa-cmd
Zettat123 5482e71
fix comments
Zettat123 ce3333b
Merge branch 'main' into disable-2fa-cmd
Zettat123 71a7de0
Merge branch 'main' into disable-2fa-cmd
GiteaBot 434b93f
Merge branch 'main' into disable-2fa-cmd
GiteaBot dded7c2
Merge branch 'main' into disable-2fa-cmd
GiteaBot 9a6b1f2
Merge branch 'main' into disable-2fa-cmd
GiteaBot fc1df5e
Merge branch 'main' into disable-2fa-cmd
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| auth_model "gitea.dev/models/auth" | ||
| user_model "gitea.dev/models/user" | ||
| "gitea.dev/modules/setting" | ||
|
|
||
| "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 c.IsSet("id") && c.IsSet("username") { | ||
| return errors.New("provide exactly one of --id or --username") | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| removed, err := auth_model.DisableTwoFactor(ctx, user.ID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Printf("Disabled 2FA for user %q (removed %d credential(s))\n", user.Name, removed) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "io" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| auth_model "gitea.dev/models/auth" | ||
| "gitea.dev/models/db" | ||
| "gitea.dev/models/unittest" | ||
| user_model "gitea.dev/models/user" | ||
|
|
||
| "github.com/go-webauthn/webauthn/webauthn" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDisableTwoFactorCommand(t *testing.T) { | ||
| ctx := t.Context() | ||
|
|
||
| defer func() { | ||
| require.NoError(t, db.TruncateBeans(t.Context(), &user_model.User{}, &auth_model.TwoFactor{}, &auth_model.WebAuthnCredential{})) | ||
| }() | ||
|
|
||
| t.Run("disable TOTP and WebAuthn", func(t *testing.T) { | ||
| require.NoError(t, microcmdUserCreate().Run(ctx, []string{"create", "--username", "tfuser", "--email", "tfuser@gitea.local", "--random-password"})) | ||
| user := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "tfuser"}) | ||
|
|
||
| // Enroll TOTP. | ||
| tf := &auth_model.TwoFactor{UID: user.ID} | ||
| require.NoError(t, tf.SetSecret("test-secret")) | ||
| _, err := tf.GenerateScratchToken() | ||
| require.NoError(t, err) | ||
| require.NoError(t, auth_model.NewTwoFactor(ctx, tf)) | ||
|
|
||
| // Register a WebAuthn credential. | ||
| _, err = auth_model.CreateCredential(ctx, user.ID, "test-key", &webauthn.Credential{ID: []byte("test-cred-id")}) | ||
| require.NoError(t, err) | ||
|
|
||
| has, err := auth_model.HasTwoFactorOrWebAuthn(ctx, user.ID) | ||
| require.NoError(t, err) | ||
| require.True(t, has) | ||
|
|
||
| require.NoError(t, microcmdUserDisableTwoFactor().Run(ctx, []string{"disable-2fa", "--username", "tfuser"})) | ||
|
|
||
| // Both factors must be gone afterwards. | ||
| has, err = auth_model.HasTwoFactorOrWebAuthn(ctx, user.ID) | ||
| require.NoError(t, err) | ||
| assert.False(t, has) | ||
| }) | ||
|
|
||
| t.Run("disable by id", func(t *testing.T) { | ||
| require.NoError(t, microcmdUserCreate().Run(ctx, []string{"create", "--username", "iduser", "--email", "iduser@gitea.local", "--random-password"})) | ||
| user := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: "iduser"}) | ||
|
|
||
| tf := &auth_model.TwoFactor{UID: user.ID} | ||
| require.NoError(t, tf.SetSecret("test-secret")) | ||
| require.NoError(t, auth_model.NewTwoFactor(ctx, tf)) | ||
|
|
||
| require.NoError(t, microcmdUserDisableTwoFactor().Run(ctx, []string{"disable-2fa", "--id", strconv.FormatInt(user.ID, 10)})) | ||
|
|
||
| has, err := auth_model.HasTwoFactorOrWebAuthn(ctx, user.ID) | ||
| require.NoError(t, err) | ||
| assert.False(t, has) | ||
| }) | ||
|
|
||
| t.Run("no enrollment is a no-op", func(t *testing.T) { | ||
| require.NoError(t, microcmdUserCreate().Run(ctx, []string{"create", "--username", "plainuser", "--email", "plainuser@gitea.local", "--random-password"})) | ||
| require.NoError(t, microcmdUserDisableTwoFactor().Run(ctx, []string{"disable-2fa", "--username", "plainuser"})) | ||
| }) | ||
|
|
||
| t.Run("failure cases", func(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| args []string | ||
| expectedErr string | ||
| }{ | ||
| { | ||
| name: "user does not exist", | ||
| args: []string{"disable-2fa", "--username", "nonexistentuser"}, | ||
| expectedErr: "user does not exist", | ||
| }, | ||
| { | ||
| name: "neither id nor username", | ||
| args: []string{"disable-2fa"}, | ||
| expectedErr: "either --id or --username must be provided", | ||
| }, | ||
| { | ||
| name: "both id and username", | ||
| args: []string{"disable-2fa", "--id", "1", "--username", "someuser"}, | ||
| expectedErr: "provide exactly one of --id or --username", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| cmd := microcmdUserDisableTwoFactor() | ||
| cmd.Writer, cmd.ErrWriter = io.Discard, io.Discard | ||
| err := cmd.Run(ctx, tc.args) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), tc.expectedErr) | ||
| }) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.