074b704b95c374a4ee387ed0571cce9c93e4a381
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AAFTrustChecker.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.aaf.v2_0;
23
24 import javax.servlet.http.HttpServletRequest ;
25
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.Lur;
28 import org.onap.aaf.cadi.TrustChecker;
29 import org.onap.aaf.cadi.aaf.AAFPermission;
30 import org.onap.aaf.cadi.config.Config;
31 import org.onap.aaf.cadi.principal.TrustPrincipal;
32 import org.onap.aaf.cadi.taf.TafResp;
33 import org.onap.aaf.cadi.taf.TrustNotTafResp;
34 import org.onap.aaf.cadi.taf.TrustTafResp;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.util.Split;
37
38 public class AAFTrustChecker implements TrustChecker {
39         private final String tag, id;
40         private final AAFPermission perm;
41         private Lur lur;
42
43         /**
44          * 
45          * Instance will be replaced by Identity
46          * @param lur 
47          *    
48          * @param tag
49          * @param perm
50          */
51         public AAFTrustChecker(final Env env) {
52                 tag = env.getProperty(Config.CADI_USER_CHAIN_TAG, Config.CADI_USER_CHAIN);
53                 id = env.getProperty(Config.CADI_ALIAS,env.getProperty(Config.AAF_APPID)); // share between components
54                 String str = env.getProperty(Config.CADI_TRUST_PERM);
55                 AAFPermission temp=null;
56                 if(str!=null) {
57                         String[] sp = Split.splitTrim('|', str);
58                         if(sp.length==3) {
59                                 temp = new AAFPermission(sp[0],sp[1],sp[2]);
60                         }
61                 }
62                 perm=temp;
63         }
64
65         public AAFTrustChecker(final Access access) {
66                 tag = access.getProperty(Config.CADI_USER_CHAIN_TAG, Config.CADI_USER_CHAIN);
67                 id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID,null)); // share between components
68                 String str = access.getProperty(Config.CADI_TRUST_PERM,null);
69                 AAFPermission temp=null;
70                 if(str!=null) {
71                         String[] sp = Split.splitTrim('|', str);
72                         if(sp.length==3) {
73                                 temp = new AAFPermission(sp[0],sp[1],sp[2]);
74                         }
75                 }
76                 perm=temp;
77         }
78
79         /* (non-Javadoc)
80          * @see org.onap.aaf.cadi.TrustChecker#setLur(org.onap.aaf.cadi.Lur)
81          */
82         @Override
83         public void setLur(Lur lur) {
84                 this.lur = lur;
85         }
86
87         @Override
88         public TafResp mayTrust(TafResp tresp, HttpServletRequest req) {
89                 String user_info = req.getHeader(tag);
90                 if(user_info !=null ) {
91                         String[] info = Split.split(',', user_info);
92                         if(info.length>0) {
93                                 String[] flds = Split.splitTrim(':',info[0]);
94                                 if(flds.length>3 && "AS".equals(flds[3])) { // is it set for "AS"
95                                         String pn = tresp.getPrincipal().getName();
96                                         if(pn.equals(id)  // We do trust our own App Components: if a trust entry is made with self, always accept
97                                            || lur.fish(tresp.getPrincipal(), perm)) { // Have Perm set by Config.CADI_TRUST_PERM
98                                                 return new TrustTafResp(tresp,
99                                                                 new TrustPrincipal(tresp.getPrincipal(), flds[0]),
100                                                                 "  " + flds[0] + " validated using " + flds[2] + " by " + flds[1] + ','
101                                                         );
102                                         } else if(pn.equals(flds[0])) { // Ignore if same identity 
103                                                 return tresp;
104                                         } else {
105                                                 return new TrustNotTafResp(tresp, tresp.getPrincipal().getName() + " requested trust as "
106                                                                 + flds[0] + ", but does not have Authorization");
107                                         }
108                                 }
109                         }
110                 }
111                 return tresp;
112         }
113
114 }