Update shiro bundle & fix sidecar
[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.shiro.authz.AuthorizationInfo;
29 import org.apache.shiro.authz.Permission;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.aaf.AAFPermission;
33
34 /**
35  * We treat "roles" and "permissions" in a similar way for first pass.
36  * 
37  * @author JonathanGathman
38  *
39  */
40 public class AAFAuthorizationInfo implements AuthorizationInfo {
41         private static final long serialVersionUID = -4805388954462426018L;
42         
43         private Access access;
44         private Principal bait;
45         // Use these to save conversions
46         private List<org.onap.aaf.cadi.Permission> cPerms;
47         private List<Permission> oPerms;
48         private List<String> sPerms;
49
50         public AAFAuthorizationInfo(Access access, Principal bait) {
51                 this.access = access;
52                 this.bait = bait;
53                 cPerms=null;
54                 oPerms=null;
55                 sPerms=null;
56         }
57         
58         public Principal principal() {
59                 return bait;
60         }
61         
62         @Override
63         public Collection<Permission> getObjectPermissions() {
64                 access.log(Level.DEBUG, "AAFAuthorizationInfo.getObjectPermissions");
65                 synchronized(bait) {
66                         if(oPerms == null) {
67                                 oPerms = new ArrayList<>();
68                                 if(cPerms==null) {
69                                         cPerms = new ArrayList<>();
70                                         AAFRealm.singleton.authz.fishAll(bait, cPerms);
71                                 }
72                                 for(final org.onap.aaf.cadi.Permission p : cPerms) {
73                                         oPerms.add(new AAFShiroPermission(p));
74                                 }
75                         }
76                 }
77                 return oPerms;
78         }
79
80         @Override
81         public Collection<String> getRoles() {
82                 access.log(Level.DEBUG,"AAFAuthorizationInfo.getRoles");
83                 // Until we decide to make Roles available, tie into String based permissions.
84                 return getStringPermissions();
85         }
86
87         @Override
88         public Collection<String> getStringPermissions() {
89                 access.log(Level.DEBUG,"AAFAuthorizationInfo.getStringPermissions");
90                 synchronized(bait) {
91                         if(sPerms == null) {
92                                 sPerms = new ArrayList<>();
93                                 if(cPerms==null) {
94                                         cPerms = new ArrayList<>();
95                                         AAFRealm.singleton.authz.fishAll(bait,cPerms);
96                                 }
97                                 for(final org.onap.aaf.cadi.Permission p : cPerms) {
98                                         sPerms.add(p.getKey());
99                                 }
100                         }
101                 }
102                 return sPerms;
103         }
104
105 }