MSP-437 implement auth cache repository
This commit is contained in:
1 parent
bd4b52e5cc
commit
2ca358a4ea
2 files changed
+58
No files matched your search
@@ -0,0 +1,9 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAuthenticationCacheMiss = errors.New("authentication not found in cache")
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/integration-system/isp-kit/json"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/cache"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type AuthenticationCache struct {
|
||||
cache *cache.Cache
|
||||
duration time.Duration
|
||||
}
|
||||
|
||||
func NewAuthenticationCache(duration time.Duration) AuthenticationCache {
|
||||
return AuthenticationCache{
|
||||
duration: duration,
|
||||
cache: cache.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r AuthenticationCache) Get(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
data, ok := r.cache.Get(token)
|
||||
if !ok {
|
||||
return nil, domain.ErrAuthenticationCacheMiss
|
||||
}
|
||||
|
||||
result := domain.AuthData{}
|
||||
err := json.Unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "json unmarshal auth data")
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r AuthenticationCache) Set(ctx context.Context, token string, data domain.AuthData) error {
|
||||
value, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "json marshal auth data")
|
||||
}
|
||||
|
||||
r.cache.Set(token, value, r.duration)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user