2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Bell Canada.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.cds.client;
22 import io.grpc.CallOptions;
23 import io.grpc.Channel;
24 import io.grpc.ClientCall;
25 import io.grpc.ClientInterceptor;
26 import io.grpc.ForwardingClientCall;
27 import io.grpc.Metadata;
28 import io.grpc.Metadata.Key;
29 import io.grpc.MethodDescriptor;
30 import org.onap.policy.cds.properties.CdsServerProperties;
33 * An interceptor to insert the client authHeader.
35 * <p>The {@link BasicAuthClientHeaderInterceptor} implements {@link ClientInterceptor} to insert authorization
36 * header data provided by {@link CdsServerProperties#getBasicAuth()} to all the outgoing calls.</p>
38 * <p>On the client context, we add metadata with "Authorization" as the key and "Basic" followed by base64 encoded
39 * username:password as its value.
40 * On the server side, CDS BasicAuthServerInterceptor (1) gets the client metadata from the server context, (2) extract
41 * the "Authorization" header key and finally (3) decodes the username and password from the authHeader.</p>
43 public class BasicAuthClientHeaderInterceptor implements ClientInterceptor {
45 static final String BASIC_AUTH_HEADER_KEY = "Authorization";
46 private CdsServerProperties props;
48 BasicAuthClientHeaderInterceptor(CdsServerProperties props) {
53 public <Q, P> ClientCall<Q, P> interceptCall(MethodDescriptor<Q, P> method,
54 CallOptions callOptions, Channel channel) {
55 Key<String> authHeader = Key.of(BASIC_AUTH_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER);
56 return new ForwardingClientCall.SimpleForwardingClientCall<Q, P>(channel.newCall(method, callOptions)) {
58 public void start(Listener<P> responseListener, Metadata headers) {
59 headers.put(authHeader, props.getBasicAuth());
60 super.start(responseListener, headers);