5ab848893055f1449fa953696c6044fe46e7f6c4
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2019 AT&T, Bell Canada, Nordix Foundation
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
18
19 import org.apache.http.conn.ssl.NoopHostnameVerifier
20 import org.apache.http.conn.ssl.SSLConnectionSocketFactory
21 import org.apache.http.conn.ssl.TrustAllStrategy
22 import org.apache.http.impl.client.CloseableHttpClient
23 import org.apache.http.impl.client.HttpClients
24 import org.apache.http.message.BasicHeader
25 import org.apache.http.ssl.SSLContextBuilder
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.rest.utils.WebClientUtils
28 import org.springframework.http.HttpHeaders
29 import org.springframework.http.MediaType
30 import java.net.URI
31 import java.nio.charset.Charset
32 import java.util.Base64
33
34 class BasicAuthRestClientService(
35     private val restClientProperties:
36         BasicAuthRestClientProperties
37 ) :
38     BlueprintWebClientService {
39
40     override fun defaultHeaders(): Map<String, String> {
41
42         val encodedCredentials = setBasicAuth(
43             restClientProperties.username,
44             restClientProperties.password
45         )
46         return mapOf(
47             HttpHeaders.CONTENT_TYPE to MediaType.APPLICATION_JSON_VALUE,
48             HttpHeaders.ACCEPT to MediaType.APPLICATION_JSON_VALUE,
49             HttpHeaders.AUTHORIZATION to "Basic $encodedCredentials"
50         )
51     }
52
53     override fun host(uri: String): String {
54         val uri: URI = URI.create(restClientProperties.url + uri)
55         return uri.resolve(uri).toString()
56     }
57
58     override fun httpClient(): CloseableHttpClient {
59         val sslContext = SSLContextBuilder.create()
60
61         sslContext.loadTrustMaterial(TrustAllStrategy.INSTANCE)
62         val csf = SSLConnectionSocketFactory(sslContext.build(), NoopHostnameVerifier())
63         return HttpClients.custom()
64             .addInterceptorFirst(WebClientUtils.logRequest())
65             .addInterceptorLast(WebClientUtils.logResponse())
66             .setSSLSocketFactory(csf).build()
67     }
68
69     override fun convertToBasicHeaders(headers: Map<String, String>):
70         Array<BasicHeader> {
71             val customHeaders: MutableMap<String, String> = headers.toMutableMap()
72             // inject additionalHeaders
73             customHeaders.putAll(verifyAdditionalHeaders(restClientProperties))
74
75             if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
76                 val encodedCredentials = setBasicAuth(
77                     restClientProperties.username,
78                     restClientProperties.password
79                 )
80                 customHeaders[HttpHeaders.AUTHORIZATION] =
81                     "Basic $encodedCredentials"
82             }
83             return super.convertToBasicHeaders(customHeaders)
84         }
85
86     private fun setBasicAuth(username: String, password: String): String {
87         val credentialsString = "$username:$password"
88         return Base64.getEncoder().encodeToString(
89             credentialsString.toByteArray(Charset.defaultCharset())
90         )
91     }
92 }