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 * ===================================================================
24 package org.onap.clamp.clds.service;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.Date;
30 import javax.ws.rs.NotAuthorizedException;
32 import org.onap.clamp.clds.util.LoggingUtils;
33 import org.springframework.security.core.Authentication;
34 import org.springframework.security.core.GrantedAuthority;
35 import org.springframework.security.core.context.SecurityContext;
36 import org.springframework.security.core.context.SecurityContextHolder;
37 import org.springframework.security.core.userdetails.UserDetails;
40 * Base/abstract Service class. Implements shared security methods.
42 public abstract class SecureServiceBase {
43 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SecureServiceBase.class);
44 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
45 protected static final EELFLogger securityLogger = EELFManager.getInstance().getSecurityLogger();
47 // By default we'll set it to a default handler
48 private static UserNameHandler userNameHandler = new DefaultUserNameHandler();
51 private SecurityContext securityContext = SecurityContextHolder.getContext();
54 * Get the userId from AAF/CSP.
58 public String getUserId() {
67 public String getUserName() {
68 String name = userNameHandler.retrieveUserName(securityContext);
69 Date startTime = new Date();
70 LoggingUtils.setTargetContext("CLDS", "getUserName");
71 LoggingUtils.setTimeContext(startTime, new Date());
72 securityLogger.debug("User logged into the CLDS system={}", name);
77 * Get the principal name.
81 public String getPrincipalName() {
82 String principal = ((UserDetails)securityContext.getAuthentication().getPrincipal()).getUsername();
83 String name = "Not found";
84 if (principal != null) {
87 logger.debug("userPrincipal.getName()={}", name);
92 * Check if user is authorized for the given the permission. Allow matches
93 * if user has a permission with an "*" in permission instance or permission
94 * action even if the permission to check has a specific value in those
95 * fields. For example: if the user has this permission: app-perm-type|*|*
96 * it will be authorized if the inPermission to check is:
97 * app-perm-type|dev|read
100 * The permission to validate
101 * @return A boolean to indicate if the user has the permission to do
102 * execute the inPermission
103 * @throws NotAuthorizedException
104 * In case of issues with the permission test, error is returned
107 public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
108 Date startTime = new Date();
109 LoggingUtils.setTargetContext("CLDS", "isAuthorized");
110 LoggingUtils.setTimeContext(startTime, new Date());
111 securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
113 return isUserPermitted(inPermission);
114 } catch (NotAuthorizedException nae) {
115 String msg = getPrincipalName() + " does not have permission: " + inPermission;
116 LoggingUtils.setErrorContext("100", "Authorization Error");
117 securityLogger.warn(msg);
118 throw new NotAuthorizedException(msg);
123 * Check if user is authorized for the given aaf permission. Allow matches
124 * if user has a permission with an "*" in permission instance or permission
125 * action even if the permission to check has a specific value in those
126 * fields. For example: if the user has this permission: app-perm-type|*|*
127 * it will be authorized if the inPermission to check is:
128 * app-perm-type|dev|read
130 * @param inPermission
131 * The permission to validate
132 * @return A boolean to indicate if the user has the permission to do
133 * execute the inPermission
135 public boolean isAuthorizedNoException(SecureServicePermission inPermission) {
136 securityLogger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
137 Date startTime = new Date();
138 LoggingUtils.setTargetContext("CLDS", "isAuthorizedNoException");
139 LoggingUtils.setTimeContext(startTime, new Date());
141 return isUserPermitted(inPermission);
142 } catch (NotAuthorizedException nae) {
143 String msg = getPrincipalName() + " does not have permission: " + inPermission;
144 LoggingUtils.setErrorContext("100", "Authorization Error");
145 securityLogger.warn(msg);
151 * This method can be used by the Application.class to set the
152 * UserNameHandler that must be used in this class. The UserNameHandler
153 * where to get the User name
156 * The Handler impl to use
158 public static final void setUserNameHandler(UserNameHandler handler) {
159 if (handler != null) {
160 userNameHandler = handler;
164 public void setSecurityContext(SecurityContext securityContext) {
165 this.securityContext = securityContext;
168 private boolean isUserPermitted(SecureServicePermission inPermission) {
169 boolean authorized = false;
170 // check if the user has the permission key or the permission key with a
171 // combination of all instance and/or all action.
172 if (hasRole(inPermission.getKey())) {
173 securityLogger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
175 // the rest of these don't seem to be required - isUserInRole method
176 // appears to take * as a wildcard
177 } else if (hasRole(inPermission.getKeyAllInstance())) {
178 securityLogger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(), inPermission.getKey());
180 } else if (hasRole(inPermission.getKeyAllInstanceAction())) {
181 securityLogger.info("{} authorized because user has permission with * for instance and * for action: {}", getPrincipalName(), inPermission.getKey());
183 } else if (hasRole(inPermission.getKeyAllAction())) {
184 securityLogger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(), inPermission.getKey());
187 throw new NotAuthorizedException("");
192 protected boolean hasRole(String role) {
193 Authentication authentication = securityContext.getAuthentication();
194 if (authentication == null) {
198 for (GrantedAuthority auth : authentication.getAuthorities()) {
199 if (role.equals(auth.getAuthority()))