2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.components.impl.aaf;
22 import javax.servlet.http.HttpServletRequest;
23 import org.aspectj.lang.JoinPoint;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.aspectj.lang.annotation.Before;
26 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.servlets.BeGenericServlet;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
33 // aop id defined via application-context.xml. the annotations are only for test purposes
35 public class RoleAuthorizationHandler {
37 private static final Logger log = Logger.getLogger(RoleAuthorizationHandler.class);
38 private final ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
40 @Before("@annotation(permissions)")
41 public void authorizeRole(JoinPoint joinPoint, PermissionAllowed permissions) {
42 if (isPermissionAuthenticationNeeded()) {
43 String methodName = joinPoint.getSignature().toShortString();
44 HttpServletRequest request = ((BeGenericServlet) joinPoint.getThis()).getServletRequest();
45 String[] perms = permissions.value();
46 logAuth(methodName, perms, true, null);
47 for (String perm : perms) {
48 if (request.isUserInRole(getFullPermission(perm))) {
49 logAuth(methodName, perms, false, true);
53 logAuth(methodName, perms, false, false);
54 throw new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
58 private void logAuth(String methodName, String[] perms, boolean beforeAuth, Boolean success) {
60 log.trace("#{} - authorizing before invoking endpoint {}", methodName);
62 String status = success ? "SUCCESS" : "FAILED";
63 log.trace("#{} - authorizing before invoking endpoint {}, Status: {}", methodName, status);
67 private String getFullPermission(String role) {
68 return AafPermission.getEnumByString(role).getFullPermission();
71 private boolean isPermissionAuthenticationNeeded() {
72 if (configurationManager.getConfiguration().getAafAuthNeeded() && ThreadLocalsHolder.isExternalRequest()) {