89816a2c2066dd5b22aa07bff52a39d177440f2e
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / OAuth2Lur.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
22 package org.onap.aaf.cadi.oauth;
23
24 import java.security.Principal;
25 import java.util.List;
26
27 import org.onap.aaf.cadi.Lur;
28 import org.onap.aaf.cadi.Permission;
29 import org.onap.aaf.cadi.aaf.AAFPermission;
30 import org.onap.aaf.cadi.lur.LocalPermission;
31 import org.onap.aaf.cadi.principal.BearerPrincipal;
32 import org.onap.aaf.misc.env.util.Split;
33
34 public class OAuth2Lur implements Lur {
35         private TokenMgr tm;
36
37         public OAuth2Lur(TokenMgr tm) {
38                 this.tm = tm;
39         }
40         
41         @Override
42         public Permission createPerm(String p) {
43                 String[] params = Split.split('|', p);
44                 if(params.length==3) {
45                         return new AAFPermission(params[0],params[1],params[2]);
46                 } else {
47                         return new LocalPermission(p);
48                 }
49         }
50
51         @Override
52         public boolean fish(Principal bait, Permission pond) {
53                 AAFPermission apond = (AAFPermission)pond;
54                 OAuth2Principal oap;
55                 if(bait instanceof OAuth2Principal) {
56                         oap = (OAuth2Principal)bait; 
57                 } else {
58                         // Here is the spot to put in Principal Conversions
59                         return false;
60                 }
61
62                 TokenPerm tp = oap.tokenPerm();
63                 if(tp==null) {
64                 } else {
65                         for(Permission p : tp.perms()) {
66                                 if(p.match(apond)) {
67                                         return true;
68                                 }
69                         }
70                 }
71                 return false;
72         }
73
74         @Override
75         public void fishAll(Principal bait, List<Permission> permissions) {
76                 OAuth2Principal oap = (OAuth2Principal)bait;
77                 TokenPerm tp = oap.tokenPerm();
78                 if(tp!=null) {
79                         for(AAFPermission p : tp.perms()) {
80                                 permissions.add(p);
81                         }
82                 }
83         }
84
85         @Override
86         public void destroy() {
87         }
88
89         @Override
90         public boolean handlesExclusively(Permission pond) {
91                 return false;
92         }
93
94         @Override
95         public boolean handles(Principal p) {
96                 if(p!=null && p instanceof BearerPrincipal) {
97                         return ((BearerPrincipal)p).getBearer()!=null;
98                 }
99                 return false;
100         }
101
102         @Override
103         public void clear(Principal p, StringBuilder report) {
104                 tm.clear(p,report);
105         }
106
107 }