Auth Client Wrapper

This commit is contained in:
Ben Toogood 2020-04-29 15:11:06 +01:00 committed by Vasiliy Tolstov
parent 750b6d4733
commit a928fc196e

36
grpc.go
View File

@ -10,7 +10,6 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker" "github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/client/selector"
@ -18,7 +17,6 @@ import (
"github.com/micro/go-micro/v2/errors" "github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/metadata" "github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/util/config"
pnet "github.com/micro/go-micro/v2/util/net" pnet "github.com/micro/go-micro/v2/util/net"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -117,13 +115,6 @@ func (g *grpcClient) call(ctx context.Context, node *registry.Node, req client.R
// set the content type for the request // set the content type for the request
header["x-content-type"] = req.ContentType() header["x-content-type"] = req.ContentType()
// set the authorization header
if opts.ServiceToken || len(header["authorization"]) == 0 {
if h := g.authorizationHeader(); len(h) > 0 {
header["authorization"] = h
}
}
md := gmetadata.New(header) md := gmetadata.New(header)
ctx = gmetadata.NewOutgoingContext(ctx, md) ctx = gmetadata.NewOutgoingContext(ctx, md)
@ -202,13 +193,6 @@ func (g *grpcClient) stream(ctx context.Context, node *registry.Node, req client
// set the content type for the request // set the content type for the request
header["x-content-type"] = req.ContentType() header["x-content-type"] = req.ContentType()
// set the authorization header
if opts.ServiceToken || len(header["authorization"]) == 0 {
if h := g.authorizationHeader(); len(h) > 0 {
header["authorization"] = h
}
}
md := gmetadata.New(header) md := gmetadata.New(header)
ctx = gmetadata.NewOutgoingContext(ctx, md) ctx = gmetadata.NewOutgoingContext(ctx, md)
@ -295,26 +279,6 @@ func (g *grpcClient) stream(ctx context.Context, node *registry.Node, req client
}, nil }, nil
} }
func (g *grpcClient) authorizationHeader() string {
// if the caller specifies using service token or no token
// was passed with the request, set the service token
var srvToken string
if g.opts.Auth != nil && g.opts.Auth.Options().Token != nil {
srvToken = g.opts.Auth.Options().Token.AccessToken
}
if len(srvToken) > 0 {
return auth.BearerScheme + srvToken
}
// fall back to using the authorization token set in config,
// this enables the CLI to provide a token
if token, err := config.Get("micro", "auth", "token"); err == nil && len(token) > 0 {
return auth.BearerScheme + token
}
return ""
}
func (g *grpcClient) poolMaxStreams() int { func (g *grpcClient) poolMaxStreams() int {
if g.opts.Context == nil { if g.opts.Context == nil {
return DefaultPoolMaxStreams return DefaultPoolMaxStreams