Non-RT RIC Dashboard
[nonrtric.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / WebSecurityConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2019 Nordix Foundation
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 package org.oransc.ric.portal.dashboard.config;
22
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.lang.reflect.InvocationTargetException;
26 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
27 import org.oransc.ric.portal.dashboard.DashboardUserManager;
28 import org.oransc.ric.portal.dashboard.controller.A1Controller;
29 import org.oransc.ric.portal.dashboard.controller.SimpleErrorController;
30 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthManager;
31 import org.oransc.ric.portal.dashboard.portalapi.PortalAuthenticationFilter;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
40 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
41 import org.springframework.security.config.annotation.web.builders.WebSecurity;
42 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
43 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
44 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
45 import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
46
47 @Configuration
48 @EnableWebSecurity
49 @EnableGlobalMethodSecurity(securedEnabled = true)
50 @Profile("!test")
51 public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
52
53         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55         // Although constructor arguments are recommended over field injection,
56         // this results in fewer lines of code.
57         @Value("${portalapi.security}")
58         private Boolean portalapiSecurity;
59         @Value("${portalapi.appname}")
60         private String appName;
61         @Value("${portalapi.username}")
62         private String userName;
63         @Value("${portalapi.password}")
64         private String password;
65         @Value("${portalapi.decryptor}")
66         private String decryptor;
67         @Value("${portalapi.usercookie}")
68         private String userCookie;
69
70         @Autowired
71         DashboardUserManager userManager;
72
73         @Override
74     protected void configure(HttpSecurity http) throws Exception {
75                 logger.debug("configure: portalapi.username {}", userName);
76                 // A chain of ".and()" always baffles me
77                 http.authorizeRequests().anyRequest().authenticated();
78                 http.headers().frameOptions().disable();
79                 http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
80                 http.addFilterBefore(portalAuthenticationFilterBean(), BasicAuthenticationFilter.class);
81         }
82
83         /**
84          * Resource paths that do not require authentication, especially including
85          * Swagger-generated documentation.
86          */
87         public static final String[] OPEN_PATHS = { //
88                         "/v2/api-docs", //
89                         "/swagger-resources/**", //
90                         "/swagger-ui.html", //
91                         "/webjars/**", //
92                         PortalApiConstants.API_PREFIX + "/**", //
93                         A1Controller.CONTROLLER_PATH + "/" + A1Controller.VERSION_METHOD, //                                    
94                         SimpleErrorController.ERROR_PATH };
95
96         @Override
97         public void configure(WebSecurity web) throws Exception {
98                 // This disables Spring security, but not the app's filter.
99                 web.ignoring().antMatchers(OPEN_PATHS);
100         }
101
102         @Bean
103         public PortalAuthManager portalAuthManagerBean()
104                         throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
105                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
106                 return new PortalAuthManager(appName, userName, password, decryptor, userCookie);
107         }
108
109         /*
110          * If this is annotated with @Bean, it is created automatically AND REGISTERED,
111          * and Spring processes annotations in the source of the class. However, the
112          * filter is added in the chain apparently in the wrong order. Alternately, with
113          * no @Bean and added to the chain up in the configure() method in the desired
114          * order, the ignoring() matcher pattern configured above causes Spring to
115          * bypass this filter, which seems to me means the filter participates
116          * correctly.
117          */
118         public PortalAuthenticationFilter portalAuthenticationFilterBean()
119                         throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException,
120                         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
121                 PortalAuthenticationFilter portalAuthenticationFilter = new PortalAuthenticationFilter(portalapiSecurity,
122                                 portalAuthManagerBean(), this.userManager);
123                 return portalAuthenticationFilter;
124         }
125
126 }