diff --git a/middles/identity/identity.go b/middles/identity/identity.go index 7682a55..1d77959 100644 --- a/middles/identity/identity.go +++ b/middles/identity/identity.go @@ -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 } diff --git a/middles/oauth/sessions.go b/middles/oauth/sessions.go index 6ec73e9..4b4b5af 100644 --- a/middles/oauth/sessions.go +++ b/middles/oauth/sessions.go @@ -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 @@ -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 @@ -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) +} diff --git a/middles/oauth/sessions_test.go b/middles/oauth/sessions_test.go index ec27a72..0a53954 100644 --- a/middles/oauth/sessions_test.go +++ b/middles/oauth/sessions_test.go @@ -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() @@ -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) +} diff --git a/middles/oauth/volatile.go b/middles/oauth/volatile.go index ffcd419..a4ef038 100644 --- a/middles/oauth/volatile.go +++ b/middles/oauth/volatile.go @@ -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 +} diff --git a/middles/oauth/volatile_test.go b/middles/oauth/volatile_test.go index abf5c29..a130c34 100644 --- a/middles/oauth/volatile_test.go +++ b/middles/oauth/volatile_test.go @@ -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) + }) +} diff --git a/middles/sessions.go b/middles/sessions.go index 5e49b18..3d2ded1 100644 --- a/middles/sessions.go +++ b/middles/sessions.go @@ -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 @@ -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) @@ -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 } @@ -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)