Remove certOnly and basicAuth from authentication methods
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / restapi / ServletConfig.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2018 Nokia. All rights reserved.s
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae.restapi;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.onap.dcae.ApplicationException;
27 import org.onap.dcae.ApplicationSettings;
28 import org.onap.dcae.common.configuration.AuthMethod;
29 import org.onap.dcae.common.configuration.AuthMethodType;
30 import org.onap.dcae.common.configuration.CertBasicAuth;
31 import org.onap.dcae.common.configuration.NoAuth;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
34 import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class ServletConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
39
40     @Autowired
41     private ApplicationSettings properties;
42
43     @Override
44     public void customize(ConfigurableServletWebServerFactory container) {
45         provideAuthConfigurations(container).getOrDefault(properties.authMethod(),
46             notSupportedOperation()).configure();
47     }
48
49     private Map<String, AuthMethod> provideAuthConfigurations(ConfigurableServletWebServerFactory container) {
50         Map<String, AuthMethod> authMethods = new HashMap<>();
51         authMethods.put(AuthMethodType.CERT_BASIC_AUTH.value(), new CertBasicAuth(container, properties));
52         authMethods.put(AuthMethodType.NO_AUTH.value(), new NoAuth(container, properties));
53         return authMethods;
54     }
55
56     private AuthMethod notSupportedOperation() {
57         return () -> {
58             throw new ApplicationException(
59                 "Provided auth method not allowed: " + properties.authMethod());
60         };
61     }
62 }