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