2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.filters;
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;
34 public class BearerAndBasicHttpAuthenticationFilter extends BearerHttpAuthenticationFilter{
36 // defined in lower-case for more efficient string comparison
37 private static final Logger LOG = LoggerFactory.getLogger(BearerAndBasicHttpAuthenticationFilter.class);
38 private ODLHttpAuthenticationHelperFilter basicAuthFilter;
40 public BearerAndBasicHttpAuthenticationFilter() {
41 this.basicAuthFilter = new ODLHttpAuthenticationHelperFilter();
44 protected static final String OPTIONS_HEADER = "OPTIONS";
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);
52 return super.createToken(request, response);
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);
61 return super.getPrincipalsAndCredentials(scheme, token);
65 protected boolean isLoginAttempt(String authzHeader) {
66 LOG.debug("isLoginAttempt with header {}", authzHeader);
67 if (this.basicAuthFilter.isLoginAttempt(authzHeader)) {
70 return super.isLoginAttempt(authzHeader);
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)) {
80 if (this.basicAuthFilter.isAccessAllowed(httpRequest, response, mappedValue)) {
81 LOG.debug("isAccessAllowed succeeded on basicAuth");
85 return super.isAccessAllowed(request, response, mappedValue);
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);
96 if (LOG.isDebugEnabled()) {
97 LOG.debug("Attempting to execute login with headers [" + authorizationHeader + "]");
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);
108 String username = prinCred[0];
109 String password = prinCred[1];
111 return createToken(username, password, request, response);
115 private static class ODLHttpAuthenticationHelperFilter extends ODLHttpAuthenticationFilter{
117 ODLHttpAuthenticationHelperFilter(){
122 protected boolean isLoginAttempt(String authzHeader) {
123 return super.isLoginAttempt(authzHeader);
126 protected String[] getPrincipalsAndCredentials(String scheme, String encoded) {
127 return super.getPrincipalsAndCredentials(scheme, encoded);
130 protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
131 return super.isAccessAllowed(request, response, mappedValue);