-
Notifications
You must be signed in to change notification settings - Fork 43
/
client_interceptors.go
53 lines (38 loc) · 1.51 KB
/
client_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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package authjwt
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// ===== [ Constants and Variables ] =====
var (
jwtToken = ""
)
// ===== [ Types ] =====
// ===== [ Implementations ] =====
// ===== [ Private Functions ] =====
// ===== [ Public Functions ] =====
// UnaryClientInterceptor is to JWT 토큰을 전달하는 Unary 클라이언트 인터셉터
func UnaryClientInterceptor(token string) grpc.UnaryClientInterceptor {
jwtToken = token
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if jwtToken == "" {
return status.Errorf(codes.Unauthenticated, "jwt token is not supplied")
}
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", jwtToken)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
// StreamClientInterceptor is to JWT 토큰을 전달하는 Stream 클라이언트 인터셉터
func StreamClientInterceptor(token string) grpc.StreamClientInterceptor {
jwtToken = token
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
if jwtToken == "" {
return nil, status.Errorf(codes.Unauthenticated, "jwt token is not supplied")
}
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", jwtToken)
return streamer(ctx, desc, cc, method, opts...)
}
}