code: public Session impersonate(Credentials credentials) throws LoginException, RepositoryException
     {
  
        String name;
        if (credentials instanceof CredentialsImpl)
        {
           name = ((CredentialsImpl)credentials).getUserID();
        }
        else if (credentials instanceof SimpleCredentials)
        {
           name = ((SimpleCredentials)credentials).getUserID();
        }
        else
        {
           throw new LoginException(
              "Credentials for the authentication should be CredentialsImpl or SimpleCredentials type");
        }
  
        SessionFactory sessionFactory = (SessionFactory)container.getComponentInstanceOfType(SessionFactory.class);
  
        ConversationState newState =
           new ConversationState(new Identity(name, userState.getIdentity().getMemberships(), userState.getIdentity()
              .getRoles()));
        return sessionFactory.createSession(newState);
  
     }
docs: {@inheritDoc}