43 lines
779 B
Go
43 lines
779 B
Go
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
|
|
}
|