Created session storage for auth and token packages

This commit is contained in:
Maxim Lebedev 2022-01-12 23:14:47 +05:00
parent 4127eca29d
commit 9078c72874
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package session
import (
"context"
"errors"
"source.toby3d.me/website/indieauth/internal/domain"
)
type Repository interface {
Create(ctx context.Context, session *domain.Session) error
GetAndDelete(ctx context.Context, code string) (*domain.Session, error)
GC()
}
var ErrNotExist = errors.New("session not exist")

View File

@ -0,0 +1,89 @@
package memory
import (
"context"
"path"
"sync"
"time"
"source.toby3d.me/website/indieauth/internal/domain"
"source.toby3d.me/website/indieauth/internal/session"
)
type (
Session struct {
CreatedAt time.Time
*domain.Session
}
memorySessionRepository struct {
store *sync.Map
config *domain.Config
}
)
const DefaultPathPrefix string = "sessions"
func NewMemorySessionRepository(config *domain.Config, store *sync.Map) session.Repository {
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
}
func (repo *memorySessionRepository) GetAndDelete(_ context.Context, code string) (*domain.Session, error) {
src, ok := repo.store.LoadAndDelete(path.Join(DefaultPathPrefix, code))
if !ok {
return nil, session.ErrNotExist
}
result, ok := src.(*Session)
if !ok {
return nil, session.ErrNotExist
}
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
})
}
}

View File

@ -0,0 +1,11 @@
package session
import (
"context"
"source.toby3d.me/website/indieauth/internal/domain"
)
type UseCase interface {
Exchange(ctx context.Context, code string) (*domain.Session, error)
}

View File

@ -0,0 +1,28 @@
package usecase
import (
"context"
"fmt"
"source.toby3d.me/website/indieauth/internal/domain"
"source.toby3d.me/website/indieauth/internal/session"
)
type sessionUseCase struct {
sessions session.Repository
}
func NewSessionUseCase(sessions session.Repository) session.UseCase {
return &sessionUseCase{
sessions: sessions,
}
}
func (useCase *sessionUseCase) Exchange(ctx context.Context, code string) (*domain.Session, error) {
session, err := useCase.sessions.GetAndDelete(ctx, code)
if err != nil {
return nil, fmt.Errorf("cannot find session in store: %w", err)
}
return session, nil
}