Showing posts with label ADF Security. Show all posts
Showing posts with label ADF Security. Show all posts

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();
    }
  }
}



Thursday, November 20, 2014

Authenticating Users using OPSS API in ADF

OPSS is the underlying security platform that provides security to ADF. In some cases it is required to access OPSS API programmatically. Here is the code snippets for authenticating users using OPSS API :
 

import oracle.security.idm.IMException;
import oracle.security.idm.IdentityStore;
import oracle.security.idm.User;
import oracle.security.idm.UserManager;
import oracle.security.jps.JpsContext;
import oracle.security.jps.JpsContextFactory;
import oracle.security.jps.service.idstore.IdentityStoreService;

public class OpssApiExample
{
  public Boolean isUserAuthenticated(String username, String password) 
  {
    Boolean isAuthenticated = Boolean.FALSE;
    try
    {
      JpsContextFactory ctxf = JpsContextFactory.getContextFactory();
      JpsContext ctx = ctxf.getContext();
      IdentityStoreService storeService =
        ctx.getServiceInstance(IdentityStoreService.class);
      IdentityStore idStore = storeService.getIdmStore();
      UserManager userManager = idStore.getUserManager();
      User authUser =null;
      try
      {
        authUser =
          userManager.authenticateUser(username, password.toCharArray());
        isAuthenticated = Boolean.TRUE;
      }
      catch (IMException ime)
      {        
        //Could not authenticate
        ime.printStackTrace(); //Print the authentication error
      }
    }
    catch (Exception exp)
    {
      exp.printStackTrace();
    }   
    return isAuthenticated;
  }
  
}