package com.mycomp;

import com.ibm.mm.sdk.common.dkCredentialVaultUserExitICM;
import com.ibm.mm.sdk.common.DKNVPair;
import com.ibm.mm.sdk.common.DKException;
import com.ibm.mm.sdk.common.DKConstantICM;
import java.util.StringTokenizer;
import java.util.ArrayList;
import javapasswordsdk.*;
import javapasswordsdk.exceptions.*;

@CyberArkGetPassword
public class MyCyberArkUserExitEX implements dkCredentialVaultUserExitICM{

  // constructor
  public MyCyberArkUserExitEX()
            throws Exception
  {
	System.out.println("MyCyberArkUserExitEX constructor");
  }

 /**
  * Get credential data
  * @param  serverId         server identifier
  * @param  userId           mapped user identifier
  * @param  authentication   mapped user's password
  * @param  options          options for user exit (Note: for future use)
  * @return an array of DKNVPairs with the actual userid and password 
  * information. If null or empty array is returned then no credential
  * information was found for this server identifier.
  * If server identifier is found, the DKNVPair array will contain 2 entries.
  *  DKNVPair 1 - Name: DK_ICM_PARM_CRED_VAULT_USERID Value: <actual userid>
  *  DKNVPair 2 - Name: DK_ICM_PARM_CRED_VAULT_AUTH Value: <actual password>
  *
  **/
    public DKNVPair[] getCredentialData(String serverId, String userid, String authentication, DKNVPair[] options)
      throws Exception
    {
	    DKNVPair[] nvArray = null;
	
	    System.out.println("+ MyCyberArkUserExitEX.getCredentialData(String,String,String)");
        System.out.println(" serverid: " + serverId);

       
        String credCMUser = "";
        String credCMPWD = "";

        if (serverId.equals("")){
	     throw new Exception("Error in MyCyberArkUserExitEX.getCredentialData() during processing");
	    }
        String aAppid = "";
        String aSafe = "";
        String aFolder = "";
        String aObject = "";
        try{
         System.out.println(" Get Cyberark information");

         StringTokenizer tokens = new StringTokenizer(serverId,";",false);
         int tokenNum = 0;
         int eqIndex = 0;
         String strOption = "";
         String strOpName = "";
         String strOpVal = "";
         for(tokenNum = 1; tokens.hasMoreTokens(); tokenNum++){
             strOption = tokens.nextToken().trim();
             eqIndex = strOption.indexOf('=');
             if (eqIndex != -1){
              strOpName = strOption.substring(0,eqIndex);
              strOpVal = strOption.substring(eqIndex+1);
              if (strOpName.equals("APPID")){
                aAppid = strOpVal;
              }
              else if (strOpName.equals("SAFE")){
                aSafe = strOpVal;
              }
              else if (strOpName.equals("FOLDER")){
                aFolder = strOpVal;
              }
             }
         }

         aObject = userid;
         System.out.println(" Get actual credentials from Cyberark");
	     PSDKPasswordRequest passRequest = new PSDKPasswordRequest();
         PSDKPassword password = null;
         passRequest.setAppID (aAppid);
         passRequest.setSafe (aSafe);
         passRequest.setFolder (aFolder);
         passRequest.setObject (aObject);
         password = javapasswordsdk.PasswordSDK.getPassword(passRequest);
         if (password != null){
          nvArray = new DKNVPair[2];
          nvArray[0] = new DKNVPair(DKConstantICM.DK_ICM_PARM_CRED_VAULT_USERID, password.getUserName());
          nvArray[1] = new DKNVPair(DKConstantICM.DK_ICM_PARM_CRED_VAULT_AUTH, password.getContent());
         }
         System.out.println(" Call to Cyberark completed");
	}
        catch(Exception exc){
         System.out.println("Exception message " + exc.getMessage());
        }

        System.out.println("- MyCyberArkUserExitEX.getCredentialData(String,String,String)");
        return nvArray;
    }

}
