From da69b50b92b37ececfd7b9d63092b8f344740ffe Mon Sep 17 00:00:00 2001 From: johnabass Date: Thu, 15 Aug 2024 15:05:19 -0700 Subject: [PATCH] simplified workflow setup for HTTP middleware --- basculehttp/authenticator.go | 16 +++++++++++++++ basculehttp/authorizer.go | 16 +++++++++++++++ basculehttp/middleware.go | 26 ++++++++++++++++++++++++- basculehttp/middleware_examples_test.go | 25 ++++++++++-------------- 4 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 basculehttp/authenticator.go create mode 100644 basculehttp/authorizer.go diff --git a/basculehttp/authenticator.go b/basculehttp/authenticator.go new file mode 100644 index 0000000..e91218c --- /dev/null +++ b/basculehttp/authenticator.go @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC +// SPDX-License-Identifier: Apache-2.0 + +package basculehttp + +import ( + "net/http" + + "github.com/xmidt-org/bascule/v1" +) + +// NewAuthenticator is a convenient wrapper around bascule.NewAuthenticator. +// This function eases the syntactical pain of generics when creating Middleware. +func NewAuthenticator(opts ...bascule.AuthenticatorOption[*http.Request]) (*bascule.Authenticator[*http.Request], error) { + return bascule.NewAuthenticator(opts...) +} diff --git a/basculehttp/authorizer.go b/basculehttp/authorizer.go new file mode 100644 index 0000000..89e0e2e --- /dev/null +++ b/basculehttp/authorizer.go @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC +// SPDX-License-Identifier: Apache-2.0 + +package basculehttp + +import ( + "net/http" + + "github.com/xmidt-org/bascule/v1" +) + +// NewAuthorizer is a convenient wrapper around bascule.NewAuthorizer. +// This function eases the syntactical pain of generics when creating Middleware. +func NewAuthorizer(opts ...bascule.AuthorizerOption[*http.Request]) (*bascule.Authorizer[*http.Request], error) { + return bascule.NewAuthorizer(opts...) +} diff --git a/basculehttp/middleware.go b/basculehttp/middleware.go index c2cf69b..6f6348a 100644 --- a/basculehttp/middleware.go +++ b/basculehttp/middleware.go @@ -24,9 +24,22 @@ func (mof middlewareOptionFunc) apply(m *Middleware) error { // WithAuthenticator supplies the Authenticator workflow for the middleware. // -// If no authenticator is supplied, NewMiddeware returns an error. +// Note: If no authenticator is supplied, NewMiddeware returns an error. func WithAuthenticator(authenticator *bascule.Authenticator[*http.Request]) MiddlewareOption { + return UseAuthenticator(authenticator, nil) +} + +// UseAuthenticator is a variant of WithAuthenticator that allows a caller to +// nest function calls a little easier. The output of NewAuthenticator +// can be passed directly to this option. +// +// Note: If no authenticator is supplied, NewMiddeware returns an error. +func UseAuthenticator(authenticator *bascule.Authenticator[*http.Request], err error) MiddlewareOption { return middlewareOptionFunc(func(m *Middleware) error { + if err != nil { + return err + } + m.authenticator = authenticator return nil }) @@ -37,7 +50,18 @@ func WithAuthenticator(authenticator *bascule.Authenticator[*http.Request]) Midd // The Authorizer is optional. If no authorizer is supplied, then no authorization // takes place and no authorization events are fired. func WithAuthorizer(authorizer *bascule.Authorizer[*http.Request]) MiddlewareOption { + return UseAuthorizer(authorizer, nil) +} + +// UseAuthorizer is a variant of WithAuthorizer that allows a caller to +// nest function calls a little easier. The output of NewAuthorizer +// can be passed directly to this option. +func UseAuthorizer(authorizer *bascule.Authorizer[*http.Request], err error) MiddlewareOption { return middlewareOptionFunc(func(m *Middleware) error { + if err != nil { + return err + } + m.authorizer = authorizer return nil }) diff --git a/basculehttp/middleware_examples_test.go b/basculehttp/middleware_examples_test.go index 240713d..742844c 100644 --- a/basculehttp/middleware_examples_test.go +++ b/basculehttp/middleware_examples_test.go @@ -18,27 +18,22 @@ func ExampleMiddleware_basicauth() { WithScheme(SchemeBasic, BasicTokenParser{}), ) - a, _ := bascule.NewAuthenticator( - bascule.WithTokenParsers(tp), + m, _ := NewMiddleware( + UseAuthenticator( + NewAuthenticator( + bascule.WithTokenParsers(tp), + ), + ), ) - m, err := NewMiddleware( - WithAuthenticator(a), - ) - - if err != nil { - panic(err) - } - // decorate a handler that needs authorization h := m.ThenFunc( func(response http.ResponseWriter, request *http.Request) { - t, ok := bascule.GetFrom(request) - if !ok { - panic("no token found") + if t, ok := bascule.GetFrom(request); ok { + fmt.Println("principal:", t.Principal()) + } else { + fmt.Println("no token found") } - - fmt.Println("principal:", t.Principal()) }, )