63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/session"
|
|
"github.com/pkg/errors"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type StoreAdapter struct {
|
|
client redis.UniversalClient
|
|
}
|
|
|
|
// Del implements authn.StoreAdapter.
|
|
func (s *StoreAdapter) Del(ctx context.Context, key string) error {
|
|
if err := s.client.Del(ctx, key).Err(); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Get implements authn.StoreAdapter.
|
|
func (s *StoreAdapter) Get(ctx context.Context, key string) ([]byte, error) {
|
|
cmd := s.client.Get(ctx, key)
|
|
|
|
if err := cmd.Err(); err != nil {
|
|
if errors.Is(err, redis.Nil) {
|
|
return nil, errors.WithStack(session.ErrNotFound)
|
|
}
|
|
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
data, err := cmd.Bytes()
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
// Set implements authn.StoreAdapter.
|
|
func (s *StoreAdapter) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error {
|
|
if err := s.client.Set(ctx, key, data, ttl).Err(); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewStoreAdapter(client redis.UniversalClient) *StoreAdapter {
|
|
return &StoreAdapter{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
var (
|
|
_ session.StoreAdapter = &StoreAdapter{}
|
|
)
|