2022-01-12 18:14:47 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-29 19:56:27 +00:00
|
|
|
"fmt"
|
2022-01-12 18:14:47 +00:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/session"
|
2022-01-12 18:14:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Session struct {
|
|
|
|
CreatedAt time.Time
|
|
|
|
*domain.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
memorySessionRepository struct {
|
|
|
|
store *sync.Map
|
|
|
|
config *domain.Config
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultPathPrefix string = "sessions"
|
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
func NewMemorySessionRepository(store *sync.Map, config *domain.Config) session.Repository {
|
2022-01-12 18:14:47 +00:00
|
|
|
return &memorySessionRepository{
|
|
|
|
config: config,
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *memorySessionRepository) Create(_ context.Context, state *domain.Session) error {
|
|
|
|
repo.store.Store(path.Join(DefaultPathPrefix, state.Code), &Session{
|
|
|
|
CreatedAt: time.Now().UTC(),
|
|
|
|
Session: state,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:56:27 +00:00
|
|
|
func (repo *memorySessionRepository) Get(_ context.Context, code string) (*domain.Session, error) {
|
|
|
|
src, ok := repo.store.Load(path.Join(DefaultPathPrefix, code))
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot find session in store: %w", session.ErrNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := src.(*Session)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot decode session in store: %w", session.ErrNotExist)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Session, nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:14:47 +00:00
|
|
|
func (repo *memorySessionRepository) GetAndDelete(_ context.Context, code string) (*domain.Session, error) {
|
|
|
|
src, ok := repo.store.LoadAndDelete(path.Join(DefaultPathPrefix, code))
|
|
|
|
if !ok {
|
2022-01-29 19:56:27 +00:00
|
|
|
return nil, fmt.Errorf("cannot find session in store: %w", session.ErrNotExist)
|
2022-01-12 18:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := src.(*Session)
|
|
|
|
if !ok {
|
2022-01-29 19:56:27 +00:00
|
|
|
return nil, fmt.Errorf("cannot decode session in store: %w", session.ErrNotExist)
|
2022-01-12 18:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result.Session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *memorySessionRepository) GC() {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for ts := range ticker.C {
|
|
|
|
ts := ts
|
|
|
|
|
|
|
|
repo.store.Range(func(key, value interface{}) bool {
|
|
|
|
k, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
matched, err := path.Match(DefaultPathPrefix+"/*", k)
|
|
|
|
if err != nil || !matched {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
val, ok := value.(*Session)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.CreatedAt.Add(repo.config.Code.Expiry).After(ts) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.store.Delete(key)
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|