[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / user / Cred.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cmd.user;\r
24 \r
25 import com.att.cadi.CadiException;\r
26 import com.att.cadi.LocatorException;\r
27 import com.att.cadi.client.Future;\r
28 import com.att.cadi.client.Rcli;\r
29 import com.att.cadi.client.Retryable;\r
30 import com.att.cmd.AAFcli;\r
31 import com.att.cmd.Cmd;\r
32 import com.att.cmd.Param;\r
33 import com.att.cssa.rserv.HttpMethods;\r
34 import com.att.inno.env.APIException;\r
35 \r
36 import aaf.v2_0.CredRequest;\r
37 \r
38 public class Cred extends Cmd {\r
39                 private static final String CRED_PATH = "/authn/cred";\r
40                 private static final String[] options = {"add","del","reset","extend"/*,"clean"*/};\r
41 //              private Clean clean;\r
42                 public Cred(User parent) {\r
43                         super(parent,"cred",\r
44                                         new Param(optionsToString(options),true),\r
45                                         new Param("id",true),\r
46                                         new Param("password (! D|E)",false),\r
47                                         new Param("entry# (if multi)",false)\r
48                         );\r
49 //                      clean = new Clean(this);\r
50                 }\r
51 \r
52                 @Override\r
53                 public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException { \r
54                     int idx = _idx;\r
55                         String key = args[idx++];\r
56                         final int option = whichOption(options,key);\r
57 \r
58                         final CredRequest cr = new CredRequest();\r
59                         cr.setId(args[idx++]);\r
60                         if(option!=1 && option!=3) {\r
61                                 if(idx>=args.length) throw new CadiException("Password Required");\r
62                                 cr.setPassword(args[idx++]);\r
63                         }\r
64                         if(args.length>idx)\r
65                                 cr.setEntry(args[idx++]);\r
66                         \r
67                         // Set Start/End commands\r
68                         setStartEnd(cr);\r
69 //                      final int cleanIDX = _idx+1;\r
70                         Integer ret = same(new Retryable<Integer>() {\r
71                                 @Override\r
72                                 public Integer code(Rcli<?> client) throws CadiException, APIException {\r
73                                         Future<CredRequest> fp=null;\r
74                                         String verb =null;\r
75                                         switch(option) {\r
76                                                 case 0:\r
77                                                         fp = client.create(\r
78                                                                 CRED_PATH, \r
79                                                                 getDF(CredRequest.class), \r
80                                                                 cr\r
81                                                                 );\r
82                                                         verb = "Added Credential [";\r
83                                                         break;\r
84                                                 case 1:\r
85 //                                                      if(aafcli.addForce())cr.setForce("TRUE");\r
86                                                         setQueryParamsOn(client);\r
87                                                         fp = client.delete(CRED_PATH,\r
88                                                                 getDF(CredRequest.class),\r
89                                                                 cr\r
90                                                                 );\r
91                                                         verb = "Deleted Credential [";\r
92                                                         break;\r
93                                                 case 2:\r
94                                                         fp = client.update(\r
95                                                                 CRED_PATH,\r
96                                                                 getDF(CredRequest.class),\r
97                                                                 cr\r
98                                                                 );\r
99                                                         verb = "Reset Credential [";\r
100                                                         break;\r
101                                                 case 3:\r
102                                                         fp = client.update(\r
103                                                                 CRED_PATH+"/5",\r
104                                                                 getDF(CredRequest.class),\r
105                                                                 cr\r
106                                                                 );\r
107                                                         verb = "Extended Credential [";\r
108                                                         break;\r
109 //                                              case 4:\r
110 //                                                      return clean.exec(cleanIDX, args);\r
111                                         }\r
112                                         if(fp.get(AAFcli.timeout())) {\r
113                                                 pw().print(verb);\r
114                                                 pw().print(cr.getId());\r
115                                                 pw().println(']');\r
116                                         } else if(fp.code()==202) {\r
117                                                         pw().println("Credential Action Accepted, but requires Approvals before actualizing");\r
118                                         } else if(fp.code()==406 && option==1) {\r
119                                                         pw().println("You cannot delete this Credential");\r
120                                         } else {\r
121                                                 error(fp);\r
122                                         }\r
123                                         return fp.code();\r
124                                 }\r
125                         });\r
126                         if(ret==null)ret = -1;\r
127                         return ret;\r
128                 }\r
129                 \r
130                 @Override\r
131                 public void detailedHelp(int _indent, StringBuilder sb) {\r
132                         int indent = _indent;\r
133                         detailLine(sb,indent,"Add, Delete or Reset Credential");\r
134                         indent+=2;\r
135                         detailLine(sb,indent,"id       - the ID to create/delete/reset within AAF");\r
136                         detailLine(sb,indent,"password - Company Policy compliant Password (not required for Delete)");\r
137                         detailLine(sb,indent,"entry    - selected option when deleting/resetting a cred with multiple entries");\r
138                         sb.append('\n');\r
139                         detailLine(sb,indent,"The Domain can be related to any Namespace you have access to *");\r
140                         detailLine(sb,indent,"The Domain is in reverse order of Namespace, i.e. ");\r
141                         detailLine(sb,indent+2,"NS of com.att.myapp can create user of XY1234@myapp.att.com");\r
142                         sb.append('\n');\r
143                         detailLine(sb,indent,"NOTE: AAF does support multiple creds with the same ID. Check with your org if you");\r
144                         detailLine(sb,indent+2,"have this implemented. (For example, this is implemented for MechIDs at AT&T)");\r
145                         sb.append('\n');                        \r
146                         detailLine(sb,indent,"Delegates can be listed by the User or by the Delegate");\r
147                         indent-=2;\r
148                         api(sb,indent,HttpMethods.POST,"authn/cred",CredRequest.class,true);\r
149                         api(sb,indent,HttpMethods.DELETE,"authn/cred",CredRequest.class,false);\r
150                         api(sb,indent,HttpMethods.PUT,"authn/cred",CredRequest.class,false);\r
151                 }\r
152 }\r