50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/conf"
|
|
)
|
|
|
|
type SettingsService struct {
|
|
uc *biz.SettingsUsecase
|
|
runtime *conf.Runtime
|
|
securityMu sync.RWMutex
|
|
securityCache *biz.SecurityConfig
|
|
cache biz.Cache
|
|
}
|
|
|
|
func NewSettingsService(uc *biz.SettingsUsecase, runtime *conf.Runtime, cache biz.Cache) *SettingsService {
|
|
return &SettingsService{uc: uc, runtime: runtime, cache: cache}
|
|
}
|
|
|
|
func (s *SettingsService) UseMultipoint() bool {
|
|
config := s.runtime.Admin()
|
|
return config != nil && config.System != nil && config.System.UseMultipoint
|
|
}
|
|
|
|
func activeTokenKey(username string) string { return "jwt:active:" + username }
|
|
|
|
func (s *SettingsService) ActiveTokenMatches(ctx context.Context, username, token string) (bool, error) {
|
|
if !s.UseMultipoint() {
|
|
return true, nil
|
|
}
|
|
active, ok, err := s.cache.Get(ctx, activeTokenKey(username))
|
|
return ok && active == token, err
|
|
}
|
|
|
|
func (s *SettingsService) RotateActiveToken(ctx context.Context, username, oldToken, newToken string, expiration time.Duration) error {
|
|
if !s.UseMultipoint() {
|
|
return nil
|
|
}
|
|
if oldToken != "" && oldToken != newToken {
|
|
if err := s.BlacklistToken(ctx, oldToken); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.cache.Set(ctx, activeTokenKey(username), newToken, expiration)
|
|
}
|