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