-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinterceptors.go
33 lines (25 loc) · 1.03 KB
/
interceptors.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
33
package main
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const authTokenKey string = "auth_token"
const authTokenValue string = "authd"
// unaryAuthInterceptor is an interceptor automatically adding the auth token
// to a request.
func unaryAuthInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.AppendToOutgoingContext(ctx, authTokenKey, authTokenValue)
err := invoker(ctx, method, req, reply, cc, opts...)
return err
}
// streamAuthInterceptor is an interceptor automatically adding the auth token
// to a request.
func streamAuthInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.AppendToOutgoingContext(ctx, authTokenKey, authTokenValue)
s, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}
return s, nil
}