challenge.go 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package challenge
  2. import (
  3. "log/slog"
  4. "net/http"
  5. "sort"
  6. "sync"
  7. "github.com/TecharoHQ/anubis/lib/policy"
  8. "github.com/a-h/templ"
  9. )
  10. var (
  11. registry map[string]Impl = map[string]Impl{}
  12. regLock sync.RWMutex
  13. )
  14. func Register(name string, impl Impl) {
  15. regLock.Lock()
  16. defer regLock.Unlock()
  17. registry[name] = impl
  18. }
  19. func Get(name string) (Impl, bool) {
  20. regLock.RLock()
  21. defer regLock.RUnlock()
  22. result, ok := registry[name]
  23. return result, ok
  24. }
  25. func Methods() []string {
  26. regLock.RLock()
  27. defer regLock.RUnlock()
  28. var result []string
  29. for method := range registry {
  30. result = append(result, method)
  31. }
  32. sort.Strings(result)
  33. return result
  34. }
  35. type Impl interface {
  36. Fail(w http.ResponseWriter, r *http.Request) error
  37. Issue(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string, ogTags map[string]string) (templ.Component, error)
  38. Validate(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string) error
  39. }