mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(http): api key cache handling (#1632)
This commit is contained in:
parent
0d53f7e5fc
commit
d13b421c42
6 changed files with 117 additions and 50 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
@ -17,7 +18,6 @@ import (
|
|||
type Service interface {
|
||||
List(ctx context.Context) ([]domain.APIKey, error)
|
||||
Store(ctx context.Context, key *domain.APIKey) error
|
||||
Update(ctx context.Context, key *domain.APIKey) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
ValidateAPIKey(ctx context.Context, token string) bool
|
||||
}
|
||||
|
@ -26,63 +26,75 @@ type service struct {
|
|||
log zerolog.Logger
|
||||
repo domain.APIRepo
|
||||
|
||||
keyCache []domain.APIKey
|
||||
keyCache map[string]domain.APIKey
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger, repo domain.APIRepo) Service {
|
||||
return &service{
|
||||
log: log.With().Str("module", "api").Logger(),
|
||||
repo: repo,
|
||||
keyCache: []domain.APIKey{},
|
||||
keyCache: map[string]domain.APIKey{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) List(ctx context.Context) ([]domain.APIKey, error) {
|
||||
if len(s.keyCache) > 0 {
|
||||
return s.keyCache, nil
|
||||
keys := make([]domain.APIKey, 0, len(s.keyCache))
|
||||
|
||||
for _, key := range s.keyCache {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
return s.repo.GetKeys(ctx)
|
||||
return s.repo.GetAllAPIKeys(ctx)
|
||||
}
|
||||
|
||||
func (s *service) Store(ctx context.Context, key *domain.APIKey) error {
|
||||
key.Key = GenerateSecureToken(16)
|
||||
func (s *service) Store(ctx context.Context, apiKey *domain.APIKey) error {
|
||||
apiKey.Key = GenerateSecureToken(16)
|
||||
|
||||
if err := s.repo.Store(ctx, key); err != nil {
|
||||
if err := s.repo.Store(ctx, apiKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(s.keyCache) > 0 {
|
||||
// set new key
|
||||
s.keyCache = append(s.keyCache, *key)
|
||||
// set new apiKey
|
||||
s.keyCache[apiKey.Key] = *apiKey
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Update(ctx context.Context, key *domain.APIKey) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, key string) error {
|
||||
// reset
|
||||
s.keyCache = []domain.APIKey{}
|
||||
err := s.repo.Delete(ctx, key)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not delete api key: %s", key)
|
||||
}
|
||||
|
||||
return s.repo.Delete(ctx, key)
|
||||
// remove key from cache
|
||||
delete(s.keyCache, key)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) ValidateAPIKey(ctx context.Context, key string) bool {
|
||||
keys, err := s.repo.GetKeys(ctx)
|
||||
if _, ok := s.keyCache[key]; ok {
|
||||
s.log.Trace().Msgf("api service key cache hit: %s", key)
|
||||
return true
|
||||
}
|
||||
|
||||
apiKey, err := s.repo.GetKey(ctx, key)
|
||||
if err != nil {
|
||||
s.log.Trace().Msgf("api service key cache invalid key: %s", key)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, k := range keys {
|
||||
if k.Key == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
s.log.Trace().Msgf("api service key cache miss: %s", key)
|
||||
|
||||
s.keyCache[key] = *apiKey
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func GenerateSecureToken(length int) string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue