268dc6b7e4
* first round cleanup based on go-critic * more issues resolved for ifelsechain and unlambda checks * updated from master and gocritic found a new ifElseChain issue * disable appendAssign. i reports false positives * re-enabled go-critic appendAssign and disabled lint check at code line level * fixed go-critic lint error * fixed // nolint add gocritic specifically
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package grpcauth
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"storj.io/storj/pkg/auth"
|
|
)
|
|
|
|
// NewAPIKeyInterceptor creates instance of apikey interceptor
|
|
func NewAPIKeyInterceptor() grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req interface{},
|
|
info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{},
|
|
err error) {
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return handler(ctx, req)
|
|
}
|
|
apikeys, ok := md["apikey"]
|
|
if !ok || len(apikeys) == 0 {
|
|
return handler(ctx, req)
|
|
}
|
|
|
|
return handler(auth.WithAPIKey(ctx, []byte(apikeys[0])), req)
|
|
}
|
|
}
|
|
|
|
// NewAPIKeyInjector injects api key to grpc connection context
|
|
func NewAPIKeyInjector(apiKey string, callOpts ...grpc.CallOption) grpc.UnaryClientInterceptor {
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
opts = append(opts, callOpts...)
|
|
ctx = metadata.AppendToOutgoingContext(ctx, "apikey", apiKey)
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
|
}
|
|
}
|