Auth Wrapper (#1174)

* Auth Wrapper

* Tweak cmd flag

* auth_excludes => auth_exclude

* Make Auth.Excludes variadic

* Use metadata.Get (passes through http and http2 it will go through various case formats)

* fix auth wrapper auth.Auth interface initialisation

Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
ben-toogood
2020-02-10 08:26:28 +00:00
committed by GitHub
parent c706afcf04
commit 4401c12e6c
9 changed files with 111 additions and 18 deletions

View File

@@ -8,6 +8,7 @@ import (
"sync"
"syscall"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/config/cmd"
"github.com/micro/go-micro/v2/debug/profile"
@@ -29,11 +30,15 @@ type service struct {
}
func newService(opts ...Option) Service {
service := new(service)
options := newOptions(opts...)
// service name
serviceName := options.Server.Options().Name
// TODO: better accessors
authFn := func() auth.Auth { return service.opts.Auth }
// wrap client to inject From-Service header on any calls
options.Client = wrapper.FromService(serviceName, options.Client)
options.Client = wrapper.TraceCall(serviceName, trace.DefaultTracer, options.Client)
@@ -42,11 +47,13 @@ func newService(opts ...Option) Service {
options.Server.Init(
server.WrapHandler(wrapper.HandlerStats(stats.DefaultStats)),
server.WrapHandler(wrapper.TraceHandler(trace.DefaultTracer)),
server.WrapHandler(wrapper.AuthHandler(authFn)),
)
return &service{
opts: options,
}
// set opts
service.opts = options
return service
}
func (s *service) Name() string {
@@ -88,6 +95,7 @@ func (s *service) Init(opts ...Option) {
// Initialise the command flags, overriding new service
if err := s.opts.Cmd.Init(
cmd.Auth(&s.opts.Auth),
cmd.Broker(&s.opts.Broker),
cmd.Registry(&s.opts.Registry),
cmd.Transport(&s.opts.Transport),