db, state: soft-revoke OAuth clients on DELETE

As Tailscale: the client stays readable as revoked; its tokens go.
The revoked-key collector reaps it after revoked_retention.
This commit is contained in:
Kristoffer Dalby committed 2026-11-20 23:02:54 +01:00
1 parent 167b7e6589
commit c4b36103a0
5 files changed
+68 -13

No files matched your search

+7
View File
@@ -384,6 +384,13 @@ func (h *Headscale) scheduledTasks(ctx context.Context) {
log.Info().Int("count", reaped).Msg("reaped revoked pre-auth keys")
}
reapedClients, err := h.state.DestroyRevokedOAuthClientsBefore(cutoff)
if err != nil {
log.Error().Err(err).Msg("reaping revoked oauth clients")
} else if reapedClients > 0 {
log.Info().Int64("count", reapedClients).Msg("reaped revoked oauth clients")
}
case <-accessTokenTicker.C:
reaped, err := h.state.DeleteExpiredAccessTokens(time.Now())
if err != nil {
+24 -6
View File
@@ -162,10 +162,11 @@ func (hsdb *HSDatabase) ListOAuthClients() ([]types.OAuthClient, error) {
return clients, nil
}
// RevokeOAuthClient deletes a client and all access tokens it issued. An unknown
// client id returns [ErrOAuthClientNotFound], so a repeated DELETE is a clean
// 404. Unlike pre-auth keys (which soft-revoke for node-registration history), an
// OAuth client has no such history and is removed outright, matching Tailscale.
// RevokeOAuthClient soft-revokes a client, as Tailscale's DELETE does: the row
// stays retrievable with its invalid flag set, and the access tokens it issued
// are deleted. Revoking a revoked client keeps its first revoke time and
// succeeds; an unknown client id returns [ErrOAuthClientNotFound]. The
// background collector hard-deletes it after the retention window.
func (hsdb *HSDatabase) RevokeOAuthClient(clientID string) error {
return hsdb.Write(func(tx *gorm.DB) error {
err := tx.Where("kind = ? AND client_id = ?", types.CredentialOAuthToken, clientID).
@@ -174,8 +175,9 @@ func (hsdb *HSDatabase) RevokeOAuthClient(clientID string) error {
return fmt.Errorf("deleting oauth access tokens: %w", err)
}
res := tx.Where("kind = ? AND identifier = ?", types.CredentialOAuthClient, clientID).
Delete(&types.Credential{})
res := tx.Model(&types.Credential{}).
Where("kind = ? AND identifier = ?", types.CredentialOAuthClient, clientID).
Update("revoked", gorm.Expr("COALESCE(revoked, ?)", time.Now()))
if res.Error != nil {
return res.Error
}
@@ -188,6 +190,22 @@ func (hsdb *HSDatabase) RevokeOAuthClient(clientID string) error {
})
}
// DestroyRevokedOAuthClientsBefore hard-deletes every client revoked before
// cutoff, returning how many were removed.
func (hsdb *HSDatabase) DestroyRevokedOAuthClientsBefore(cutoff time.Time) (int64, error) {
var count int64
err := hsdb.Write(func(tx *gorm.DB) error {
res := tx.Where("kind = ? AND revoked IS NOT NULL AND revoked < ?", types.CredentialOAuthClient, cutoff).
Delete(&types.Credential{})
count = res.RowsAffected
return res.Error
})
return count, err
}
// MintAccessToken stores a new [types.OAuthAccessToken] for clientID with the
// given (already narrowed) scopes/tags and expiration, returning the plaintext
// token (shown ONCE).
+27 -2
View File
@@ -312,10 +312,35 @@ func TestOAuthClientRevoke(t *testing.T) {
require.NoError(t, db.RevokeOAuthClient(client.ClientID))
// The client no longer authenticates and a repeated revoke is a clean 404.
// The client no longer authenticates and its tokens are gone, but it stays
// readable as revoked, and a repeated revoke keeps the first time, as on
// Tailscale (probe-G H6-H8).
_, err = db.AuthenticateOAuthClient(secret)
require.ErrorIs(t, err, ErrOAuthClientRevoked)
var tokens int64
require.NoError(t, db.DB.Model(&types.Credential{}).
Where("kind = ? AND client_id = ?", types.CredentialOAuthToken, client.ClientID).Count(&tokens).Error)
assert.Zero(t, tokens)
revoked, err := db.GetOAuthClientByClientID(client.ClientID)
require.NoError(t, err)
require.NotNil(t, revoked.Revoked)
require.NoError(t, db.RevokeOAuthClient(client.ClientID))
again, err := db.GetOAuthClientByClientID(client.ClientID)
require.NoError(t, err)
assert.Equal(t, revoked.Revoked, again.Revoked)
require.ErrorIs(t, db.RevokeOAuthClient("unknown"), ErrOAuthClientNotFound)
// The collector removes it once the retention window has passed.
n, err := db.DestroyRevokedOAuthClientsBefore(revoked.Revoked.Add(time.Second))
require.NoError(t, err)
assert.Equal(t, int64(1), n)
_, err = db.GetOAuthClientByClientID(client.ClientID)
require.Error(t, err)
require.ErrorIs(t, db.RevokeOAuthClient(client.ClientID), ErrOAuthClientNotFound)
}
func TestOAuthAccessTokenMintAuthenticateExpire(t *testing.T) {
+6 -1
View File
@@ -2319,11 +2319,16 @@ func (s *State) ListOAuthClients() ([]types.OAuthClient, error) {
return s.db.ListOAuthClients()
}
// RevokeOAuthClient deletes a client and the access tokens it issued.
// RevokeOAuthClient revokes a client and deletes the access tokens it issued.
func (s *State) RevokeOAuthClient(clientID string) error {
return s.db.RevokeOAuthClient(clientID)
}
// DestroyRevokedOAuthClientsBefore hard-deletes clients revoked before cutoff.
func (s *State) DestroyRevokedOAuthClientsBefore(cutoff time.Time) (int64, error) {
return s.db.DestroyRevokedOAuthClientsBefore(cutoff)
}
// MintAccessToken stores a new scoped access token for an OAuth client.
func (s *State) MintAccessToken(clientID string, scopes, tags []string, expiration *time.Time) (string, *types.OAuthAccessToken, error) {
return s.db.MintAccessToken(clientID, scopes, tags, expiration)
+4 -4
View File
@@ -118,10 +118,10 @@ type RouteConfig struct {
// PreAuthKeysConfig contains configuration for pre-auth key lifecycle.
type PreAuthKeysConfig struct {
// RevokedRetention is how long a soft-revoked pre-auth key (revoked via the
// v2 API's DELETE) is kept retrievable before the background collector
// hard-deletes it. Keys still backing a node are kept until the node is
// deleted. A zero or negative duration disables the collector.
// RevokedRetention is how long a soft-revoked pre-auth key or OAuth client
// (revoked via the v2 API's DELETE) is kept retrievable before the
// background collector hard-deletes it. Keys still backing a node are kept
// until the node is deleted. A zero or negative duration disables the collector.
RevokedRetention time.Duration `example:"\"720h\"" koanf:"revoked_retention"`
}