Sunday, 17 June 2018

OIM API - How to Remove Assigned Proxy from User?

To execute below code you have to add following jars in classpath:

  • commons-logging.jar

  • eclipselink.jar

  • jrf-api.jar

  • oimclient.jar

  • spring.jar

  • wlfullclient.jar

 

Stand Alone Code:

 

import java.util.HashSet;
import java.util.Hashtable;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;

public class ProxyOps {
    OIMClient oimClient = null;
    UserManager userManager = null;
   
    //identity self service details
    String username = "xelsysadm";
    String password = "<password>"; //xelsysadm password
    String t3url = "t3://<hostname>:<port>"; //OIM HostName and Port
    String authwl_location = "<location of authwl.conf file in your local machine>"; //eg. D:\\authwl.conf
      
    public void getOIMConnection(){
        System.out.println("getOIMConnection() : Start");
        //set system properties
        System.setProperty("java.security.auth.login.config", authwl_location);
        System.setProperty("OIM.AppServerType", "wls");
        System.setProperty("APPSERVER_TYPE", "wls");

        Hashtable oimenv = new Hashtable();
        oimenv.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, "weblogic.jndi.WLInitialContextFactory");
        oimenv.put(OIMClient.JAVA_NAMING_PROVIDER_URL,t3url);
        oimClient = new OIMClient(oimenv);
        try {
            oimClient.login(username, password.toCharArray());
            System.out.println("Connected");
        } catch (LoginException e) {
            e.printStackTrace();
        }
        System.out.println("getOIMConnection() : End");
    }
   
    public void removeUserProxy(String userLogin, String proxyUserLogin) {
        System.out.println("removeUserProxy() : Start");
       
        //get user manager service
        userManager = oimClient.getService(UserManager.class);
       
        try {
            //get proxy user key from user login
            String proxyUserKey = getUserKeyByUserLogin(proxyUserLogin);
           
            System.out.println("UserLogin :: " + userLogin + " " + "ProxyUserKey :: " + proxyUserKey);

            userManager.removeProxy(userLogin, proxyUserKey, true);
        }catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println("removeUserProxy() : End");
    }

    public String getUserKeyByUserLogin(String userLogin){
        HashSet<String> attrsToFetch = new HashSet<String>();
        attrsToFetch.add(UserManagerConstants.AttributeName.USER_KEY.getId());
   
        try{    
            User user = userManager.getDetails(userLogin, attrsToFetch, true);
            return user.getEntityId();
        }catch(Exception e){
            System.out.println("Exception occured while fetching user key");
            return null;
        }
    }
   
    public static void main(String[] args) {
        try {
            ProxyOps obj = new ProxyOps();
            obj.getOIMConnection();
           
            String userLogin = "TestUser1";
            String proxyUserLogin = "ProxyUser1";
           
            obj.removeUserProxy(userLogin, proxyUserLogin);
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

 

Happy Learning!!! 

No comments:

Post a Comment