Configuration and Auto-Certificates
[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                         switch(sp.length) {
59                                 case 3:
60                                         temp = new AAFPermission(null,sp[0],sp[1],sp[2]);
61                                         break;
62                                 case 4:
63                                         temp = new AAFPermission(sp[0],sp[1],sp[2],sp[3]);
64                                         break;
65                         }
66                 }
67                 perm=temp;
68         }
69
70         public AAFTrustChecker(final Access access) {
71                 tag = access.getProperty(Config.CADI_USER_CHAIN_TAG, Config.CADI_USER_CHAIN);
72                 id = access.getProperty(Config.CADI_ALIAS,access.getProperty(Config.AAF_APPID,null)); // share between components
73                 String str = access.getProperty(Config.CADI_TRUST_PERM,null);
74                 AAFPermission temp=null;
75                 if(str!=null) {
76                         String[] sp = Split.splitTrim('|', str);
77                         switch(sp.length) {
78                                 case 3:
79                                         temp = new AAFPermission(null,sp[0],sp[1],sp[2]);
80                                         break;
81                                 case 4:
82                                         temp = new AAFPermission(sp[0],sp[1],sp[2],sp[3]);
83                                         break;
84                         }
85                 }
86                 perm=temp;
87         }
88
89         /* (non-Javadoc)
90          * @see org.onap.aaf.cadi.TrustChecker#setLur(org.onap.aaf.cadi.Lur)
91          */
92         @Override
93         public void setLur(Lur lur) {
94                 this.lur = lur;
95         }
96
97         @Override
98         public TafResp mayTrust(TafResp tresp, HttpServletRequest req) {
99                 String user_info = req.getHeader(tag);
100                 if (user_info == null) {
101                         return tresp;
102                 }
103
104                 String[] info = Split.split(',', user_info);
105                 String[] flds = Split.splitTrim(':', info[0]);
106                 if (flds.length < 4) {
107                         return tresp;
108                 }
109                 if (!("AS".equals(flds[3]))) { // is it set for "AS"
110                         return tresp;
111                 }
112
113                 String principalName = tresp.getPrincipal().getName();
114                 if(principalName.equals(id)  // We do trust our own App Components: if a trust entry is made with self, always accept
115                                 || lur.fish(tresp.getPrincipal(), perm)) { // Have Perm set by Config.CADI_TRUST_PERM
116                         String desc = "  " + flds[0] + " validated using " + flds[2] + " by " + flds[1] + ',';
117                         return new TrustTafResp(tresp, new TrustPrincipal(tresp.getPrincipal(), flds[0]), desc);
118                 } else if(principalName.equals(flds[0])) { // Ignore if same identity
119                         return tresp;
120                 } else {
121                         String desc = tresp.getPrincipal().getName() + " requested trust as " + flds[0] + ", but does not have Authorization";
122                         return new TrustNotTafResp(tresp, desc);
123                 }
124         }
125
126 }