add token service, sqlite driver and test

This commit is contained in:
2025-12-18 09:46:57 +02:00
parent 8836ea2673
commit cdde811e72
4 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package token
import (
"fmt"
"time"
"gorm.io/gorm"
)
type SQLiteTokenStore struct {
db *gorm.DB
}
type Token struct {
TokenID string `gorm:"primaryKey"`
Expiration time.Time `gorm:"index"`
}
func NewSQLiteTokenStore(db *gorm.DB) (*SQLiteTokenStore, error) {
if db == nil {
return nil, fmt.Errorf("db is nil")
}
return &SQLiteTokenStore{
db: db,
}, nil
}
func (s *SQLiteTokenStore) revoke(tokenID string, expiresAt time.Time) error {
return s.db.Create(&Token{
TokenID: tokenID,
Expiration: expiresAt,
}).Error
}
func (s *SQLiteTokenStore) isRevoked(tokenID string) (bool, error) {
var count int64
err := s.db.Model(&Token{}).Where("token_id = ?", tokenID).Count(&count).Error
if err != nil {
return false, err
}
return count > 0, nil
}