Skip to content

Commit

Permalink
feat: clean up session on janitor run
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen3 committed Jun 14, 2021
1 parent 81e0784 commit 0dda22d
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion persistence/sql/persister_consent.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,9 @@ func (p *Persister) FlushInactiveLoginConsentRequests(ctx context.Context, notAf
var cr consent.ConsentRequest
var crh consent.HandledConsentRequest

return p.transaction(ctx, func(ctx context.Context, c *pop.Connection) error {
var ls consent.LoginSession

return p.transaction(ctx, func(ctx context.Context, c *pop.Connection) error {
// Delete all entries (and their FK)
// where hydra_oauth2_authentication_request were timed-out or rejected
// - hydra_oauth2_authentication_request_handled
Expand Down Expand Up @@ -517,6 +518,32 @@ func (p *Persister) FlushInactiveLoginConsentRequests(ctx context.Context, notAf
time.Now().Add(-p.config.ConsentRequestMaxAge()),
notAfter).Exec()

if err != nil {
return sqlcon.HandleError(err)
}

err = p.Connection(ctx).RawQuery(fmt.Sprintf(`
DELETE
FROM %[1]s
WHERE NOT EXISTS
(
SELECT NULL
FROM %[2]s
WHERE %[2]s.login_session_id = %[1]s.id
)
AND NOT EXISTS
(
SELECT NULL
FROM %[3]s
WHERE %[3]s.login_session_id = %[1]s.id
)
AND authenticated_at < ?
`,
(&ls).TableName(),
(&lr).TableName(),
(&cr).TableName()),
notAfter).Exec()

return sqlcon.HandleError(err)
})
}

0 comments on commit 0dda22d

Please sign in to comment.