Skip to content

Commit

Permalink
Delete user related oauth stuff on user deletion too (go-gitea#19677)
Browse files Browse the repository at this point in the history
* delete user related oauth stuff on user deletion too

* extend doctor check-db-consistency
  • Loading branch information
6543 committed May 11, 2022
1 parent d6aab06 commit c0042b3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package auth

import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
Expand All @@ -18,6 +19,7 @@ import (

uuid "github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
"xorm.io/builder"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -562,3 +564,21 @@ func GetActiveOAuth2SourceByName(name string) (*Source, error) {

return authSource, nil
}

func DeleteOAuth2RelictsByUserID(ctx context.Context, userID int64) error {
deleteCond := builder.Select("id").From("oauth2_grant").Where(builder.Eq{"oauth2_grant.user_id": userID})

if _, err := db.GetEngine(ctx).In("grant_id", deleteCond).
Delete(&OAuth2AuthorizationCode{}); err != nil {
return err
}

if err := db.DeleteBeans(ctx,
&OAuth2Application{UID: userID},
&OAuth2Grant{UserID: userID},
); err != nil {
return fmt.Errorf("DeleteBeans: %v", err)
}

return nil
}
5 changes: 5 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "image/jpeg" // Needed for jpeg support

asymkey_model "code.gitea.io/gitea/models/asymkey"
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -104,6 +105,10 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
return fmt.Errorf("deleteBeans: %v", err)
}

if err := auth_model.DeleteOAuth2RelictsByUserID(ctx, u.ID); err != nil {
return err
}

if setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now()) {

Expand Down
9 changes: 9 additions & 0 deletions modules/doctor/dbconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
// find action without repository
genericOrphanCheck("Action entries without existing repository",
"action", "repository", "action.repo_id=repository.id"),
// find OAuth2Grant without existing user
genericOrphanCheck("Orphaned OAuth2Grant without existing User",
"oauth2_grant", "user", "oauth2_grant.user_id=user.id"),
// find OAuth2Application without existing user
genericOrphanCheck("Orphaned OAuth2Application without existing User",
"oauth2_application", "user", "oauth2_application.uid=user.id"),
// find OAuth2AuthorizationCode without existing OAuth2Grant
genericOrphanCheck("Orphaned OAuth2AuthorizationCode without existing OAuth2Grant",
"oauth2_authorization_code", "oauth2_grant", "oauth2_authorization_code.grant_id=oauth2_grant.id"),
)

for _, c := range consistencyChecks {
Expand Down

0 comments on commit c0042b3

Please sign in to comment.