mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-23 00:26:42 +00:00
feat(lib): use new challenge creation flow
Previously Anubis constructed challenge strings from request metadata. This was a good idea in spirit, but has turned out to be a very bad idea in practice. This new flow reuses the Store facility to dynamically create challenge values with completely random data. This is a fairly big rewrite of how Anubis processes challenges. Right now it defaults to using the in-memory storage backend, but on-disk (boltdb) and valkey-based adaptors will come soon. Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/TecharoHQ/anubis/decaymap"
|
||||
)
|
||||
|
||||
type decayMapStore struct {
|
||||
store *decaymap.Impl[string, []byte]
|
||||
}
|
||||
|
||||
func (d *decayMapStore) Delete(_ context.Context, key string) error {
|
||||
if !d.store.Delete(key) {
|
||||
return fmt.Errorf("%w: %q", ErrNotFound, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *decayMapStore) Get(_ context.Context, key string) ([]byte, error) {
|
||||
result, ok := d.store.Get(key)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: %q", ErrNotFound, key)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (d *decayMapStore) Set(_ context.Context, key string, value []byte, expiry time.Duration) error {
|
||||
d.store.Set(key, value, expiry)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *decayMapStore) cleanupThread(ctx context.Context) {
|
||||
t := time.NewTicker(5 * time.Minute)
|
||||
defer t.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
d.store.Cleanup()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewDecayMapStore creates a simple in-memory store. This will not scale
|
||||
// to multiple Anubis instances.
|
||||
func NewDecayMapStore(ctx context.Context) Interface {
|
||||
result := &decayMapStore{
|
||||
store: decaymap.New[string, []byte](),
|
||||
}
|
||||
|
||||
go result.cleanupThread(ctx)
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user