1a1adcc54a9a62b8627b344f049e50db24162f5a
[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         switch(params.length) {
45             case 3:
46                 return new AAFPermission(null,params[0],params[1],params[2]);
47             case 4:
48                 return new AAFPermission(params[0],params[1],params[2],params[3]);
49             default:
50                 return new LocalPermission(p);
51         }
52     }
53
54     @Override
55     public boolean fish(Principal bait, Permission ... pond) {
56         boolean rv = false;
57         
58         if (bait instanceof OAuth2Principal) {
59             OAuth2Principal oap = (OAuth2Principal)bait; 
60             for (Permission p : pond ) {
61                 AAFPermission apond = (AAFPermission)p;
62         
63                 TokenPerm tp = oap.tokenPerm();
64                 if (tp==null) {
65                 } else {
66                     for (Permission perm : tp.perms()) {
67                         if (perm.match(apond)) {
68                             return true;
69                         }
70                     }
71                 }
72             }
73         }
74         return rv;
75     }
76
77     @Override
78     public void fishAll(Principal bait, List<Permission> permissions) {
79         OAuth2Principal oap = (OAuth2Principal)bait;
80         TokenPerm tp = oap.tokenPerm();
81         if (tp!=null) {
82             for (AAFPermission p : tp.perms()) {
83                 permissions.add(p);
84             }
85         }
86     }
87
88     @Override
89     public void destroy() {
90     }
91
92     @Override
93     public boolean handlesExclusively(Permission ... pond) {
94         return false;
95     }
96
97     @Override
98     public boolean handles(Principal p) {
99         if (p!=null && p instanceof BearerPrincipal) {
100             return ((BearerPrincipal)p).getBearer()!=null;
101         }
102         return false;
103     }
104
105     @Override
106     public void clear(Principal p, StringBuilder report) {
107         tm.clear(p,report);
108     }
109
110 }