1package auth
2
3import (
4 "context"
5 "errors"
6 "sync"
7)
8
9var (
10 registry = map[string]Constructor{}
11 mtx sync.RWMutex
12
13 // ErrNotFound is returned when a store is not found.
14 ErrNotFound = errors.New("auth store not found")
15)
16
17// Constructor is a function that returns a new store.
18type Constructor func(ctx context.Context) (Auth, error)
19
20// Register registers a store.
21func Register(name string, fn Constructor) {
22 mtx.Lock()
23 defer mtx.Unlock()
24
25 registry[name] = fn
26}
27
28// New returns a new store.
29func New(ctx context.Context, name string) (Auth, error) {
30 mtx.RLock()
31 fn, ok := registry[name]
32 mtx.RUnlock()
33
34 if !ok {
35 return nil, ErrNotFound
36 }
37
38 return fn(ctx)
39}
40
41// List returns a list of registered stores.
42func List() []string {
43 mtx.Lock()
44 defer mtx.Unlock()
45 stores := make([]string, 0)
46 for name := range registry {
47 stores = append(stores, name)
48 }
49 return stores
50}