Changes from Onsite Tests
[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.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.TreeMap;
39
40 import javax.servlet.http.Cookie;
41 import javax.servlet.http.HttpServletRequest;
42
43 import org.onap.aaf.auth.common.Define;
44 import org.onap.aaf.auth.env.AuthzEnv;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.auth.gui.pages.Home;
47 import org.onap.aaf.cadi.Permission;
48 import org.onap.aaf.cadi.aaf.AAFPermission;
49 import org.onap.aaf.cadi.config.Config;
50 import org.onap.aaf.cadi.principal.TaggedPrincipal;
51 import org.onap.aaf.misc.env.APIException;
52 import org.onap.aaf.misc.env.Env;
53 import org.onap.aaf.misc.env.Slot;
54 import org.onap.aaf.misc.env.StaticSlot;
55 import org.onap.aaf.misc.env.util.Split;
56 import org.onap.aaf.misc.xgen.Cache;
57 import org.onap.aaf.misc.xgen.CacheGen;
58 import org.onap.aaf.misc.xgen.Code;
59 import org.onap.aaf.misc.xgen.DynamicCode;
60 import org.onap.aaf.misc.xgen.Mark;
61 import org.onap.aaf.misc.xgen.html.HTMLCacheGen;
62 import org.onap.aaf.misc.xgen.html.HTMLGen;
63 import org.onap.aaf.misc.xgen.html.Imports;
64
65 /**
66  * A Base "Mobile First" Page 
67  * 
68  * @author Jonathan
69  *
70  */
71 public class Page extends HTMLCacheGen {
72     public static final String AAF_THEME = "aaf_theme";
73     public static final String AAFURL_TOOLS = "aaf_url.tools";
74     public static final String AAF_URL_TOOL_DOT = "aaf_url.tool.";
75     public static final String AAF_URL_CUIGUI = "aaf_url.cuigui"; // link to help
76     public static final String AAF_URL_GUI_ONBOARD = "aaf_url.gui_onboard";
77     public static final String AAF_URL_AAF_HELP = "aaf_url.aaf_help";
78     public static final String AAF_URL_CADI_HELP = "aaf_url.cadi_help";
79     public static final String PERM_CA_TYPE = "certman";
80     public static final String PERM_NS = Define.ROOT_NS();
81
82     public static enum BROWSER {iPhone,html5,ie,ieOld};
83
84     public static final int MAX_LINE = 20;
85     protected static final String[] NO_FIELDS = new String[0];
86     private static final String BROWSER_TYPE = "BROWSER_TYPE";
87
88     private final String bcName, bcUrl;
89     private final String[] fields;
90
91     public final boolean no_cache;
92
93     // Note: Only access is synchronized in "getPerm"
94     private final static Map<String,Map<String,Permission>> perms = new HashMap<>();
95
96     public String name() {
97         return bcName;
98     }
99     
100     public String url() {
101         return bcUrl;
102     }
103     
104     public String[] fields() {
105         return fields;
106     }
107     
108     public Page(AuthzEnv env, String name, String url, Enum<?>[] en, final NamedCode ...content) throws APIException, IOException {
109         super(CacheGen.PRETTY, new PageCode(env, 1, content));
110         fields = new String[en.length];
111         int i=-1;
112         for (Enum<?> p : en) {
113             fields[++i]=p.name();
114         }
115
116         bcName = name;
117         bcUrl = url;
118         // Mark which fields must be "no_cache"
119         boolean no_cacheTemp=false;
120         for (NamedCode nc : content) {
121             if (nc.no_cache()) { 
122                 no_cacheTemp=true;
123                 break;
124             }
125         }
126         no_cache=no_cacheTemp;
127     }
128     public Page(AuthzEnv env, String name, String url, String [] fields, final NamedCode ... content) throws APIException,IOException {
129         this(env,name,url,1,fields,content);
130     }
131     
132     public Page(AuthzEnv env, String name, String url, int backdots, String [] fields, final NamedCode ... content) throws APIException,IOException {
133         super(CacheGen.PRETTY, new PageCode(env, backdots, content));
134         if (fields==null) {
135             this.fields = new String[0];
136         } else {
137             this.fields = fields;
138         }
139         bcName = name;
140         bcUrl = url;
141         // Mark which fields must be "no_cache"
142         boolean no_cacheTemp=false;
143         for (NamedCode nc : content) {
144             if (nc.no_cache()) { 
145                 no_cacheTemp=true;
146                 break;
147             }
148         }
149         no_cache=no_cacheTemp;
150     }
151     
152     
153     private static class PageCode implements Code<HTMLGen> {
154                         private static final String AAF_GUI_THEME = "aaf.gui.theme";
155                         private static final String AAF_GUI_TITLE = "aaf_gui_title";
156             
157             private final ContentCode[] content;
158             private final Slot browserSlot;
159             private final int backdots;
160             protected AuthzEnv env;
161             private StaticSlot sTheme;
162                 private static Map<String,List<String>> themes;
163                 private static Map<String,Properties> themeProps;
164
165             public PageCode(AuthzEnv env, int backdots, final ContentCode[] content) {
166                 this.content = content;
167                 this.backdots = backdots;
168                 browserSlot = env.slot(BROWSER_TYPE);
169                 sTheme = env.staticSlot(AAF_GUI.AAF_GUI_THEME);
170                 this.env = env;
171                 getThemeFiles(env,""); //
172             }
173
174             private static synchronized List<String> getThemeFiles(Env env, String theme) {
175                 if(themes==null) {
176                         themes = new TreeMap<>();
177                     File themeD = new File("theme");
178                     if(themeD.exists() && themeD.isDirectory()) {
179                         for (File t : themeD.listFiles()) {
180                                 if(t.isDirectory()) {
181                                         List<String> la = new ArrayList<>();
182                                         for(File f : t.listFiles()) {
183                                                 if(f.isFile()) {
184                                                         if(f.getName().endsWith(".props")) {
185                                                                 Properties props;
186                                                                 if(themeProps == null) {
187                                                                         themeProps = new TreeMap<>();
188                                                                         props = null;
189                                                                 } else {
190                                                                         props = themeProps.get(t.getName());
191                                                                 }
192                                                                 if(props==null) {
193                                                                         props = new Properties();
194                                                                         themeProps.put(t.getName(), props);
195                                                                 }
196                                                                 
197                                                                 try {
198                                                                         FileInputStream fis = new FileInputStream(f);
199                                                                         try {
200                                                                                 props.load(fis);
201                                                                         } finally {
202                                                                                 fis.close();
203                                                                         }
204                                                                 } catch (IOException e) {
205                                                                         env.error().log(e);
206                                                                 }
207                                                         } else {
208                                                                 la.add(f.getName());
209                                                         }
210                                                 }
211                                         }
212                                                 themes.put(t.getName(),la);
213                                 }
214                         }
215                     }
216                 }
217                 return themes.get(theme);
218             }
219             
220             protected Imports getImports(Env env, String theme, int backdots, BROWSER browser) {
221                 List<String> ls = getThemeFiles(env,theme);
222                 Imports imp = new Imports(backdots);
223                         String prefix = "theme/" + theme + '/';
224                         for(String f : ls) {
225                         if(f.endsWith(".js")) {
226                                 imp.js(prefix + f);
227                         } else if(f.endsWith(".css")) {
228                                 if(f.endsWith("iPhone.css")) {
229                                         if(BROWSER.iPhone.equals(browser)) {
230                                                 imp.css(prefix + f);
231                                         }
232                                 } else if (f.endsWith("Desktop.css")){
233                                         if(!BROWSER.iPhone.equals(browser)) {
234                                                 imp.css(prefix + f);
235                                         }
236                                 // Make Console specific to Console page
237                                 } else if (!"console.js".equals(f)) {
238                                         imp.css(prefix + f);
239                                 }
240                         }
241                 }
242                 return imp;
243             }
244             
245             @Override
246             public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
247                 // Note: I found that App Storage saves everything about the page, or not.  Thus, if you declare the page uncacheable, none of the 
248                 // Artifacts, like JPGs are stored, which makes this feature useless for Server driven elements
249                 cache.dynamic(hgen,  new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
250                     @Override
251                     public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
252                         switch(browser(trans,browserSlot)) {
253                             case ieOld:
254                             case ie:
255                                 hgen.directive("!DOCTYPE html");
256                                 hgen.directive("meta", "http-equiv=X-UA-Compatible","content=IE=11");
257                             default:
258                         }
259                     }
260                 });
261                 hgen.html();
262                 final String title = env.getProperty(AAF_GUI_TITLE,"Authentication/Authorization Framework");
263                 final String defaultTheme = env.get(sTheme,"onap"); 
264               
265                 Mark head = hgen.head();
266                     hgen.leaf(TITLE).text(title).end();
267                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
268                         @Override
269                         public void code(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
270                                 BROWSER browser = browser(trans,browserSlot);  
271                                 String theme = null;
272                                 Cookie[] cookies = trans.hreq().getCookies();
273                                 if(cookies!=null) {
274                                         for(Cookie c : cookies) {
275                                                 if(AAF_GUI_THEME.equals(c.getName())) {
276                                                         theme=c.getValue();
277                                                         if(!(themes.containsKey(theme))) {
278                                                                 theme = defaultTheme;
279                                                         }
280                                                         break;
281                                                 }
282                                         }
283                                 }
284                                 
285                                 if(theme==null) {
286                                         for(String t : themes.keySet()) {
287                                                 if(!t.equals(defaultTheme) && trans.fish(new AAFPermission(null,trans.user()+":id", AAF_GUI_THEME, t))) {
288                                                         theme=t;
289                                                         break;
290                                                 }
291                                         }
292                                         if(theme==null) {
293                                                 theme = defaultTheme;
294                                         }
295                                         Cookie cookie = new Cookie(AAF_GUI_THEME,theme);
296                                         cookie.setMaxAge(604_800); // one week
297                                         trans.hresp().addCookie(cookie);
298                                 }
299                                 trans.setProperty(Page.AAF_THEME, theme);
300
301                             hgen.imports(getImports(env,theme,backdots,browser));
302                             switch(browser) {
303                                 case ie:
304                                 case ieOld:
305                                     hgen.js().text("document.createElement('header');")
306                                             .text("document.createElement('nav');")
307                                             .done();
308                                     break;
309                                 default:
310                             }
311                             
312                         }
313                     });
314                     hgen.end(head);
315                     
316                 Mark body = hgen.body();
317                     Mark header = hgen.header();
318                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
319                         @Override
320                         public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen)
321                                 throws APIException, IOException {
322                             // Obtain Server Info, and print
323                             // AT&T Only
324                             String env = trans.getProperty(Config.AAF_ENV,"N/A");
325                             xgen.leaf(H1).text(title + " on " + env).end();
326                             xgen.leaf("p","id=version").text("AAF Version: " + state.deployedVersion).end();
327                             
328                             // Obtain User Info, and print
329                             TaggedPrincipal p = trans.getUserPrincipal();
330                             String user,secured;
331                             if (p==null) {
332                                 user = "please choose a Login Authority";
333                                 secured = "NOT Secure!";
334                             } else {
335                                 user = p.personalName();
336                                 secured = p.tag();
337                             }
338                             xgen.leaf("p","id=welcome").text("Welcome, ")
339                                 .text(user)
340                                 .text("<sup>")
341                                 .text(secured)
342                                 .text("</sup>").end();
343                             
344                             switch(browser(trans,browserSlot)) {
345                                 case ieOld:
346                                 case ie:
347                                     xgen.incr("h5").text("This app is Mobile First HTML5.  Internet Explorer " 
348                                             + " does not support all HTML5 standards. Old, non TSS-Standard versions may not function correctly.").br()
349                                             .text("  For best results, use a highly compliant HTML5 browser like Firefox.")
350                                         .end();
351                                     break;
352                                 default:
353                             }
354                         }
355                     });
356                     
357                     hgen.hr();
358                     
359                     int cIdx;
360                     ContentCode nc;
361                     // If BreadCrumbs, put here
362                     if (content.length>0 && content[0] instanceof BreadCrumbs) {
363                         nc = content[0];
364                         Mark ctnt = hgen.divID(nc.idattrs());
365                         nc.code(cache, hgen);
366                         hgen.end(ctnt);
367                         cIdx = 1;
368                     } else {
369                         cIdx = 0;
370                     }
371                     
372                     hgen.end(header);
373
374                     hgen.divID("pageContent");
375                     Mark inner = hgen.divID("inner");
376                         // Content
377                         for (int i=cIdx;i<content.length;++i) {
378                             nc = content[i];
379                             Mark ctnt = hgen.divID(nc.idattrs());
380                             nc.code(cache, hgen);
381                             hgen.end(ctnt);
382                         }
383
384                     hgen.end(inner);    
385
386                     
387                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
388                         @Override
389                         public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
390                                 String theme = trans.getProperty(Page.AAF_THEME);
391                                 Properties props;
392                                 if(theme==null) {
393                                         props = null;
394                                 } else {
395                                         props = themeProps==null?null:themeProps.get(theme);
396                                 }
397                                 
398                                 if(props!=null && "TRUE".equalsIgnoreCase(props.getProperty("enable_nav_btn"))) {
399                                                 xgen.leaf("button", "id=navBtn").end();
400                                 }
401                         }
402                     });
403                     // Adding "nav Hamburger button"
404                     // Navigation - Using older Nav to work with decrepit   IE versions
405                     Mark nav = hgen.divID("nav");
406                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI,AuthzTrans>() {
407                         @Override
408                         public void code(AAF_GUI state, AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen xgen) throws APIException, IOException {
409                                 String theme = trans.getProperty(Page.AAF_THEME);
410                                 Properties props;
411                                 if(theme==null) {
412                                         props = null;
413                                 } else {
414                                         props = themeProps==null?null:themeProps.get(theme);
415                                 }
416                                 
417                                 if(props!=null) {
418                                         if("TRUE".equalsIgnoreCase(props.getProperty("main_menu_in_nav"))) {
419                                         xgen.incr("h2").text("Navigation").end();
420                                         Mark mark = new Mark();
421                                         boolean selected = isSelected(trans.path(),Home.HREF);
422                                                         //trans.path().endsWith("home");
423                                         xgen.incr(mark,HTMLGen.UL)
424                                                 .incr(HTMLGen.LI,selected?"class=selected":"")
425                                                 .incr(HTMLGen.A, "href=home")
426                                                 .text("Home")
427                                                 .end(2);
428                                         boolean noSelection = !selected;
429                                         for(String[] mi : Home.MENU_ITEMS) {
430                                                 //selected = trans.path().endsWith(mi[0]);
431                                                 if(noSelection) {
432                                                         selected = isSelected(trans.path(),mi[2]);
433                                                         noSelection = !selected;
434                                                 } else {
435                                                         selected = false;
436                                                 }
437                                                 xgen.incr(HTMLGen.LI,selected?"class=selected":"")
438                                                     .incr(HTMLGen.A, "href="+mi[2])
439                                                     .text(mi[1])
440                                                     .end(2);
441                                         }
442                                         xgen.end(mark);
443                                         }
444                                 }
445                         }
446
447                                                 private boolean isSelected(String path, String item) {
448                                                         if(item.equals(path)) {
449                                                                 return true;
450                                                         } else {
451                                                                 for(ContentCode c : content) {
452                                                                         if(c instanceof BreadCrumbs) {
453                                                                                 Page[] bc = ((BreadCrumbs)c).breadcrumbs;
454                                                                                 if(bc!=null) {
455                                                                                         for(int i = bc.length-1;i>0;--i) {
456                                                                                                 if(bc[i].url().equals(item)) {
457                                                                                                         return true;
458                                                                                                 }
459                                                                                         }
460                                                                                         return false;
461                                                                                 }
462                                                                         }
463                                                                 }
464                                                         }
465                                                         return false;
466                                                 }
467                     });
468                     hgen.incr("h2").text("Related Links").end();
469                     hgen.incr(UL);
470                     String aaf_help = env.getProperty(AAF_URL_AAF_HELP,null);
471                     if (aaf_help!=null) {
472                         hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP),"target=_blank").text("AAF WIKI").end(2);
473                         String sub = env.getProperty(AAF_URL_AAF_HELP+".sub");
474                         if (sub!=null) {
475                             hgen.incr(UL,"style=margin-left:5%");
476                             for (String s : Split.splitTrim(',', sub)) {
477                                 hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_AAF_HELP+".sub."+s),"target=_blank").text(s.replace('+', ' ')).end(2);
478                             }
479                             hgen.end();
480                         }
481                     }
482                     aaf_help = env.getProperty(AAF_URL_CADI_HELP,null);
483                     if (aaf_help!=null) {
484                         hgen.leaf(LI).leaf(A,"href="+aaf_help,"target=_blank").text("CADI WIKI").end(2);
485                     }
486                     String tools = env.getProperty(AAFURL_TOOLS);
487                     if (tools!=null) {
488                         hgen.hr()
489                             .incr(HTMLGen.UL,"style=margin-left:5%")
490                              .leaf(HTMLGen.H3).text("Related Tools").end();
491
492                         for (String tool : Split.splitTrim(',',tools)) {
493                             hgen.leaf(LI).leaf(A,"href="+env.getProperty(AAF_URL_TOOL_DOT+tool),"target=_blank").text(tool.replace('+', ' ')).end(2);
494                         }
495                         hgen.end();
496                     }
497                     hgen.end();
498                     
499                     hgen.hr();
500                     
501                     hgen.end(nav);
502                     // Footer - Using older Footer to work with decrepit IE versions
503                     Mark footer = hgen.divID("footer");
504                         hgen.textCR(1, env.getProperty(AAF_GUI.AAF_GUI_COPYRIGHT))
505                         .end(footer);
506                         
507                     hgen.end(body);
508                 hgen.endAll();
509         }
510     }
511
512     public static String getBrowserType() {
513         return BROWSER_TYPE;
514     }
515     
516     /**
517      * It's IE if int >=0
518      * 
519      * Use int found in "ieVersion"
520      * 
521      * Official IE 7
522      *         Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; 
523      *         .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
524      * Official IE 8
525      *         Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; 
526      *         .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)
527      * 
528      * IE 11 Compatibility
529      *         Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; 
530      *         .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)
531      * 
532      * IE 11 (not Compatiblity)
533      *         Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; 
534      *         .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)
535      * 
536      * @param trans
537      * @return
538      */
539     public static BROWSER browser(AuthzTrans trans, Slot slot) {
540         BROWSER br = trans.get(slot, null);
541         if (br==null) {
542             String agent = trans.agent();
543             int msie; 
544             if (agent.contains("iPhone") /* other phones? */) {
545                 br=BROWSER.iPhone;
546             } else if ((msie = agent.indexOf("MSIE"))>=0) {
547                 msie+=5;
548                 int end = agent.indexOf(";",msie);
549                 float ver;
550                 try {
551                     ver = Float.valueOf(agent.substring(msie,end));
552                     br = ver<8f?BROWSER.ieOld:BROWSER.ie;
553                 } catch (Exception e) {
554                     br = BROWSER.ie;
555                 }
556             } else {
557                 br = BROWSER.html5;
558             }
559             trans.put(slot,br);
560         }
561         return br;
562     }
563     
564     /*
565      * Get, rather than create each time, permissions for validations
566      */
567     protected static synchronized Permission getPerm(String instance, String action) {
568         Map<String,Permission> msp = perms.get(instance);
569         Permission p;
570         if (msp==null) {
571             msp = new HashMap<>();
572             perms.put(instance, msp);
573             p=null;
574         } else {
575             p = msp.get(instance);
576         }
577         if (p==null) {
578             p=new AAFPermission(PERM_NS, PERM_CA_TYPE,instance,action);
579             msp.put(action, p);
580         }
581         return p;
582      }
583
584     protected static String getSingleParam(HttpServletRequest req, String tag) {
585         String values[] = req.getParameterValues(tag);
586         return values.length<1?null:values[0];
587     }
588
589 }
590