1
0
Fork 0
forked from forgejo/forgejo

[BUG] Don't remove builtin OAuth2 applications

- When the database consistency is being run it would check for any
OAuth2 applications that don't have an existing user. However there are
few special OAuth2 applications that don't have an user set, because
they are global applications.
- This was not taken into account by the database consistency checker
and were removed if the database consistency check was being run with
autofix enabled.
- Take into account to ignore these global OAuth2 applications when
running the database consistency check.
- Add unit tests.
- Ref: https://codeberg.org/Codeberg/Community/issues/1530

(cherry picked from commit 6af8f3a3f2)
This commit is contained in:
Gusted 2024-04-06 00:52:39 +02:00 committed by GitHub
parent 7e5fed7e29
commit 2236574d50
4 changed files with 93 additions and 3 deletions

View file

@ -74,6 +74,13 @@ func BuiltinApplications() map[string]*BuiltinOAuth2Application {
return m
}
func BuiltinApplicationsClientIDs() (clientIDs []string) {
for clientID := range BuiltinApplications() {
clientIDs = append(clientIDs, clientID)
}
return clientIDs
}
func Init(ctx context.Context) error {
builtinApps := BuiltinApplications()
var builtinAllClientIDs []string
@ -637,3 +644,27 @@ func DeleteOAuth2RelictsByUserID(ctx context.Context, userID int64) error {
return nil
}
// CountOrphanedOAuth2Applications returns the amount of orphaned OAuth2 applications.
func CountOrphanedOAuth2Applications(ctx context.Context) (int64, error) {
return db.GetEngine(ctx).
Table("`oauth2_application`").
Join("LEFT", "`user`", "`oauth2_application`.`uid` = `user`.`id`").
Where(builder.IsNull{"`user`.id"}).
Where(builder.NotIn("`oauth2_application`.`client_id`", BuiltinApplicationsClientIDs())).
Select("COUNT(`oauth2_application`.`id`)").
Count()
}
// DeleteOrphanedOAuth2Applications deletes orphaned OAuth2 applications.
func DeleteOrphanedOAuth2Applications(ctx context.Context) (int64, error) {
subQuery := builder.Select("`oauth2_application`.id").
From("`oauth2_application`").
Join("LEFT", "`user`", "`oauth2_application`.`uid` = `user`.`id`").
Where(builder.IsNull{"`user`.id"}).
Where(builder.NotIn("`oauth2_application`.`client_id`", BuiltinApplicationsClientIDs()))
b := builder.Delete(builder.In("id", subQuery)).From("`oauth2_application`")
_, err := db.GetEngine(ctx).Exec(b)
return -1, err
}