19a90c3e76f558e67bbcde253d11bbafb65cd994
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / NsInfoAction.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.text.ParseException;
9
10 import com.att.authz.env.AuthzTrans;
11 import com.att.authz.gui.AuthGUI;
12 import com.att.authz.gui.BreadCrumbs;
13 import com.att.authz.gui.NamedCode;
14 import com.att.authz.gui.Page;
15 import org.onap.aaf.cadi.CadiException;
16 import org.onap.aaf.cadi.client.Future;
17 import org.onap.aaf.cadi.client.Rcli;
18 import org.onap.aaf.cadi.client.Retryable;
19 import org.onap.aaf.inno.env.APIException;
20 import org.onap.aaf.inno.env.Env;
21 import org.onap.aaf.inno.env.Slot;
22 import org.onap.aaf.inno.env.TimeTaken;
23 import org.onap.aaf.inno.env.util.Chrono;
24 import com.att.xgen.Cache;
25 import com.att.xgen.DynamicCode;
26 import com.att.xgen.html.HTMLGen;
27
28 import aaf.v2_0.CredRequest;
29
30 public class NsInfoAction extends Page {
31         public NsInfoAction(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
32                 super(gui.env,"Onboard",PassChangeForm.HREF, PassChangeForm.fields,
33                         new BreadCrumbs(breadcrumbs),
34                         new NamedCode(true,"content") {
35                                 final Slot sID = gui.env.slot(PassChangeForm.NAME+'.'+PassChangeForm.fields[0]);
36                                 final Slot sCurrPass = gui.env.slot(PassChangeForm.NAME+'.'+PassChangeForm.fields[1]);
37                                 final Slot sPassword = gui.env.slot(PassChangeForm.NAME+'.'+PassChangeForm.fields[2]);
38                                 final Slot sPassword2 = gui.env.slot(PassChangeForm.NAME+'.'+PassChangeForm.fields[3]);
39                                 final Slot startDate = gui.env.slot(PassChangeForm.NAME+'.'+PassChangeForm.fields[4]);
40                                 
41                                 @Override
42                                 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
43                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI, AuthzTrans>() {
44                                                 @Override
45                                                 public void code(final AuthGUI gui, final AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
46                                                         String id = trans.get(sID,null);
47                                                         String currPass = trans.get(sCurrPass,null);
48                                                         String password = trans.get(sPassword,null);
49                                                         String password2 = trans.get(sPassword2,null);
50                                                         
51                                                         // Run Validations
52                                                         boolean fail = true;
53                                                         
54                                                         if (id==null || id.indexOf('@')<=0) {
55                                                                 hgen.p("Data Entry Failure: Please enter a valid ID, including domain.");
56                                                         } else if(password == null || password2 == null || currPass == null) {
57                                                                 hgen.p("Data Entry Failure: Both Password Fields need entries.");
58                                                         } else if(!password.equals(password2)) {
59                                                                 hgen.p("Data Entry Failure: Passwords do not match.");
60                                                         } else { // everything else is checked by Server
61                                                                 final CredRequest cred = new CredRequest();
62                                                                 cred.setId(id);
63                                                                 cred.setPassword(currPass);
64                                                                 try {
65                                                                         fail = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
66                                                                                 @Override
67                                                                                 public Boolean code(Rcli<?> client)throws CadiException, ConnectException, APIException {
68                                                                                         TimeTaken tt = trans.start("Check Current Password",Env.REMOTE);
69                                                                                         try {
70                                                                                                 Future<CredRequest> fcr = client.create( // Note: Need "Post", because of hiding password in SSL Data
71                                                                                                                         "/authn/validate",
72                                                                                                                         gui.credReqDF,
73                                                                                                                         cred
74                                                                                                                 );
75                                                                                                 boolean go;
76                                                                                                 boolean fail = true;
77                                                                                                 fcr.get(5000);
78                                                                                                 if(fcr.code() == 200) {
79                                                                                                         hgen.p("Current Password validated");
80                                                                                                         go = true;
81                                                                                                 } else {
82                                                                                                         hgen.p(String.format("Invalid Current Password: %d %s",fcr.code(),fcr.body()));
83                                                                                                         go = false;
84                                                                                                 }
85                                                                                                 if(go) {
86                                                                                                         tt.done();
87                                                                                                         tt = trans.start("AAF Change Password",Env.REMOTE);
88                                                                                                         try {
89                                                                                                                 // Change over Cred to reset mode
90                                                                                                                 cred.setPassword(password);
91                                                                                                                 String start = trans.get(startDate, null);
92                                                                                                                 if(start!=null) {
93                                                                                                                         try {
94                                                                                                                                 cred.setStart(Chrono.timeStamp(Chrono.dateOnlyFmt.parse(start)));
95                                                                                                                         } catch (ParseException e) {
96                                                                                                                                 throw new CadiException(e);
97                                                                                                                         }
98                                                                                                                 }
99                                                                                                                 
100                                                                                                                 fcr = client.create(
101                                                                                                                                 "/authn/cred",
102                                                                                                                                 gui.credReqDF,
103                                                                                                                                 cred
104                                                                                                                                 );
105                                         
106                                                                                                                 if(fcr.get(5000)) {
107                                                                                                                         // Do Remote Call
108                                                                                                                         hgen.p("New Password has been added.");
109                                                                                                                         fail = false;
110                                                                                                                 } else {
111                                                                                                                         gui.writeError(trans, fcr, hgen);
112                                                                                                                 }
113                                                                                                         } finally {
114                                                                                                                 tt.done();
115                                                                                                         }
116                                                                                                 }
117                                                                                                 return fail;
118                                                                                         } finally {
119                                                                                                 tt.done();
120                                                                                         }
121                                                                                 }
122                                                                         });
123
124                                                                 } catch (Exception e) {
125                                                                         hgen.p("Unknown Error");
126                                                                         e.printStackTrace();
127                                                                 }
128                                                         }
129                                                 hgen.br();
130                                                 if(fail) {
131                                                         hgen.incr("a",true,"href="+PassChangeForm.HREF+"?id="+id).text("Try again").end();
132                                                 } else {
133                                                         hgen.incr("a",true,"href="+Home.HREF).text("Home").end(); 
134                                                 }
135                                         }
136                                 });
137                         }
138                 });
139         }
140 }