From 64e65bff8d0ce209e038906b9f166f8ab546aab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Jochum?= Date: Thu, 22 Sep 2022 19:01:49 +0200 Subject: [PATCH] Move the Wrapper inside the registry from plugins so you can load plugins lazyly --- cmd/microauth2sqld/main.go | 2 +- noop.go | 8 ++------ plugin.go | 2 +- plugins/client/jwt/jwt.go | 26 +++++++++++--------------- registry.go | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/cmd/microauth2sqld/main.go b/cmd/microauth2sqld/main.go index eeed3b8..8c1cea0 100644 --- a/cmd/microauth2sqld/main.go +++ b/cmd/microauth2sqld/main.go @@ -167,7 +167,7 @@ func main() { micro.Name(config.Name), micro.Version(config.Version), micro.Flags(flags...), - micro.WrapHandler(auth2ClientReg.Plugin().Wrapper()), + micro.WrapHandler(auth2ClientReg.Wrapper()), micro.Action(func(c *cli.Context) error { // Start the logger if err := ilogger.Start(c); err != nil { diff --git a/noop.go b/noop.go index 38352b5..3810499 100644 --- a/noop.go +++ b/noop.go @@ -52,12 +52,8 @@ func (p *noopClientPlugin) Inspect(ctx context.Context) (*User, error) { return &User{Id: uuid.New().String(), Issuer: p.String()}, nil } -func (p *noopClientPlugin) Wrapper() server.HandlerWrapper { - return func(h server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - return h(ctx, req, rsp) - } - } +func (p *noopClientPlugin) WrapperFunc(h server.HandlerFunc, ctx context.Context, req server.Request, rsp interface{}) error { + return h(ctx, req, rsp) } func newNoopRouterPlugin() RouterPlugin { diff --git a/plugin.go b/plugin.go index e715ec1..19d1e45 100644 --- a/plugin.go +++ b/plugin.go @@ -45,7 +45,7 @@ type ClientPlugin interface { Inspect(ctx context.Context) (*User, error) // Wrapper returns the Auth Wrapper for your service - Wrapper() server.HandlerWrapper + WrapperFunc(h server.HandlerFunc, ctx context.Context, req server.Request, rsp interface{}) error } // RouterPlugin is for routers that forward the token or do other stuff required by ClientPlugin diff --git a/plugins/client/jwt/jwt.go b/plugins/client/jwt/jwt.go index 62e21d3..c08f764 100644 --- a/plugins/client/jwt/jwt.go +++ b/plugins/client/jwt/jwt.go @@ -169,20 +169,16 @@ func (p *jwtPlugin) Inspect(ctx context.Context) (*auth2.User, error) { return &auth2.User{Id: claims.ID, Type: claims.Type, Issuer: claims.Issuer, Metadata: cMD, Scopes: claims.Scopes, Roles: claims.Roles}, nil } -func (p *jwtPlugin) Wrapper() server.HandlerWrapper { - return func(h server.HandlerFunc) server.HandlerFunc { - return func(ctx context.Context, req server.Request, rsp interface{}) error { - u, err := p.Inspect(ctx) - if err != nil { - u = auth2.AnonUser - } - ctx = context.WithValue(ctx, auth2.ContextUserKey{}, u) - - if err = p.verifier.Verify(ctx, u, req); err != nil { - return err - } - - return h(ctx, req, rsp) - } +func (p *jwtPlugin) WrapperFunc(h server.HandlerFunc, ctx context.Context, req server.Request, rsp interface{}) error { + u, err := p.Inspect(ctx) + if err != nil { + u = auth2.AnonUser } + ctx = context.WithValue(ctx, auth2.ContextUserKey{}, u) + + if err = p.verifier.Verify(ctx, u, req); err != nil { + return err + } + + return h(ctx, req, rsp) } diff --git a/registry.go b/registry.go index d4a4f34..9e78fd7 100644 --- a/registry.go +++ b/registry.go @@ -7,6 +7,8 @@ import ( "github.com/urfave/cli/v2" "go-micro.dev/v4" + "go-micro.dev/v4/errors" + "go-micro.dev/v4/server" "jochum.dev/jo-micro/auth2/shared/sutil" ) @@ -100,3 +102,17 @@ func (r *AuthRegistry[T]) Health(ctx context.Context) (string, error) { m, _ := any(r.plugin).(registryFuncs) return m.Health(ctx) } + +// Wrapper returns a server.HandleWrapper, this works only for ClientPlugin +func (r *AuthRegistry[T]) Wrapper() server.HandlerWrapper { + return func(h server.HandlerFunc) server.HandlerFunc { + return func(ctx context.Context, req server.Request, rsp interface{}) error { + m, ok := any(r.plugin).(ClientPlugin) + if !ok { + return errors.InternalServerError("auth2.registry.AuthRegistry.Wrapper:No such plugin", "No plugin found") + } + + return m.WrapperFunc(h, ctx, req, rsp) + } + } +}