From 5a7499ea3ba5876289a843f32e529a098ff167de Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Thu, 5 Jun 2025 20:26:23 -0400 Subject: [PATCH] fix(lib/challenge): allow challenges to register HTTP routes (#620) Signed-off-by: Xe Iaso --- docs/docs/CHANGELOG.md | 1 + lib/challenge/challenge.go | 7 ++++++- lib/challenge/proofofwork/proofofwork.go | 4 ++-- lib/config.go | 5 +++++ web/index_templ.go | 2 +- xess/xess_templ.go | 2 +- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index 994683c..e43e18a 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Refactor challenge presentation logic to use a challenge registry +- Allow challenge implementations to register HTTP routes ## v1.19.1: Jenomis cen Lexentale - Echo 1 diff --git a/lib/challenge/challenge.go b/lib/challenge/challenge.go index 25f3fd9..a26f90b 100644 --- a/lib/challenge/challenge.go +++ b/lib/challenge/challenge.go @@ -41,7 +41,12 @@ func Methods() []string { } type Impl interface { - Fail(w http.ResponseWriter, r *http.Request) error + // Setup registers any additional routes with the Impl for assets or API routes. + Setup(mux *http.ServeMux) + + // Issue a new challenge to the user, called by the Anubis. Issue(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string, ogTags map[string]string) (templ.Component, error) + + // Validate a challenge, making sure that it passes muster. Validate(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string) error } diff --git a/lib/challenge/proofofwork/proofofwork.go b/lib/challenge/proofofwork/proofofwork.go index c895e58..eb8e312 100644 --- a/lib/challenge/proofofwork/proofofwork.go +++ b/lib/challenge/proofofwork/proofofwork.go @@ -24,8 +24,8 @@ type Impl struct { Algorithm string } -func (i *Impl) Fail(w http.ResponseWriter, r *http.Request) error { - return nil +func (i *Impl) Setup(mux *http.ServeMux) { + /* no implementation required */ } func (i *Impl) Issue(r *http.Request, lg *slog.Logger, rule *policy.Bot, challenge string, ogTags map[string]string) (templ.Component, error) { diff --git a/lib/config.go b/lib/config.go index e64cb62..9bb78e8 100644 --- a/lib/config.go +++ b/lib/config.go @@ -151,6 +151,11 @@ func New(opts Options) (*Server, error) { registerWithPrefix(anubis.APIPrefix+"test-error", http.HandlerFunc(result.TestError), "GET") registerWithPrefix("/", http.HandlerFunc(result.maybeReverseProxyOrPage), "") + for _, implKind := range challenge.Methods() { + impl, _ := challenge.Get(implKind) + impl.Setup(mux) + } + result.mux = mux return result, nil diff --git a/web/index_templ.go b/web/index_templ.go index b015dea..34946b5 100644 --- a/web/index_templ.go +++ b/web/index_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.865 +// templ: version: v0.3.887 package web //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/xess/xess_templ.go b/xess/xess_templ.go index 1ac7615..975de27 100644 --- a/xess/xess_templ.go +++ b/xess/xess_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.865 +// templ: version: v0.3.887 package xess //lint:file-ignore SA4006 This context is only used if a nested component is present.