b981b806c505c1308c5f67e6407a0b3e940b5b17
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.sdclistener.client;
17
18 import io.grpc.*;
19 import io.grpc.Metadata.Key;
20 import org.springframework.beans.factory.annotation.Value;
21 import org.springframework.boot.context.properties.ConfigurationProperties;
22 import org.springframework.stereotype.Component;
23
24 /**
25  * To provide authentication with GRPC server.
26  */
27 @ConfigurationProperties("listenerservice")
28 @Component
29 public class SdcListenerAuthClientInterceptor implements ClientInterceptor {
30
31     @Value("${listenerservice.config.authHeader}")
32     private String basicAuth;
33
34     @Override
35     public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> methodDescriptor,
36                                                                CallOptions callOptions, Channel channel) {
37         Key<String> authHeader = Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
38         return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
39             channel.newCall(methodDescriptor, callOptions)) {
40             @Override
41             public void start(Listener<RespT> responseListener, Metadata headers) {
42                 headers.put(authHeader, basicAuth);
43                 super.start(responseListener, headers);
44             }
45         };
46     }
47 }