mirror of
https://github.com/juanfont/headscale.git
synced 2026-07-17 16:36:02 +00:00
db: report a missing pre-auth key on expire and destroy
Expiring or deleting a pre-auth key that does not exist updated zero rows and returned no error, so the caller could not tell a missing key from a successful no-op. Return ErrPreAuthKeyNotFound when RowsAffected is zero.
This commit is contained in:
parent
a00de89c85
commit
97eff90ebe
1 changed files with 20 additions and 5 deletions
|
|
@ -309,9 +309,13 @@ func DestroyPreAuthKey(tx *gorm.DB, id uint64) error {
|
|||
}
|
||||
|
||||
// Then delete the pre-auth key
|
||||
err = tx.Unscoped().Delete(&types.PreAuthKey{}, id).Error
|
||||
if err != nil {
|
||||
return err
|
||||
res := tx.Unscoped().Delete(&types.PreAuthKey{}, id)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
|
||||
if res.RowsAffected == 0 {
|
||||
return ErrPreAuthKeyNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -353,8 +357,19 @@ func UsePreAuthKey(tx *gorm.DB, k *types.PreAuthKey) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ExpirePreAuthKey marks a [types.PreAuthKey] as expired.
|
||||
// ExpirePreAuthKey marks a [types.PreAuthKey] as expired, returning
|
||||
// [ErrPreAuthKeyNotFound] rather than succeeding silently when no such key exists.
|
||||
func ExpirePreAuthKey(tx *gorm.DB, id uint64) error {
|
||||
now := time.Now()
|
||||
return tx.Model(&types.PreAuthKey{}).Where("id = ?", id).Update("expiration", now).Error
|
||||
|
||||
res := tx.Model(&types.PreAuthKey{}).Where("id = ?", id).Update("expiration", now)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
|
||||
if res.RowsAffected == 0 {
|
||||
return ErrPreAuthKeyNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue