-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterceptor.go
124 lines (117 loc) · 3.37 KB
/
interceptor.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package private_maprdb_go_client
import (
"fmt"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
)
// User metadata struct
// encodedUserMetadata - base64 encoded username:password
// token - unique JWT from server
type userMetadata struct {
encodedUserMetadata, token string
}
// UpdateToken method updates or set JWT token which must be present in gRPC request
func (userMetadata *userMetadata) UpdateToken(header, trailer metadata.MD) {
if userMetadata.token == "" {
if val, ok := header["bearer-token"]; ok {
userMetadata.token = val[0]
}
}
}
// Closure function which returns custom UnaryClientInterceptor for channel
func UnaryClientAuthInterceptor(umd *userMetadata) grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req interface{},
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
if umd.token == "" {
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("basic %v", umd.encodedUserMetadata))
} else {
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("bearer %v", umd.token))
}
err := invoker(ctx, method, req, reply, cc, opts...)
return err
}
}
// UnaryClientTokenInterceptor responsible for unauthenticated response code in Unary calls.
func UnaryClientTokenInterceptor(umd *userMetadata) grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req interface{},
reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
err := invoker(ctx, method, req, reply, cc, opts...)
if grpc.Code(err) == codes.Unauthenticated {
if umd.token != "" {
umd.token = ""
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("basic %v", umd.encodedUserMetadata))
err = invoker(ctx, method, req, reply, cc, opts...)
} else {
return fmt.Errorf("authentication PAM failed on server. %v", err)
}
}
return err
}
}
func StreamClientAuthInterceptor(umd *userMetadata) grpc.StreamClientInterceptor {
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
if umd.token == "" {
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("basic %v", umd.encodedUserMetadata))
} else {
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("bearer %v", umd.token))
}
return streamer(ctx, desc, cc, method, opts...)
}
}
func StreamClientTokenInterceptor(umd *userMetadata) grpc.StreamClientInterceptor {
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
clientStream, err := streamer(ctx, desc, cc, method, opts...)
if grpc.Code(err) == codes.Unauthenticated {
if umd.token != "" {
umd.token = ""
ctx = metadata.AppendToOutgoingContext(ctx,
"authorization",
fmt.Sprintf("basic %v", umd.encodedUserMetadata))
clientStream, err = streamer(ctx, desc, cc, method, opts...)
} else {
return nil, fmt.Errorf("authentication PAM failed on server. %v", err)
}
}
return clientStream, err
}
}