You've already forked oauth2-proxy
mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-12-01 22:51:45 +02:00
Centralize Ticket management of persistent stores (#682)
* Centralize Ticket management of persistent stores persistence package with Manager & Ticket will handle all the details about keys, secrets, ticket into cookies, etc. Persistent stores just need to pass Save, Load & Clear function handles to the persistent manager now. * Shift to persistence.Manager wrapping a persistence.Store * Break up the Redis client builder logic * Move error messages to Store from Manager * Convert ticket to private for Manager use only * Add persistence Manager & ticket tests * Make a custom MockStore that handles time FastForwards
This commit is contained in:
58
pkg/sessions/tests/mock_store.go
Normal file
58
pkg/sessions/tests/mock_store.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// entry is a MockStore cache entry with an expiration
|
||||
type entry struct {
|
||||
data []byte
|
||||
expiration time.Duration
|
||||
}
|
||||
|
||||
// MockStore is a generic in-memory implementation of persistence.Store
|
||||
// for mocking in tests
|
||||
type MockStore struct {
|
||||
cache map[string]entry
|
||||
elapsed time.Duration
|
||||
}
|
||||
|
||||
// NewMockStore creates a MockStore
|
||||
func NewMockStore() *MockStore {
|
||||
return &MockStore{
|
||||
cache: map[string]entry{},
|
||||
elapsed: 0 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// Save sets a key to the data to the memory cache
|
||||
func (s *MockStore) Save(_ context.Context, key string, value []byte, exp time.Duration) error {
|
||||
s.cache[key] = entry{
|
||||
data: value,
|
||||
expiration: exp,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load gets data from the memory cache via a key
|
||||
func (s *MockStore) Load(_ context.Context, key string) ([]byte, error) {
|
||||
entry, ok := s.cache[key]
|
||||
if !ok || entry.expiration <= s.elapsed {
|
||||
delete(s.cache, key)
|
||||
return nil, fmt.Errorf("key not found: %s", key)
|
||||
}
|
||||
return entry.data, nil
|
||||
}
|
||||
|
||||
// Clear deletes an entry from the memory cache
|
||||
func (s *MockStore) Clear(_ context.Context, key string) error {
|
||||
delete(s.cache, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// FastForward simulates the flow of time to test expirations
|
||||
func (s *MockStore) FastForward(duration time.Duration) {
|
||||
s.elapsed += duration
|
||||
}
|
||||
@@ -133,18 +133,6 @@ func RunSessionStoreTests(newSS NewSessionStoreFunc, persistentFastForward Persi
|
||||
PersistentSessionStoreInterfaceTests(&input)
|
||||
}
|
||||
})
|
||||
|
||||
Context("with an invalid cookie secret", func() {
|
||||
BeforeEach(func() {
|
||||
input.cookieOpts.Secret = "invalid"
|
||||
})
|
||||
|
||||
It("returns an error when initialising the session store", func() {
|
||||
ss, err := newSS(opts, input.cookieOpts)
|
||||
Expect(err).To(MatchError("error initialising cipher: crypto/aes: invalid key size 7"))
|
||||
Expect(ss).To(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user