[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / service / SecureServiceBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
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;
33
34 /**
35  * Base/abstract Service class.
36  * Implements shared security methods.
37  */
38 public abstract class SecureServiceBase {
39     private static final Logger logger = LoggerFactory.getLogger(SecureServiceBase.class);
40
41     @Context
42     private SecurityContext securityContext;
43
44     /**
45      * Get the userid
46      *
47      * @return
48      */
49     public String getUserid() {
50         return getPrincipalName();
51     }
52
53     /**
54      * Get the principal name.
55      *
56      * @return
57      */
58     public String getPrincipalName() {
59         Principal p = securityContext.getUserPrincipal();
60         String name = "Not found";
61         if (p != null) {
62             name = p.getName();
63         }
64         logger.debug("userPrincipal.getName()={}", name);
65         return name;
66     }
67
68     /**
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
75      *
76      * @param inPermission
77      * @return
78      * @throws NotAuthorizedException
79      */
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());
86             authorized = true;
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());
90             authorized = true;
91         } else if (securityContext.isUserInRole(inPermission.getKeyAllInstanceAction())) {
92             logger.info("{} authorized because user has permission with * for instance and * for action: {}", getPrincipalName(), inPermission.getKey());
93             authorized = true;
94         } else if (securityContext.isUserInRole(inPermission.getKeyAllAction())) {
95             logger.info("{} authorized because user has permission with * for action: {}", getPrincipalName(), inPermission.getKey());
96             authorized = true;
97         } else {
98             String msg = getPrincipalName() + " does not have permission: " + inPermission;
99             logger.warn(msg);
100             throw new NotAuthorizedException(msg);
101         }
102         return authorized;
103     }
104
105 }