Skip to content

Commit f41c2be

Browse files
authored
Delete user related oauth stuff on user deletion too (#19677)
* delete user related oauth stuff on user deletion too * extend doctor check-db-consistency
1 parent cbd4547 commit f41c2be

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

models/auth/oauth2.go

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package auth
66

77
import (
8+
"context"
89
"crypto/sha256"
910
"encoding/base32"
1011
"encoding/base64"
@@ -18,6 +19,7 @@ import (
1819

1920
uuid "github.com/google/uuid"
2021
"golang.org/x/crypto/bcrypt"
22+
"xorm.io/builder"
2123
"xorm.io/xorm"
2224
)
2325

@@ -576,3 +578,21 @@ func GetActiveOAuth2SourceByName(name string) (*Source, error) {
576578

577579
return authSource, nil
578580
}
581+
582+
func DeleteOAuth2RelictsByUserID(ctx context.Context, userID int64) error {
583+
deleteCond := builder.Select("id").From("oauth2_grant").Where(builder.Eq{"oauth2_grant.user_id": userID})
584+
585+
if _, err := db.GetEngine(ctx).In("grant_id", deleteCond).
586+
Delete(&OAuth2AuthorizationCode{}); err != nil {
587+
return err
588+
}
589+
590+
if err := db.DeleteBeans(ctx,
591+
&OAuth2Application{UID: userID},
592+
&OAuth2Grant{UserID: userID},
593+
); err != nil {
594+
return fmt.Errorf("DeleteBeans: %v", err)
595+
}
596+
597+
return nil
598+
}

models/user.go

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
_ "image/jpeg" // Needed for jpeg support
1414

1515
asymkey_model "code.gitea.io/gitea/models/asymkey"
16+
auth_model "code.gitea.io/gitea/models/auth"
1617
"code.gitea.io/gitea/models/db"
1718
"code.gitea.io/gitea/models/issues"
1819
"code.gitea.io/gitea/models/organization"
@@ -89,6 +90,10 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
8990
return fmt.Errorf("deleteBeans: %v", err)
9091
}
9192

93+
if err := auth_model.DeleteOAuth2RelictsByUserID(ctx, u.ID); err != nil {
94+
return err
95+
}
96+
9297
if setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
9398
u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now()) {
9499

modules/doctor/dbconsistency.go

+9
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
186186
// find action without repository
187187
genericOrphanCheck("Action entries without existing repository",
188188
"action", "repository", "action.repo_id=repository.id"),
189+
// find OAuth2Grant without existing user
190+
genericOrphanCheck("Orphaned OAuth2Grant without existing User",
191+
"oauth2_grant", "user", "oauth2_grant.user_id=user.id"),
192+
// find OAuth2Application without existing user
193+
genericOrphanCheck("Orphaned OAuth2Application without existing User",
194+
"oauth2_application", "user", "oauth2_application.uid=user.id"),
195+
// find OAuth2AuthorizationCode without existing OAuth2Grant
196+
genericOrphanCheck("Orphaned OAuth2AuthorizationCode without existing OAuth2Grant",
197+
"oauth2_authorization_code", "oauth2_grant", "oauth2_authorization_code.grant_id=oauth2_grant.id"),
189198
)
190199

191200
for _, c := range consistencyChecks {

0 commit comments

Comments
 (0)