346c8ae21083292078be02836482e2f9cbc2be63
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / Page.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.auth.gui;
23
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.A;
25 import static org.onap.aaf.misc.xgen.html.HTMLGen.H1;
26 import static org.onap.aaf.misc.xgen.html.HTMLGen.LI;
27 import static org.onap.aaf.misc.xgen.html.HTMLGen.TITLE;
28 import static org.onap.aaf.misc.xgen.html.HTMLGen.UL;
29
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.onap.aaf.auth.common.Define;
37 import org.onap.aaf.auth.env.AuthzEnv;
38 import org.onap.aaf.auth.env.AuthzTrans;
39 import org.onap.aaf.auth.rserv.CachingFileAccess;
40 import org.onap.aaf.cadi.Permission;
41 import org.onap.aaf.cadi.aaf.AAFPermission;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.cadi.principal.TaggedPrincipal;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Slot;
46 import org.onap.aaf.misc.env.StaticSlot;
47 import org.onap.aaf.misc.env.util.Split;
48 import org.onap.aaf.misc.xgen.Cache;
49 import org.onap.aaf.misc.xgen.CacheGen;
50 import org.onap.aaf.misc.xgen.Code;
51 import org.onap.aaf.misc.xgen.DynamicCode;
52 import org.onap.aaf.misc.xgen.Mark;
53 import org.onap.aaf.misc.xgen.html.HTMLCacheGen;
54 import org.onap.aaf.misc.xgen.html.HTMLGen;
55 import org.onap.aaf.misc.xgen.html.Imports;
56
57 /**
58  * A Base "Mobile First" Page 
59  * 
60  * @author Jonathan
61  *
62  */
63 public class Page extends HTMLCacheGen {
64         public static final String AAFURL_TOOLS = "aaf_url.tools";
65         public static final String AAF_URL_TOOL_DOT = "aaf_url.tool.";
66         public static final String AAF_URL_CUIGUI = "aaf_url.cuigui"; // link to help
67         public static final String AAF_URL_GUI_ONBOARD = "aaf_url.gui_onboard";
68         public static final String AAF_URL_AAF_HELP = "aaf_url.aaf_help";
69         public static final String AAF_URL_CADI_HELP = "aaf_url.cadi_help";
70         public static final String PERM_CA_TYPE = Define.ROOT_NS() + ".ca";
71
72         public static enum BROWSER {iPhone,html5,ie,ieOld};
73         
74         public static final int MAX_LINE=20;
75
76         protected static final String[] NO_FIELDS = new String[0];
77
78         private static final String BROWSER_TYPE = "BROWSER_TYPE";
79
80         private final String bcName, bcUrl;
81         private final String[] fields;
82
83         public final boolean no_cache;
84
85         // Note: Only access is synchronized in "getPerm"
86         private final static Map<String,Map<String,Permission>> perms = new HashMap<>();
87
88         public String name() {
89                 return bcName;
90         }
91         
92         public String url() {
93                 return bcUrl;
94         }
95         
96         public String[] fields() {
97                 return fields;
98         }
99         
100         public Page(AuthzEnv env, String name, String url, Enum<?>[] en, final NamedCode ...content) throws APIException, IOException {
101                 super(CacheGen.PRETTY, new PageCode(env, 1, content));
102                 fields = new String[en.length];
103                 int i=-1;
104                 for(Enum<?> p : en) {
105                         fields[++i]=p.name();
106                 }
107
108                 bcName = name;
109                 bcUrl = url;
110                 // Mark which fields must be "no_cache"
111                 boolean no_cacheTemp=false;
112                 for(NamedCode nc : content) {
113                         if(nc.no_cache()) { 
114                                 no_cacheTemp=true;
115                                 break;
116                         }
117                 }
118                 no_cache=no_cacheTemp;
119         }
120         public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
121                 this(env,name,url,1,fields,content);
122         }
123         
124         public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
125                 super(CacheGen.PRETTY, new PageCode(env, backdots, content));
126                 if(fields==null) {
127                         this.fields = new String[0];
128                 } else {
129                         this.fields = fields;
130                 }
131                 bcName = name;
132                 bcUrl = url;
133                 // Mark which fields must be "no_cache"
134                 boolean no_cacheTemp=false;
135                 for(NamedCode nc : content) {
136                         if(nc.no_cache()) { 
137                                 no_cacheTemp=true;
138                                 break;
139                         }
140                 }
141                 no_cache=no_cacheTemp;
142         }
143         
144         
145         private static class PageCode implements Code<HTMLGen> {
146                         private static final String AAF_GUI_TITLE = "aaf_gui_title";
147                         
148                         private final ContentCode[] content;
149                         private final Slot browserSlot;
150                         private final int backdots;
151                         protected AuthzEnv env;
152                         private StaticSlot sTheme;
153
154                         public PageCode(AuthzEnv env, int backdots, final ContentCode[] content) {
155                                 this.content = content;
156                                 this.backdots = backdots;
157                                 browserSlot = env.slot(BROWSER_TYPE);
158                                 sTheme = env.staticSlot(CachingFileAccess.CFA_WEB_PATH);
159                                 this.env = env;
160                         }
161                         
162                         @Override
163                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
164                                 // Note: I found that App Storage saves everything about the page, or not.  Thus, if you declare the page uncacheable, none of the 
165                                 // Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
166                                 cache.dynamic(hgen,  new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
167                                         @Override
168                                         public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
169                                                 switch(browser(trans,browserSlot)) {
170                                                         case ieOld:
171                                                         case ie:
172                                                                 hgen.directive("!DOCTYPE html");
173                                                                 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
174                                                         default:
175                                                 }
176                                         }
177                                 });
178                                 hgen.html();
179                                 final String title = env.getProperty(AAF_GUI_TITLE,"Authentication/Authorization Framework");
180                                 final String theme = env.get(sTheme); 
181                                 Mark head = hgen.head();
182                                         hgen.leaf(TITLE).text(title).end();
183                                         hgen.imports(new Imports(backdots).css(theme + "/aaf5.css")
184                                                                                                 .js(theme + "/comm.js")
185                                                                                                 .js(theme + "/console.js")
186                                                                                                 .js(theme + "/common.js"));
187                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
188                                                 @Override
189                                                 public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
190                                                         switch(browser(trans,browserSlot)) {
191                                                                 case iPhone:
192                                                                         hgen.imports(new Imports(backdots).css(theme + "/aaf5iPhone.css"));
193                                                                         break;
194                                                                 case ie:
195                                                                 case ieOld:
196                                                                         hgen.js().text("document.createElement('header');")
197                                                                                         .text("document.createElement('nav');")
198                                                                                         .done();
199                                                                 case html5:
200                                                                         hgen.imports(new Imports(backdots).css(theme + "/aaf5Desktop.css"));
201                                                                         break;
202                                                         }
203                                                 }
204                                         });
205                                         hgen.end(head);
206                                         
207                                 Mark body = hgen.body();
208                                         Mark header = hgen.header();
209                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
210                                                 @Override
211                                                 public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
212                                                                 throws APIException, IOException {
213                                                         // Obtain Server Info, and print
214                                                         // AT&T Only
215                                                         String env = trans.getProperty(Config.AAF_ENV,"N/A");
216                                                         xgen.leaf(H1).text(title + " on " + env).end();
217                                                         xgen.leaf("p","id=version").text("AAF Version: " + state.deployedVersion).end();
218                                                         
219                                                         // Obtain User Info, and print
220                                                         TaggedPrincipal p = trans.getUserPrincipal();
221                                                         String user,secured;
222                                                         if(p==null) {
223                                                                 user = "please choose a Login Authority";
224                                                                 secured = "NOT Secure!";
225                                                         } else {
226                                                                 user = p.personalName();
227                                                                 secured = p.tag();
228                                                         }
229                                                         xgen.leaf("p","id=welcome").text("Welcome, ")
230                                                                 .text(user)
231                                                                 .text("<sup>")
232                                                                 .text(secured)
233                                                                 .text("</sup>").end();
234                                                         
235                                                         switch(browser(trans,browserSlot)) {
236                                                                 case ieOld:
237                                                                 case ie:
238                                                                         xgen.incr("h5").text("This app is Mobile First HTML5.  Internet Explorer " 
239                                                                                         + " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
240                                                                                         .text("  For best results, use a highly compliant HTML5 browser like Firefox.")
241                                                                                 .end();
242                                                                         break;
243                                                                 default:
244                                                         }
245                                                 }
246                                         });
247                                         
248                                         hgen.hr();
249                                         
250                                         int cIdx;
251                                         ContentCode nc;
252                                         // If BreadCrumbs, put here
253                                         if(content.length>0 && content[0] instanceof BreadCrumbs) {
254                                                 nc = content[0];
255                                                 Mark ctnt = hgen.divID(nc.idattrs());
256                                                 nc.code(cache, hgen);
257                                                 hgen.end(ctnt);
258                                                 cIdx = 1;
259                                         } else {
260                                                 cIdx = 0;
261                                         }
262                                         
263                                         hgen.end(header);
264                                         
265                                         Mark inner = hgen.divID("inner");
266                                                 // Content
267                                                 for(int i=cIdx;i<content.length;++i) {
268                                                         nc = content[i];
269                                                         Mark ctnt = hgen.divID(nc.idattrs());
270                                                         nc.code(cache, hgen);
271                                                         hgen.end(ctnt);
272                                                 }
273
274                                         hgen.end(inner);        
275                                         
276                                         // Navigation - Using older Nav to work with decrepit  IE versions
277                                         
278                                         Mark nav = hgen.divID("nav");
279                                         hgen.incr("h2").text("Related Links").end();
280                                         hgen.incr(UL);
281                                         String aaf_help = env.getProperty(AAF_URL_AAF_HELP,null);
282                                         if(aaf_help!=null) {
283                                                 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP),"target=_blank").text("AAF WIKI").end(2);
284                                                 String sub = env.getProperty(AAF_URL_AAF_HELP+".sub");
285                                                 if(sub!=null) {
286                                                         hgen.incr(UL,"style=margin-left:5%");
287                                                         for(String s : Split.splitTrim(',', sub)) {
288                                                                 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP+".sub."+s),"target=_blank").text(s.replace('+', ' ')).end(2);
289                                                         }
290                                                         hgen.end();
291                                                 }
292                                         }
293                                         aaf_help = env.getProperty(AAF_URL_CADI_HELP,null);
294                                         if(aaf_help!=null) {
295                                                 hgen.leaf(LI).leaf(A,"href="+aaf_help,"target=_blank").text("CADI WIKI").end(2);
296                                         }
297                                         String tools = env.getProperty(AAFURL_TOOLS);
298                                         if(tools!=null) {
299                                                 hgen.hr()
300                                                         .incr(HTMLGen.UL,"style=margin-left:5%")
301                                                         .leaf(HTMLGen.H3).text("Related Tools").end();
302
303                                                 for(String tool : Split.splitTrim(',',tools)) {
304                                                         hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_TOOL_DOT+tool),"target=_blank").text(tool.replace('+', ' ')).end(2);
305                                                 }
306                                                 hgen.end();
307                                         }
308                                         hgen.end();
309                                         
310                                         hgen.hr();
311                                         
312                                         hgen.end(nav);
313                                         // Footer - Using older Footer to work with decrepit IE versions
314                                         Mark footer = hgen.divID("footer");
315                                                 hgen.textCR(1, env.getProperty(AAF_GUI.AAF_GUI_COPYRIGHT))
316                                                 .end(footer);
317                                                 
318                                         hgen.end(body);
319                                 hgen.endAll();
320                 }
321         }
322
323         public static String getBrowserType() {
324                 return BROWSER_TYPE;
325         }
326         
327         /**
328          * It's IE if int >=0
329          * 
330          * Use int found in "ieVersion"
331          * 
332          * Official IE 7
333          *              Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; 
334          *              .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
335          * Official IE 8
336          *              Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; 
337          *              .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; ATT)
338          * 
339          * IE 11 Compatibility
340          *              Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; 
341          *              .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; InfoPath.3; HVD; ATT)
342          * 
343          * IE 11 (not Compatiblity)
344          *              Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; 
345          *              .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; InfoPath.3; HVD; ATT)
346          * 
347          * @param trans
348          * @return
349          */
350         public static BROWSER browser(AuthzTrans trans, Slot slot) {
351                 BROWSER br = trans.get(slot, null);
352                 if(br==null) {
353                         String agent = trans.agent();
354                         int msie; 
355                         if(agent.contains("iPhone") /* other phones? */) {
356                                 br=BROWSER.iPhone;
357                         } else if ((msie = agent.indexOf("MSIE"))>=0) {
358                                 msie+=5;
359                                 int end = agent.indexOf(";",msie);
360                                 float ver;
361                                 try {
362                                         ver = Float.valueOf(agent.substring(msie,end));
363                                         br = ver<8f?BROWSER.ieOld:BROWSER.ie;
364                                 } catch (Exception e) {
365                                         br = BROWSER.ie;
366                                 }
367                         } else {
368                                 br = BROWSER.html5;
369                         }
370                         trans.put(slot,br);
371                 }
372                 return br;
373         }
374         
375         /*
376          * Get, rather than create each time, permissions for validations
377          */
378         protected static synchronized Permission getPerm(String instance, String action) {
379                 Map<String,Permission> msp = perms.get(instance);
380                 Permission p;
381                 if(msp==null) {
382                         msp = new HashMap<>();
383                         perms.put(instance, msp);
384                         p=null;
385                 } else {
386                         p = msp.get(instance);
387                 }
388                 if(p==null) {
389                         p=new AAFPermission(PERM_CA_TYPE,instance,action);
390                         msp.put(action, p);
391                 }
392                 return p;
393         }
394
395         protected static String getSingleParam(HttpServletRequest req, String tag) {
396                 String values[] = req.getParameterValues(tag);
397                 return values.length<1?null:values[0];
398         }
399
400
401 }
402