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=========================================================
21 package org.openecomp.sdc.be.components.impl.aaf;
24 import org.aspectj.lang.JoinPoint;
25 import org.aspectj.lang.annotation.Aspect;
26 import org.aspectj.lang.annotation.Before;
27 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.servlets.BeGenericServlet;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
34 import javax.servlet.http.HttpServletRequest;
36 // aop id defined via application-context.xml. the annotations are only for test purposes
38 public class RoleAuthorizationHandler {
40 private static final Logger log = Logger.getLogger(RoleAuthorizationHandler.class);
42 private ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();
44 @Before("@annotation(permissions)")
45 public void authorizeRole(JoinPoint joinPoint, PermissionAllowed permissions) {
47 if (isPermissionAuthenticationNeeded()) {
48 String methodName = joinPoint.getSignature().toShortString();
49 HttpServletRequest request = ((BeGenericServlet) joinPoint.getThis()).getServletRequest();
50 String[] perms = permissions.value();
51 logAuth(methodName, perms, true, null);
52 for (String perm : perms) {
53 if (request.isUserInRole(getFullPermission(perm))) {
54 logAuth(methodName, perms, false, true);
58 logAuth(methodName, perms, false, false);
59 throw new ByActionStatusComponentException(ActionStatus.AUTH_FAILED);
64 private void logAuth(String methodName, String[] perms, boolean beforeAuth, Boolean success) {
66 log.trace("#{} - authorizing before invoking endpoint {}", methodName);
68 String status = success ? "SUCCESS" : "FAILED";
69 log.trace("#{} - authorizing before invoking endpoint {}, Status: {}", methodName, status);
73 private String getFullPermission(String role) {
74 return AafPermission.getEnumByString(role).getFullPermission();
77 private boolean isPermissionAuthenticationNeeded() {
78 if (configurationManager.getConfiguration().getAafAuthNeeded() && ThreadLocalsHolder.isExternalRequest()) {