Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions middles/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import "github.com/shoenig/go-conceal"

type UserIdentity any

// UserData is the data stored in a session cookie.
type UserData[I UserIdentity] interface {
Identity() I
Token() *conceal.Text
}

// UserSession is a live session, established by a middleware after validating
// the session cookie and its associated server-side token.
type UserSession[I UserIdentity] interface {
Identity() I
Token() *conceal.Text
Active() bool
}
7 changes: 6 additions & 1 deletion middles/oauth/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
// ErrNotFound indidcates no oauth session was found in the request context.
// ErrNotFound indidcates no such session was found in the request context.
ErrNotFound = errors.New("session: not found")

// ErrNotMatch indicates the stored session does not match the session from
Expand All @@ -23,6 +23,7 @@ var (
type Cache[K, T any] interface {
Get(K) (T, bool)
Put(K, T, time.Duration)
Delete(K) error
}

// Unique is a unique number assigned to each user that can be associated
Expand Down Expand Up @@ -64,3 +65,7 @@ func (s *Sessions[U]) Match(id U, token *conceal.Text) error {
return nil
}
}

func (s *Sessions[U]) Delete(token *conceal.Text) error {
return s.Cache.Delete(token)
}
24 changes: 24 additions & 0 deletions middles/oauth/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (m *mockCache) Put(k *conceal.Text, v rowid, _ time.Duration) {
m.storage[k.Unveil()] = v
}

func (m *mockCache) Delete(k *conceal.Text) error {
delete(m.storage, k.Unveil())
return nil
}

func TestSessions_Create(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -90,3 +95,22 @@ func TestSessions_Match(t *testing.T) {
must.ErrorIs(t, err, ErrNotMatch)
})
}

func TestSessions_Delete(t *testing.T) {
t.Parallel()

cookies := (*CookieFactory[rowid])(nil)
cache := &mockCache{storage: make(map[string]rowid)}
sessions := NewSessions(cookies, cache)

id := rowid(12345)
token := conceal.UUIDv4()
cache.Put(token, id, 1*time.Hour)

err := sessions.Delete(token)
must.NoError(t, err)

// the session must no longer exist
err = sessions.Match(id, token)
must.ErrorIs(t, err, ErrNotFound)
}
8 changes: 8 additions & 0 deletions middles/oauth/volatile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ func (vc *VolatileCache[T]) Put(path string, value T, ttl time.Duration) {
value: value,
}
}

func (vc *VolatileCache[T]) Delete(path string) error {
vc.lock.Lock()
defer vc.lock.Unlock()

delete(vc.data, path)
return nil
}
23 changes: 23 additions & 0 deletions middles/oauth/volatile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,26 @@ func TestVolatileCache_Put(t *testing.T) {
must.Eq(t, 2, val)
})
}

func TestVolatileCache_Delete(t *testing.T) {
t.Parallel()

t.Run("existing key", func(t *testing.T) {
vc := NewVolatileCache[string](size)
vc.Put("key-1", "hello", 1*time.Minute)

err := vc.Delete("key-1")
must.NoError(t, err)

val, ok := vc.Get("key-1")
must.False(t, ok)
must.Eq(t, "", val)
})

t.Run("missing key", func(t *testing.T) {
vc := NewVolatileCache[string](size)

err := vc.Delete("non-existent")
must.NoError(t, err)
})
}
13 changes: 12 additions & 1 deletion middles/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import (
)

type Sessions[I identity.UserIdentity] interface {
// Create a new session for the given identity and ttl.
Create(I, time.Duration) *http.Cookie

// Match asserts the given token is associated with the user identity.
Match(I, *conceal.Text) error

// Delete terminates the session of the given token.
Delete(*conceal.Text) error
}

// GetSession extracts the user session out of the http.Request, which will
Expand Down Expand Up @@ -76,7 +82,7 @@ func (ss *SetSession[D, I]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// we found a matching token; we can allow the session
live := &session[I]{id: data.Identity(), active: true}
live := &session[I]{id: data.Identity(), token: data.Token(), active: true}
ctx2 := context.WithValue(r.Context(), sessionContextKey, live)
r2 := r.WithContext(ctx2)

Expand All @@ -87,6 +93,7 @@ func (ss *SetSession[D, I]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// allowing an identity based session to be recognized as active or not.
type session[I identity.UserIdentity] struct {
id I
token *conceal.Text
active bool
}

Expand All @@ -98,6 +105,10 @@ func (s *session[I]) Active() bool {
return s.active
}

func (s *session[I]) Token() *conceal.Text {
return s.token
}

func fromBase64(s string) string {
b, _ := base64.StdEncoding.DecodeString(s)
return string(b)
Expand Down