60ea11f7a7f3dbc4df1bfee9ee5e080a2dd57fa8
[aaf/cadi.git] / shiro / src / main / java / org / onap / aaf / cadi / shiro / AAFAuthorizationInfo.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21 package org.onap.aaf.cadi.shiro;
22
23 import java.security.Principal;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.apache.log4j.Logger;
29 import org.apache.shiro.authz.AuthorizationInfo;
30 import org.apache.shiro.authz.Permission;
31 import org.onap.aaf.cadi.Access;
32
33 /**
34  * We treat "roles" and "permissions" in a similar way for first pass.
35  * 
36  * @author JonathanGathman
37  *
38  */
39 public class AAFAuthorizationInfo implements AuthorizationInfo {
40         
41         final static Logger logger = Logger.getLogger(AuthorizationInfo.class);
42         
43         private static final long serialVersionUID = -4805388954462426018L;
44         private Access access;
45         private Principal bait;
46         private List<org.onap.aaf.cadi.Permission> pond;
47         private ArrayList<String> sPerms;
48         private ArrayList<Permission> oPerms;
49
50         public AAFAuthorizationInfo(Access access, Principal bait, List<org.onap.aaf.cadi.Permission> pond) {
51                 this.access = access;
52                 this.bait = bait;
53                 this.pond = pond;
54                 sPerms=null;
55                 oPerms=null;
56         }
57         
58         public Principal principal() {
59                 return bait;
60         }
61         
62         @Override
63         public Collection<Permission> getObjectPermissions() {
64                 logger.debug("AAFAuthorizationInfo.getObjectPermissions");
65                 synchronized(bait) {
66                         if(oPerms == null) {
67                                 oPerms = new ArrayList<Permission>(); 
68                                 for(final org.onap.aaf.cadi.Permission p : pond) {
69                                         oPerms.add(new AAFShiroPermission(p));
70                                 }
71                         }
72                 }
73                 return oPerms;
74         }
75
76         @Override
77         public Collection<String> getRoles() {
78                 logger.debug("AAFAuthorizationInfo.getRoles");
79                 // Until we decide to make Roles available, tie into String based permissions.
80                 return getStringPermissions();
81         }
82
83         @Override
84         public Collection<String> getStringPermissions() {
85                 logger.debug("AAFAuthorizationInfo.getStringPermissions");
86                 synchronized(bait) {
87                         if(sPerms == null) {
88                                 sPerms = new ArrayList<String>(); 
89                                 for(org.onap.aaf.cadi.Permission p : pond) {
90                                         sPerms.add(p.getKey().replace("|",":"));
91                                 }
92                         }
93                 }
94                 return sPerms;
95         }
96
97 }