Wednesday, December 24, 2014

Finding User Identity using Identity Governance Framework

The Identity Governance Framework (IGF) enables secure exchange of identity-related information between users and applications and service providers (Oracle docs). Below is the code snippet for finding users based on their attribute values:

import oracle.security.jps.JpsContext;
import oracle.security.jps.JpsContextFactory;
import oracle.security.jps.service.idstore.IdentityStoreService;

import oracle.igf.ids.IdentityDirectory;
import oracle.igf.ids.ResultSet;
import oracle.igf.ids.SearchFilter;
import oracle.igf.ids.SearchOptions;
import oracle.igf.ids.User;
import oracle.igf.ids.UserManager;

public class IGFExample
{
  private void findUsersFromAttributeVal(String attrVal)
  {
    IdentityDirectory ids = null;
    UserManager uMgr = null;
    try
    {
      JpsContext context =
        JpsContextFactory.getContextFactory().getContext();
      IdentityStoreService idstore =
        (IdentityStoreService) context.getServiceInstance(IdentityStoreService.class);
      ids = idstore.getIdentityStore();
      uMgr = ids.getUserManager();

      //We are trying to find users whose description or displayname contains attrValue
      SearchFilter filter =
        new SearchFilter(SearchFilter.LogicalOp.OR, new SearchFilter("description",
                                                                     SearchFilter.Operator.CONTAINS,
                                                                     attrVal),
                         new SearchFilter("displayname",
                                          SearchFilter.Operator.CONTAINS,
                                          attrVal));
      SearchOptions searchOpts = new SearchOptions();
      //Sorting the results by firstname
      searchOpts.setSortAttrs(new String[]
          { "firstname" });
      ResultSet sr = uMgr.searchUsers(filter, searchOpts);
      while (sr.hasMore())
      {
        User user = sr.getNext();
        System.out.println("User Name: " + user.getSubjectName());
        System.out.println("User ID: " + user.getId());
      }
    }
    catch (Exception exp)
    {
      exp.printStackTrace();
    }
  }
}



No comments:

Post a Comment