528fbe2f3cb28ae224edec7ca175ac34bdbbf200
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada. All rights reserved.
3  *
4  * NOTICE:  All the intellectual and technical concepts contained herein are
5  * proprietary to Bell Canada and are protected by trade secret or copyright law.
6  * Unauthorized copying of this file, via any medium is strictly prohibited.
7  */
8
9 package org.onap.ccsdk.cds.cdssdclistener.client;
10
11 import io.grpc.CallOptions;
12 import io.grpc.Channel;
13 import io.grpc.ClientCall;
14 import io.grpc.ClientInterceptor;
15 import io.grpc.ForwardingClientCall;
16 import io.grpc.Metadata;
17 import io.grpc.Metadata.Key;
18 import io.grpc.MethodDescriptor;
19 import org.springframework.beans.factory.annotation.Value;
20 import org.springframework.boot.context.properties.ConfigurationProperties;
21 import org.springframework.stereotype.Component;
22
23 /**
24  * To provide authentication with GRPC server.
25  */
26 @ConfigurationProperties("listenerservice")
27 @Component
28 public class CdsSdcListenerAuthClientInterceptor implements ClientInterceptor {
29
30     @Value("${listenerservice.config.authHeader}")
31     private String basicAuth;
32
33     @Override
34     public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> methodDescriptor,
35                                                                CallOptions callOptions, Channel channel) {
36         Key<String> authHeader = Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
37         return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
38             channel.newCall(methodDescriptor, callOptions)) {
39             @Override
40             public void start(Listener<RespT> responseListener, Metadata headers) {
41                 headers.put(authHeader, basicAuth);
42                 super.start(responseListener, headers);
43             }
44         };
45     }
46 }