add token service, sqlite driver and test
This commit is contained in:
42
internal/token/store_sqlite.go
Normal file
42
internal/token/store_sqlite.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user