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