diff --git a/.version b/.version index aedc15b..fe16b34 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -2.5.3 +2.5.4 diff --git a/CHANGELOG.md b/CHANGELOG.md index ffc0972..b5f7256 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 2.5.4 +* fix auth cache ## 2.5.3 * fix grpc multipart error response ## 2.5.2 diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index 3896176..5e40bc5 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -37,11 +37,11 @@ type authenticate struct { } func ReceiveConfiguration(conf conf.Cache) { - if conf.EnableCash { + if conf.EnableCache { if timeout, err := time.ParseDuration(conf.EvictTimeout); err != nil { log.Fatalf(stdcodes.ModuleInvalidRemoteConfig, "invalid timeout '%s'", timeout) } else { - auth.verify = veritification.NewCacheableVerify(timeout) + auth.verify = veritification.NewCacheablesVerify(timeout) } } else { auth.verify = veritification.NewRuntimeVerify() diff --git a/conf/default_remote_config.json b/conf/default_remote_config.json index 634b14c..9f0d1cd 100644 --- a/conf/default_remote_config.json +++ b/conf/default_remote_config.json @@ -69,7 +69,7 @@ "userSecret": "secret" }, "authCacheSetting": { - "enableCash": false, + "enableCache": false, "evictTimeout": "1m" } } diff --git a/conf/remote_config.go b/conf/remote_config.go index 9375157..8f1bcfa 100644 --- a/conf/remote_config.go +++ b/conf/remote_config.go @@ -32,7 +32,7 @@ type ( } Cache struct { - EnableCash bool `schema:"Кеширование,включает кеширования для токена приложения"` + EnableCache bool `schema:"Кеширование,включает кеширования для токена приложения"` EvictTimeout string `schema:"Время жизни записи в кеше"` } diff --git a/service/veritification/cacheable_verify.go b/service/veritification/cacheable_verify.go index 473db43..2b64e8b 100644 --- a/service/veritification/cacheable_verify.go +++ b/service/veritification/cacheable_verify.go @@ -6,7 +6,7 @@ import ( ) type ( - cacheableVerify struct { + cacheablesVerify struct { cache map[string]cacheInfo timeout time.Duration rv *runtimeVerify @@ -19,34 +19,42 @@ type ( } ) -func (cv *cacheableVerify) ApplicationToken(token string) (map[string]string, error) { +func (cv *cacheablesVerify) ApplicationToken(token string) (map[string]string, error) { cv.lock.RLock() cache, ok := cv.cache[token] cv.lock.RUnlock() if ok { - if time.Now().Sub(cache.created) > cv.timeout { - return cache.identity, nil + if time.Now().Sub(cache.created) < cv.timeout { + return cv.copyCache(cache.identity), nil } } cv.lock.Lock() defer cv.lock.Unlock() - if cache, ok = cv.cache[token]; ok { - if time.Now().Sub(cache.created) > cv.timeout { - return cache.identity, nil - } else { - delete(cv.cache, token) + cache, ok = cv.cache[token] + if ok { + if time.Now().Sub(cache.created) < cv.timeout { + return cv.copyCache(cache.identity), nil } + delete(cv.cache, token) } - if identity, err := cv.rv.ApplicationToken(token); err != nil { + + identity, err := cv.rv.ApplicationToken(token) + if err != nil { return nil, err - } else { - cv.cache[token] = cacheInfo{created: time.Now(), identity: identity} - resp := identity - return resp, nil } + cv.cache[token] = cacheInfo{created: time.Now(), identity: identity} + return cv.copyCache(identity), nil } -func (cv *cacheableVerify) Identity(identity map[string]string, uri string) (map[string]string, error) { +func (cv *cacheablesVerify) Identity(identity map[string]string, uri string) (map[string]string, error) { return cv.rv.Identity(identity, uri) } + +func (cv *cacheablesVerify) copyCache(cache map[string]string) map[string]string { + resp := make(map[string]string, 4) + for key, value := range cache { + resp[key] = value + } + return resp +} diff --git a/service/veritification/verify.go b/service/veritification/verify.go index 37e62d7..0def4e5 100644 --- a/service/veritification/verify.go +++ b/service/veritification/verify.go @@ -15,8 +15,8 @@ func NewRuntimeVerify() Verify { return &runtimeVerify{} } -func NewCacheableVerify(timeout time.Duration) Verify { - return &cacheableVerify{ +func NewCacheablesVerify(timeout time.Duration) Verify { + return &cacheablesVerify{ cache: make(map[string]cacheInfo), timeout: timeout, rv: &runtimeVerify{},