2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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============================================
20 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.service;
26 import java.security.Principal;
28 import javax.ws.rs.NotAuthorizedException;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.SecurityContext;
32 import org.onap.clamp.clds.util.LoggingUtils;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
38 * Base/abstract Service class. Implements shared security methods.
40 public abstract class SecureServiceBase {
41 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SecureServiceBase.class);
42 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
44 // By default we'll set it to a default handler
45 private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
48 private SecurityContext securityContext;
51 * Get the userId from AAF/CSP.
55 public String getUserId() {
64 public String getUserName() {
65 String name = userNameHandler.retrieveUserName(securityContext);
66 logger.debug("userName={}", name);
71 * Get the principal name.
75 public String getPrincipalName() {
76 Principal principal = securityContext.getUserPrincipal();
77 String name = "Not found";
78 if (principal != null) {
79 name = principal.getName();
81 logger.debug("userPrincipal.getName()={}", name);
86 * Check if user is authorized for the given the permission. Allow matches
87 * if user has a permission with an "*" in permission instance or permission
88 * action even if the permission to check has a specific value in those
89 * fields. For example: if the user has this permission: app-perm-type|*|*
90 * it will be authorized if the inPermission to check is:
91 * app-perm-type|dev|read
95 * @throws NotAuthorizedException
97 public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
98 boolean authorized = false;
99 logger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
100 // check if the user has the permission key or the permission key with a
101 // combination of all instance and/or all action.
102 if (securityContext.isUserInRole(inPermission.getKey())) {
103 logger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
105 // the rest of these don't seem to be required - isUserInRole method
106 // appears to take * as a wildcard
107 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
108 logger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(),
109 inPermission.getKey());
111 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
112 logger.info("{} authorized because user has permission with * for instance and * for action: {}",
113 getPrincipalName(), inPermission.getKey());
115 } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
116 logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(),
117 inPermission.getKey());
120 String msg = getPrincipalName() + " does not have permission: " + inPermission;
121 LoggingUtils.setErrorContext("100", "Authorization Error");
123 throw new NotAuthorizedException(msg);
129 * Check if user is authorized for the given aaf permission. Allow matches
130 * if user has a permission with an "*" in permission instance or permission
131 * action even if the permission to check has a specific value in those
132 * fields. For example: if the user has this permission: app-perm-type|*|*
133 * it will be authorized if the inPermission to check is:
134 * app-perm-type|dev|read
136 * @param aafPermission
138 * @throws NotAuthorizedException
140 public boolean isAuthorizedNoException(SecureServicePermission inPermission) throws NotAuthorizedException {
141 boolean authorized = false;
142 logger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
143 // check if the user has the permission key or the permission key with a
144 // combination of all instance and/or all action.
145 if (securityContext.isUserInRole(inPermission.getKey())) {
146 logger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
148 // the rest of these don't seem to be required - isUserInRole method
149 // appears to take * as a wildcard
150 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
151 logger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(),
152 inPermission.getKey());
154 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
155 logger.info("{} authorized because user has permission with * for instance and * for action: {}",
156 getPrincipalName(), inPermission.getKey());
158 } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
159 logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(),
160 inPermission.getKey());
163 String msg = getPrincipalName() + " does not have permission: " + inPermission;
164 LoggingUtils.setErrorContext("100", "Authorization Error");
170 public static final void setUserNameHandler(UserNameHandler handler) {
171 if (handler != null) {
172 userNameHandler = handler;