Skip to content

Commit

Permalink
Merge pull request #280 from xmidt-org/feature/simpler-middleware-opt…
Browse files Browse the repository at this point in the history
…ions

simplified workflow setup for HTTP middleware
  • Loading branch information
johnabass authored Aug 15, 2024
2 parents 58b9aab + da69b50 commit b82c8f1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
16 changes: 16 additions & 0 deletions basculehttp/authenticator.go
Original file line number Diff line number Diff line change
@@ -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...)
}
16 changes: 16 additions & 0 deletions basculehttp/authorizer.go
Original file line number Diff line number Diff line change
@@ -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...)
}
26 changes: 25 additions & 1 deletion basculehttp/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
})
Expand Down
25 changes: 10 additions & 15 deletions basculehttp/middleware_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
)

Expand Down

0 comments on commit b82c8f1

Please sign in to comment.