2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.cmd.user;
24 import java.util.List;
26 import org.onap.aaf.auth.cmd.AAFcli;
27 import org.onap.aaf.auth.cmd.Cmd;
28 import org.onap.aaf.auth.cmd.Param;
29 import org.onap.aaf.auth.rserv.HttpMethods;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.aaf.client.ErrMessage;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.misc.env.APIException;
38 import aaf.v2_0.CredRequest;
39 import aaf.v2_0.Error;
41 public class Cred extends Cmd {
42 public static final String ATTEMPT_FAILED_SPECIFICS_WITHELD = "Attempt Failed. Specifics witheld.";
43 private static final String CRED_PATH = "/authn/cred";
44 private static final String[] options = {"add","del","reset","extend"/*,"clean"*/};
45 private ErrMessage em;
46 // private RosettaDF<Error> errDF;
47 public Cred(User parent) throws APIException {
49 new Param(optionsToString(options),true),
51 new Param("password (! D|E)",false),
52 new Param("entry# (if multi)",false)
54 em = new ErrMessage(aafcli.env());
58 public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException {
60 String key = args[idx++];
61 final int option = whichOption(options,key);
63 final CredRequest cr = new CredRequest();
64 cr.setId(args[idx++]);
65 if (option!=1 && option!=3) {
66 if (idx>=args.length) throw new CadiException("Password Required");
67 cr.setPassword(args[idx++]);
69 if (args.length>idx) {
70 cr.setEntry(args[idx]);
73 // Set Start/End commands
75 Integer ret = same(new Retryable<Integer>() {
77 public Integer code(Rcli<?> client) throws CadiException, APIException {
78 Future<CredRequest> fp=null;
84 getDF(CredRequest.class),
87 verb = "Added Credential [";
90 setQueryParamsOn(client);
91 fp = client.delete(CRED_PATH,
92 getDF(CredRequest.class),
95 verb = "Deleted Credential [";
100 getDF(CredRequest.class),
103 verb = "Reset Credential [";
108 getDF(CredRequest.class),
111 verb = "Extended Credential [";
117 return null; // get by Sonar check.
119 if (fp.get(AAFcli.timeout())) {
121 pw().print(cr.getId());
123 } else if (fp.code()==202) {
124 pw().println("Credential Action Accepted, but requires Approvals before actualizing");
125 } else if (fp.code()==300 || fp.code()==406) {
126 Error err = em.getError(fp);
127 String text = err.getText();
128 List<String> vars = err.getVariables();
130 // IMPORTANT! We do this backward, because it is looking for string
131 // %1 or %13. If we replace %1 first, that messes up %13
132 for(int i=vars.size()-1;i>0;--i) {
133 text = text.replace("%"+(i+1), (i<10?" ":"") + i+") " + vars.get(i));
136 text = text.replace("%1",vars.get(0));
138 } else if (fp.code()==406 && option==1) {
139 pw().println("You cannot delete this Credential");
140 } else if (fp.code()==409 && option==0) {
141 pw().println("You cannot add two Passwords for same day");
143 pw().println(ATTEMPT_FAILED_SPECIFICS_WITHELD);
148 if (ret==null)ret = -1;
153 public void detailedHelp(int _indent, StringBuilder sb) {
154 int indent = _indent;
155 detailLine(sb,indent,"Add, Delete or Reset Credential");
157 detailLine(sb,indent,"id - the ID to create/delete/reset within AAF");
158 detailLine(sb,indent,"password - Company Policy compliant Password (not required for Delete)");
159 detailLine(sb,indent,"entry - selected option when deleting/resetting a cred with multiple entries");
161 detailLine(sb,indent,"The Domain can be related to any Namespace you have access to *");
162 detailLine(sb,indent,"The Domain is in reverse order of Namespace, i.e. ");
163 detailLine(sb,indent+2,"NS of com.att.myapp can create user of XY1234@myapp.att.com");
165 detailLine(sb,indent,"NOTE: AAF does support multiple creds with the same ID. Check with your org if you");
166 detailLine(sb,indent+2,"have this implemented. (For example, this is implemented for MechIDs at AT&T)");
168 detailLine(sb,indent,"*NOTE: com.att.csp is a reserved Domain for Global Sign On");
170 detailLine(sb,indent,"Delegates can be listed by the User or by the Delegate");
172 api(sb,indent,HttpMethods.POST,"authn/cred",CredRequest.class,true);
173 api(sb,indent,HttpMethods.DELETE,"authn/cred",CredRequest.class,false);
174 api(sb,indent,HttpMethods.PUT,"authn/cred",CredRequest.class,false);