6fb41d79991553cd5edfc5cb73c52d62124b1f43
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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.ccsdk.features.sdnr.wt.oauthprovider.filters;
23
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26 import javax.servlet.http.HttpServletRequest;
27 import org.apache.shiro.authc.AuthenticationToken;
28 import org.apache.shiro.web.filter.authc.BearerHttpAuthenticationFilter;
29 import org.apache.shiro.web.util.WebUtils;
30 import org.opendaylight.aaa.shiro.filters.ODLHttpAuthenticationFilter;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class BearerAndBasicHttpAuthenticationFilter extends BearerHttpAuthenticationFilter{
35
36     // defined in lower-case for more efficient string comparison
37     private static final Logger LOG = LoggerFactory.getLogger(BearerAndBasicHttpAuthenticationFilter.class);
38     private ODLHttpAuthenticationHelperFilter basicAuthFilter;
39
40     public BearerAndBasicHttpAuthenticationFilter() {
41         this.basicAuthFilter = new ODLHttpAuthenticationHelperFilter();
42     }
43
44     protected static final String OPTIONS_HEADER = "OPTIONS";
45
46     @Override
47     protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
48         final String authHeader = this.getAuthzHeader(request);
49         if (authHeader != null && authHeader.startsWith("Basic")) {
50             return this.createBasicAuthToken(request, response);
51         }
52         return super.createToken(request, response);
53     }
54
55     @Override
56     protected String[] getPrincipalsAndCredentials(String scheme, String token) {
57         LOG.debug("getPrincipalsAndCredentials with scheme {} and token {}", scheme, token);
58         if (scheme.toLowerCase().equals("basic")) {
59             return this.basicAuthFilter.getPrincipalsAndCredentials(scheme, token);
60         }
61         return super.getPrincipalsAndCredentials(scheme, token);
62     }
63
64     @Override
65     protected boolean isLoginAttempt(String authzHeader) {
66         LOG.debug("isLoginAttempt with header {}", authzHeader);
67         if (this.basicAuthFilter.isLoginAttempt(authzHeader)) {
68             return true;
69         }
70         return super.isLoginAttempt(authzHeader);
71     }
72
73     @Override
74     protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
75         final HttpServletRequest httpRequest = WebUtils.toHttp(request);
76         final String httpMethod = httpRequest.getMethod();
77         if (OPTIONS_HEADER.equalsIgnoreCase(httpMethod)) {
78             return true;
79         } else {
80             if (this.basicAuthFilter.isAccessAllowed(httpRequest, response, mappedValue)) {
81                 LOG.debug("isAccessAllowed succeeded on basicAuth");
82                 return true;
83             }
84         }
85         return super.isAccessAllowed(request, response, mappedValue);
86     }
87
88     protected AuthenticationToken createBasicAuthToken(ServletRequest request, ServletResponse response) {
89         String authorizationHeader = getAuthzHeader(request);
90         if (authorizationHeader == null || authorizationHeader.length() == 0) {
91             // Create an empty authentication token since there is no
92             // Authorization header.
93             return createToken("", "", request, response);
94         }
95
96         if (LOG.isDebugEnabled()) {
97             LOG.debug("Attempting to execute login with headers [" + authorizationHeader + "]");
98         }
99
100         String[] prinCred = getPrincipalsAndCredentials(authorizationHeader, request);
101         if (prinCred == null || prinCred.length < 2) {
102             // Create an authentication token with an empty password,
103             // since one hasn't been provided in the request.
104             String username = prinCred == null || prinCred.length == 0 ? "" : prinCred[0];
105             return createToken(username, "", request, response);
106         }
107
108         String username = prinCred[0];
109         String password = prinCred[1];
110
111         return createToken(username, password, request, response);
112     }
113
114
115     private static class ODLHttpAuthenticationHelperFilter extends ODLHttpAuthenticationFilter{
116
117         ODLHttpAuthenticationHelperFilter(){
118             super();
119         }
120
121         @Override
122         protected boolean isLoginAttempt(String authzHeader) {
123             return super.isLoginAttempt(authzHeader);
124         }
125         @Override
126         protected String[] getPrincipalsAndCredentials(String scheme, String encoded) {
127             return super.getPrincipalsAndCredentials(scheme, encoded);
128         }
129         @Override
130         protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
131             return super.isAccessAllowed(request, response, mappedValue);
132         }
133     }
134 }