-
Notifications
You must be signed in to change notification settings - Fork 51
/
auth_token.go
32 lines (28 loc) · 1.05 KB
/
auth_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package sdk
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"os"
)
func getAuthKeyEnv() (key string) {
return os.Getenv(GrpcAuthKeyEnv)
}
func GetAuthTokenUnaryChainInterceptor() grpc.UnaryClientInterceptor {
md := metadata.Pairs(GrpcHeaderAuthorization, getAuthKeyEnv())
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.NewOutgoingContext(context.Background(), md)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func GetAuthTokenStreamChainInterceptor() grpc.StreamClientInterceptor {
md := metadata.Pairs(GrpcHeaderAuthorization, getAuthKeyEnv())
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.NewOutgoingContext(context.Background(), md)
s, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}
return s, nil
}
}