interface.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package challenge
  2. import (
  3. "log/slog"
  4. "net/http"
  5. "sort"
  6. "sync"
  7. "github.com/TecharoHQ/anubis/lib/config"
  8. "github.com/TecharoHQ/anubis/lib/policy"
  9. "github.com/TecharoHQ/anubis/lib/store"
  10. "github.com/a-h/templ"
  11. )
  12. var (
  13. registry map[string]Impl = map[string]Impl{}
  14. regLock sync.RWMutex
  15. )
  16. func Register(name string, impl Impl) {
  17. regLock.Lock()
  18. defer regLock.Unlock()
  19. registry[name] = impl
  20. }
  21. func Get(name string) (Impl, bool) {
  22. regLock.RLock()
  23. defer regLock.RUnlock()
  24. result, ok := registry[name]
  25. return result, ok
  26. }
  27. func Methods() []string {
  28. regLock.RLock()
  29. defer regLock.RUnlock()
  30. var result []string
  31. for method := range registry {
  32. result = append(result, method)
  33. }
  34. sort.Strings(result)
  35. return result
  36. }
  37. type IssueInput struct {
  38. Impressum *config.Impressum
  39. Rule *policy.Bot
  40. Challenge *Challenge
  41. OGTags map[string]string
  42. Store store.Interface
  43. }
  44. type ValidateInput struct {
  45. Rule *policy.Bot
  46. Challenge *Challenge
  47. Store store.Interface
  48. }
  49. type Impl interface {
  50. // Setup registers any additional routes with the Impl for assets or API routes.
  51. Setup(mux *http.ServeMux)
  52. // Issue a new challenge to the user, called by the Anubis.
  53. Issue(w http.ResponseWriter, r *http.Request, lg *slog.Logger, in *IssueInput) (templ.Component, error)
  54. // Validate a challenge, making sure that it passes muster.
  55. Validate(r *http.Request, lg *slog.Logger, in *ValidateInput) error
  56. }