Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplified workflow setup for HTTP middleware #280

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading