diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-02-15 16:44:29 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-02-15 16:44:29 -0700 |
commit | 46ad450327111b9d28b592658d75ef57da498298 (patch) | |
tree | 2b837bac9ae36d5a34dda06ba745850da216257d /contrib/tlsauth | |
parent | bc96af40db6104580c22086c8db7c8119a404257 (diff) |
Switch Handler to an interface.
HandlerFunc is much better as a function returning a Handler, rather
than a newtype for the function type itself. This way there is no
confusion creating a type-inferenced variable with HandlerFunc(func(...
and then using a HandlerFunc where a Handler is expected. Much better to
only have one public type.
Diffstat (limited to 'contrib/tlsauth')
-rw-r--r-- | contrib/tlsauth/auth_test.go | 20 | ||||
-rw-r--r-- | contrib/tlsauth/gemini.go | 12 | ||||
-rw-r--r-- | contrib/tlsauth/gemini_test.go | 20 |
3 files changed, 26 insertions, 26 deletions
diff --git a/contrib/tlsauth/auth_test.go b/contrib/tlsauth/auth_test.go index 30b63f5..3cbc106 100644 --- a/contrib/tlsauth/auth_test.go +++ b/contrib/tlsauth/auth_test.go @@ -24,7 +24,7 @@ func TestIdentify(t *testing.T) { server, client, clientCert := setup(t, "testdata/server.crt", "testdata/server.key", "testdata/client1.crt", "testdata/client1.key", - func(_ context.Context, request *gus.Request) *gus.Response { + gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked = true ident := tlsauth.Identity(request) @@ -33,7 +33,7 @@ func TestIdentify(t *testing.T) { } return nil - }, + }), ) leafCert, err := x509.ParseCertificate(clientCert.Certificate[0]) require.Nil(t, err) @@ -51,15 +51,15 @@ func TestRequiredAuth(t *testing.T) { invoked1 := false invoked2 := false - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked1 = true return gemini.Success("", &bytes.Buffer{}) - } + }) - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked2 = true return gemini.Success("", &bytes.Buffer{}) - } + }) authMiddleware := gus.Filter(tlsauth.RequiredAuth(tlsauth.Allow), nil) @@ -94,19 +94,19 @@ func TestOptionalAuth(t *testing.T) { invoked1 := false invoked2 := false - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/one") { return nil } invoked1 = true return gemini.Success("", &bytes.Buffer{}) - } + }) - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { invoked2 = true return gemini.Success("", &bytes.Buffer{}) - } + }) mw := gus.Filter(tlsauth.OptionalAuth(tlsauth.Reject), nil) handler := gus.FallthroughHandler(mw(handler1), mw(handler2)) diff --git a/contrib/tlsauth/gemini.go b/contrib/tlsauth/gemini.go index 0db89de..40bee9e 100644 --- a/contrib/tlsauth/gemini.go +++ b/contrib/tlsauth/gemini.go @@ -14,7 +14,7 @@ import ( // not pass the approver it will be rejected with "62 certificate invalid". func GeminiAuth(approver Approver) gus.Middleware { return func(inner gus.Handler) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { identity := Identity(request) if identity == nil { return geminiMissingCert(ctx, request) @@ -23,8 +23,8 @@ func GeminiAuth(approver Approver) gus.Middleware { return geminiCertNotAuthorized(ctx, request) } - return inner(ctx, request) - } + return inner.Handle(ctx, request) + }) } } @@ -35,14 +35,14 @@ func GeminiAuth(approver Approver) gus.Middleware { // certificate, but it fails the approval. func GeminiOptionalAuth(approver Approver) gus.Middleware { return func(inner gus.Handler) gus.Handler { - return func(ctx context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response { identity := Identity(request) if identity != nil && !approver(identity) { return geminiCertNotAuthorized(ctx, request) } - return inner(ctx, request) - } + return inner.Handle(ctx, request) + }) } } diff --git a/contrib/tlsauth/gemini_test.go b/contrib/tlsauth/gemini_test.go index 8f1efda..7823de6 100644 --- a/contrib/tlsauth/gemini_test.go +++ b/contrib/tlsauth/gemini_test.go @@ -14,30 +14,30 @@ import ( ) func TestGeminiAuth(t *testing.T) { - handler1 := func(_ context.Context, request *gus.Request) *gus.Response { + handler1 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/one") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler2 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler2 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/two") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler3 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler3 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, "/three") { return nil } return gemini.Success("", &bytes.Buffer{}) - } - handler4 := func(_ context.Context, request *gus.Request) *gus.Response { + }) + handler4 := gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { return gemini.Success("", &bytes.Buffer{}) - } + }) handler := gus.FallthroughHandler( tlsauth.GeminiAuth(tlsauth.Allow)(handler1), @@ -74,12 +74,12 @@ func TestGeminiAuth(t *testing.T) { func TestGeminiOptionalAuth(t *testing.T) { pathHandler := func(path string) gus.Handler { - return func(_ context.Context, request *gus.Request) *gus.Response { + return gus.HandlerFunc(func(_ context.Context, request *gus.Request) *gus.Response { if !strings.HasPrefix(request.Path, path) { return nil } return gemini.Success("", &bytes.Buffer{}) - } + }) } handler := gus.FallthroughHandler( |