34
34
import com .uber .cadence .internal .Version ;
35
35
import com .uber .cadence .internal .tracing .TracingPropagator ;
36
36
import com .uber .cadence .serviceclient .ClientOptions ;
37
+ import com .uber .cadence .serviceclient .auth .IAuthorizationProvider ;
37
38
import io .grpc .CallOptions ;
38
39
import io .grpc .Channel ;
39
40
import io .grpc .ClientCall ;
53
54
import io .opentelemetry .context .propagation .TextMapSetter ;
54
55
import io .opentracing .Span ;
55
56
import io .opentracing .Tracer ;
57
+ import java .nio .charset .StandardCharsets ;
56
58
import java .util .HashMap ;
57
59
import java .util .Map ;
58
60
import java .util .Objects ;
@@ -79,6 +81,9 @@ final class GrpcServiceStubs implements IGrpcServiceStubs {
79
81
private static final Metadata .Key <String > RPC_ENCODING_HEADER_KEY =
80
82
Metadata .Key .of ("rpc-encoding" , Metadata .ASCII_STRING_MARSHALLER );
81
83
84
+ private static final Metadata .Key <String > AUTHORIZATION_HEADER_KEY =
85
+ Metadata .Key .of ("cadence-authorization" , Metadata .ASCII_STRING_MARSHALLER );
86
+
82
87
private static final String CLIENT_IMPL_HEADER_VALUE = "uber-java" ;
83
88
84
89
private final ClientOptions options ;
@@ -121,6 +126,7 @@ final class GrpcServiceStubs implements IGrpcServiceStubs {
121
126
if (!Strings .isNullOrEmpty (options .getIsolationGroup ())) {
122
127
headers .put (ISOLATION_GROUP_HEADER_KEY , options .getIsolationGroup ());
123
128
}
129
+
124
130
Channel interceptedChannel =
125
131
ClientInterceptors .intercept (
126
132
channel ,
@@ -131,6 +137,11 @@ final class GrpcServiceStubs implements IGrpcServiceStubs {
131
137
if (log .isTraceEnabled ()) {
132
138
interceptedChannel = ClientInterceptors .intercept (interceptedChannel , tracingInterceptor );
133
139
}
140
+ if (options .getAuthProvider () != null ) {
141
+ interceptedChannel =
142
+ ClientInterceptors .intercept (
143
+ interceptedChannel , newAuthorizationInterceptor (options .getAuthProvider ()));
144
+ }
134
145
this .domainBlockingStub = DomainAPIGrpc .newBlockingStub (interceptedChannel );
135
146
this .domainFutureStub = DomainAPIGrpc .newFutureStub (interceptedChannel );
136
147
this .visibilityBlockingStub = VisibilityAPIGrpc .newBlockingStub (interceptedChannel );
@@ -143,6 +154,36 @@ final class GrpcServiceStubs implements IGrpcServiceStubs {
143
154
this .metaFutureStub = MetaAPIGrpc .newFutureStub (interceptedChannel );
144
155
}
145
156
157
+ private ClientInterceptor newAuthorizationInterceptor (IAuthorizationProvider provider ) {
158
+ return new ClientInterceptor () {
159
+ @ Override
160
+ public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
161
+ MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
162
+ return new ForwardingClientCall .SimpleForwardingClientCall <ReqT , RespT >(
163
+ next .newCall (method , callOptions )) {
164
+
165
+ @ Override
166
+ public void start (Listener <RespT > responseListener , Metadata headers ) {
167
+ headers .put (
168
+ AUTHORIZATION_HEADER_KEY ,
169
+ new String (provider .getAuthToken (), StandardCharsets .UTF_8 ));
170
+
171
+ Listener <RespT > listener =
172
+ new ForwardingClientCallListener .SimpleForwardingClientCallListener <RespT >(
173
+ responseListener ) {
174
+
175
+ @ Override
176
+ public void onHeaders (Metadata headers ) {
177
+ super .onHeaders (headers );
178
+ }
179
+ };
180
+ super .start (listener , headers );
181
+ }
182
+ };
183
+ }
184
+ };
185
+ }
186
+
146
187
private ClientInterceptor newOpenTelemetryInterceptor () {
147
188
return new ClientInterceptor () {
148
189
@ Override
0 commit comments