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 org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import javax.ws.rs.NotAuthorizedException;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.SecurityContext;
32 import java.security.Principal;
35 * Base/abstract Service class.
36 * Implements shared security methods.
38 public abstract class SecureServiceBase {
39 private static final Logger logger = LoggerFactory.getLogger(SecureServiceBase.class);
42 private SecurityContext securityContext;
49 public String getUserid() {
50 return getPrincipalName();
54 * Get the principal name.
58 public String getPrincipalName() {
59 Principal p = securityContext.getUserPrincipal();
60 String name = "Not found";
64 logger.debug("userPrincipal.getName()={}", name);
69 * Check if user is authorized for the given the permission.
70 * Allow matches if user has a permission with an "*" in permission instance
71 * or permission action even if the permission to check has a specific value
72 * in those fields. For example:
73 * if the user has this permission: app-perm-type|*|*
74 * it will be authorized if the inPermission to check is: app-perm-type|dev|read
78 * @throws NotAuthorizedException
80 public boolean isAuthorized(SecureServicePermission inPermission) throws NotAuthorizedException {
81 boolean authorized = false;
82 logger.debug("checking if {} has permission: {}", getPrincipalName(), inPermission);
83 // check if the user has the permission key or the permission key with a combination of all instance and/or all action.
84 if (securityContext.isUserInRole(inPermission.getKey())) {
85 logger.info("{} authorized for permission: {}", getPrincipalName(), inPermission.getKey());
87 // the rest of these don't seem to be required - isUserInRole method appears to take * as a wildcard
88 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstance())) {
89 logger.info("{} authorized because user has permission with * for instance: {}", getPrincipalName(), inPermission.getKey());
91 } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
92 logger.info("{} authorized because user has permission with * for instance and * for action: {}", getPrincipalName(), inPermission.getKey());
94 } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
95 logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(), inPermission.getKey());
98 String msg = getPrincipalName() + " does not have permission: " + inPermission;
100 throw new NotAuthorizedException(msg);