Published

Wed 13 November 2013

←Home

ADF Essentials and OPSS

ADF essentials release does not include the two IDM jar files that are used for OPSS integration (these are included with normal ADF release) .  Although to use OPSS APIs’ features one does not require a porting effort. I just hope that Oracle can offer these with the ADF essentials release. Here are the reasons why Oracle should do it.

  1. OPSS API’s are Directory server vendor agnostic: The same code works for OID as it works for OpenLdap and Active Directory (you only have to switch the providers).
  2. OPSS API’s are container agnostic: The same code runs on weblogic as on glassfish
  3. ADF essentials lacks a security framework; these API’s could be used to fill the gap.

The following snippet is based on a earlier post of mine which used OID as a provider, this snippet however, is configured for open ldap provider.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package model;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;

import oracle.adf.share.ADFContext;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.share.security.SecurityContext;
import oracle.jbo.JboException;

import oracle.security.idm.ComplexSearchFilter;
import oracle.security.idm.IMException;
import oracle.security.idm.Identity;
import oracle.security.idm.IdentityStore;


import oracle.security.idm.IdentityStoreFactory;
import oracle.security.idm.IdentityStoreFactoryBuilder;
import oracle.security.idm.ModProperty;
import oracle.security.idm.ObjectNotFoundException;
import oracle.security.idm.OperationNotSupportedException;
import oracle.security.idm.Role;
import oracle.security.idm.RoleManager;
import oracle.security.idm.RoleProfile;
import oracle.security.idm.SearchFilter;
import oracle.security.idm.SearchParameters;
import oracle.security.idm.SearchResponse;
import oracle.security.idm.SimpleSearchFilter;
import oracle.security.idm.User;
import oracle.security.idm.UserManager;
import oracle.security.idm.UserProfile;
import oracle.security.idm.providers.oid.OIDIdentityStoreFactory;
import oracle.security.idm.providers.openldap.OLdapIdentityStoreFactory;
/**
 *This class can be used to perform operation on OpenLdap using OPSS API
 * @author Ramandeep Nanda
 */

public class LDAPOperations {
  public static final ADFLogger LDAPLogger=ADFLogger.createADFLogger(LDAPOperations.class);
  private static final ResourceBundle rb =
    ResourceBundle.getBundle("model.myresourcebundle");
    /**
     *
     * @return The store instance for OpenLDAP store
     */
    public static IdentityStore getStoreInstance(){
        return IdentityStoreConfigurator.initializeDefaultStore();
        }
    public static IdentityStoreFactory getIdentityStoreFactory(){
        return IdentityStoreConfigurator.idStoreFactory;
        }


    /**
     * Assigns the specified role to the user
     * @param roleName the role to assign
     * @param userName the user to assign role to
     */
    public static void assignRoleToUser(String roleName,String userName){
       String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
      IdentityStore store = LDAPOperations.getStoreInstance();
        try {
           Role role= store.searchRole(IdentityStore.SEARCH_BY_NAME,roleName);
           User user= store.searchUser(userName);
           RoleManager rm=store.getRoleManager();
          if(!rm.isGranted(role, user.getPrincipal())){
           rm.grantRole(role, user.getPrincipal());
          }

        } catch (IMException e) {
          LDAPLogger.severe("Exception in "+methodName + "Could not assign role ["+roleName+"] to the user ["+userName +"] because of " +e.getMessage() +" ", e);
          throw new JboException("Could not assign role ["+roleName+"] to the user ["+userName +"] due to "+e.getMessage());

        }
        finally  {
            try{
            store.close();
                }
              catch (IMException e) {
              LDAPLogger.severe("Exception occured in closing store");
              }
            }
    }
  /**
     * Assigns the specified role to the user
     * @param roleNames the roles to assign
     * @param userName the user to assign role to
     * @return the set of users who are assigned roles
     */
  public static Set assignRolesToUser(Set roleNames,String userName){
     Set rolesAssigned=new HashSet();

     String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
     IdentityStore store = LDAPOperations.getStoreInstance();
     String roleName=null;
      try {
        User user= store.searchUser(userName);
        Principal userPrincipal=user.getPrincipal();
        RoleManager rm=store.getRoleManager();
        Iterator it=roleNames.iterator();
        while(it.hasNext()){
         roleName=(String)it.next();
         Role role= store.searchRole(IdentityStore.SEARCH_BY_NAME,roleName);
          if(!rm.isGranted(role, user.getPrincipal())){
         rm.grantRole(role,userPrincipal);
         rolesAssigned.add(roleName);
          }
        }
      } catch (IMException e) {

        LDAPLogger.severe("Exception in "+methodName + "Could not assign role ["+roleName+"] to the user ["+userName +"] because of " +e.getMessage() +" ", e);
        throw new JboException("Could not assign role ["+roleName+"] to the user ["+userName +"] due to "+e.getMessage());


      }
    finally  {
        try{
        store.close();
            }
          catch (IMException e) {
          LDAPLogger.severe("Exception occured in closing store");
          }
        }

      return rolesAssigned;
  }
  /**
     * Assigns the specified role to the user
     * @param roleName the role to assign
     * @param users the users to assign role to
     * @return The users who are assigned the role
     */
  public static Set assignRoleToUsers(String roleName,Map users){
     Set usersAssigned=new HashSet();
     String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
     IdentityStore store = LDAPOperations.getStoreInstance();

        Set entrySet = users.entrySet();
        Iterator it=entrySet.iterator();
        String userName=null;

      try {
         Role role= store.searchRole(IdentityStore.SEARCH_BY_NAME,roleName);
         RoleManager rm=store.getRoleManager();
        while(it.hasNext()){
          Map.Entry entry=(Map.Entry)it.next();
          userName=(String)entry.getKey();
          User user= store.searchUser(userName);
            if(!rm.isGranted(role, user.getPrincipal())){
              rm.grantRole(role, user.getPrincipal());
              usersAssigned.add(user);
            }
        }
      } catch (IMException e) {
        LDAPLogger.severe("Exception in "+methodName + "Could not assign role ["+roleName+"] to the user ["+userName +"] because of " +e.getMessage() +" ", e);
       }
    finally  {
        try{
        store.close();
            }
          catch (IMException e) {
          LDAPLogger.severe("Exception occured in closing store");
          }
        }
      return usersAssigned;
  }

//revoke sample below It is similar to the above mentioned assign case so mentioning a sample operation

    /**
     * To remove the role from user
     * @param roleName the role to remove/ revoke
     * @param userName the user from which to revoke role
     */
    public static void removeRoleFromUser(String roleName,String userName){
          String methodName=Thread.currentThread().getStackTrace()[1].getMethodName();
          IdentityStore store = LDAPOperations.getStoreInstance();
           try {
              Role role= store.searchRole(IdentityStore.SEARCH_BY_NAME,roleName);

              User user= store.searchUser(userName);
              RoleManager rm=store.getRoleManager();
             if(rm.isGranted(role, user.getPrincipal())){
              rm.revokeRole(role, user.getPrincipal());
             }
           } catch (IMException e) {
             LDAPLogger.severe("Exception in "+methodName + "Could not revoke role ["+roleName+"] from the user ["+userName +"] because of " +e.getMessage() +" ", e);
             throw new JboException("Could not remove role ["+roleName+"] from the user ["+userName +"] due to "+e.getMessage());

           }
          finally  {
              try{
              store.close();
                  }
                catch (IMException e) {
                LDAPLogger.severe("Exception occured in closing store");
                }
              }
        }
    public static void dropUserWithRoles(String userId){
          UserManager um = null;
          IdentityStore store=null;
          User newUser = null;
           try {
              store = LDAPOperations.getStoreInstance();
              User user = store.searchUser(IdentityStore.SEARCH_BY_NAME, userId);
              um=store.getUserManager();
              if (user != null) {
                  //drop user if already present
                  um.dropUser(user);
                  RoleManager rm = store.getRoleManager();
                  Principal userPrincipal= user.getPrincipal();
                  SearchResponse resp=rm.getGrantedRoles(userPrincipal, true);
                  while(resp.hasNext()){
                  rm.revokeRole((Role)resp.next(), user.getPrincipal());
                  }
                }
          }
          catch (IMException e) {
              LDAPLogger.info("[dropUser]" +

                            e);

          }
          finally  {
              try{
              store.close();
                  }
                catch (IMException e) {
                LDAPLogger.severe("Exception occured in closing store");
                }
              }
        }
  public static void dropUser(String userId){
        UserManager um = null;
        User newUser = null;
        IdentityStore store=null;

        try {
           store = LDAPOperations.getStoreInstance();
            User user = store.searchUser(IdentityStore.SEARCH_BY_NAME, userId);
            um=store.getUserManager();
            if (user != null) {
                //drop user if already present
                um.dropUser(user);
              }
        }
        catch (IMException e) {
            LDAPLogger.info("[dropUser]" +
                          e);

        }
        finally  {
            try{
            store.close();
                }
              catch (IMException e) {
              LDAPLogger.severe("Exception occured in closing store");
              }
            }
      }

    /**
     * Gets the userProfile of the logged in user
     * @param approverUser
     * @return
     */
    public static oracle.security.idm.UserProfile getUserProfile(String approverUser) {
      IdentityStore store = LDAPOperations.getStoreInstance();
       oracle.security.idm.UserProfile profile=null;
        try {
           User user= store.searchUser(approverUser);
           profile=user.getUserProfile();

        } catch (IMException e) {
            LDAPLogger.info("Could not find user in OID with supplied Id"+approverUser);
          throw new JboException(e.getMessage());
        }
      finally  {
          try{
          store.close();
              }
            catch (IMException e) {
            LDAPLogger.severe("Exception occured in closing store");
            }
          }
        return profile;
    }
    /**
     * Gets all the roles
     * @return
     */
    public static List getAllRoles(){
          String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
          List returnList=new ArrayList();
          IdentityStore store = LDAPOperations.getStoreInstance();

          try{
             SimpleSearchFilter filter=store.getSimpleSearchFilter(RoleProfile.NAME,SimpleSearchFilter.TYPE_EQUAL,null);
             String wildCardChar=filter.getWildCardChar();
            // Here the default_role is a property this is just a placeholder can be  any pattern you want to search
            filter.setValue(wildCardChar+rb.getString("DEFAULT_ROLE")+wildCardChar);
                       SearchParameters parameters=new SearchParameters(filter,SearchParameters.SEARCH_ROLES_ONLY) ;
             SearchResponse resp=store.searchRoles(Role.SCOPE_ANY,parameters);
             while(resp.hasNext()){
                 Role role=(Role)resp.next();
                           String tempRole=role.getPrincipal().getName();
                           returnList.add(tempRole);
                          }
             store.close();
           }catch(IMException e){
                 LDAPLogger.severe("Exception in "+methodName + " " +e.getMessage() +" ", e);
                 throw new JboException(e.getMessage());
                 }
          finally  {
              try{
              store.close();
                  }
                catch (IMException e) {
                LDAPLogger.severe("Exception occured in closing store");
                }
              }

           return returnList;
        }
    /**
     * Fetches all the roles assigned to the user
     * @param userName
     * @return
     */
    public static List getAllUserRoles(String userName, String searchPath) {
      String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
      List returnList=new ArrayList();
      IdentityStoreFactory storeFactory = LDAPOperations.getIdentityStoreFactory();
      IdentityStore store=null;
      String[] userSearchBases=   {rb.getString(searchPath)};
      String[] groupSearchBases=     {rb.getString("group.search.bases")};
      Hashtable storeEnv=new Hashtable();
      storeEnv.put(OLdapIdentityStoreFactory.ADF_IM_SUBSCRIBER_NAME,rb.getString("oidsubscribername"));
      storeEnv.put(OLdapIdentityStoreFactory.RT_USER_SEARCH_BASES,userSearchBases);
      storeEnv.put(OLdapIdentityStoreFactory.RT_GROUP_SEARCH_BASES,groupSearchBases);

       try{
          store = storeFactory.getIdentityStoreInstance(storeEnv);
          User user= store.searchUser(IdentityStore.SEARCH_BY_NAME,userName);
          RoleManager mgr=store.getRoleManager();
          SearchResponse resp= mgr.getGrantedRoles(user.getPrincipal(), false);
           while(resp.hasNext()){
              String name= resp.next().getName();
              returnList.add(name);
               }

        }catch(IMException e){
              LDAPLogger.severe("Exception in "+methodName + " " +e.getMessage() +" ", e);
              throw new JboException(e.getMessage());
              }
      finally  {
          try{
          store.close();
              }
            catch (IMException e) {
            LDAPLogger.severe("Exception occured in closing store");
            }
          }

        return returnList;
    }

 /**
*Use to change the passoword for logged in user It uses ADF Security Context to get logged in user
*
**/
  public static void changePasswordForUser(String oldPassword,String newPassword, String userName){
        String methodName =
            java.lang.Thread.currentThread().getStackTrace()[1].getMethodName();
        SecurityContext securityContext =
            ADFContext.getCurrent().getSecurityContext();
        String user = securityContext.getUserName();
      IdentityStore oidStore=null;
         oidStore = LDAPOperations.getStoreInstance();
        try {
            UserManager uMgr = oidStore.getUserManager();
            User authUser =
                uMgr.authenticateUser(user, oldPassword.toCharArray());

            if (authUser != null) {
                UserProfile profile = authUser.getUserProfile();

                profile.setPassword( oldPassword.toCharArray(),
                                    newPassword.toCharArray());
              }
        } catch (IMException e) {
            if (LDAPLogger.isLoggable(Level.SEVERE)) {
                LDAPLogger.severe("[" + methodName +
                                "]  Exception occured due to " + e.getCause(),
                                e);
            }
            throw new JboException(e.getMessage());
        }
      finally  {
          try{
          oidStore.close();
              }
            catch (IMException e) {
            LDAPLogger.severe("Exception occured in closing store");
            }
          }


}
    /**
     * It is used to generate a random password
     *@code{
     * generateRandomPassword();
     * }
     * @return
     */
    public static String generateRandomPassword() {
        String password =
            Long.toHexString(Double.doubleToLongBits(Math.random()));
        int passLength = password.length();
        if (passLength >= 8) {
            password = password.substring(passLength - 8, passLength);
        }

        return password;
    }

/**
* Resets the password for user
*
**/
public static void resetPasswordForUser(String userId)
{
        String methodName =
            java.lang.Thread.currentThread().getStackTrace()[1].getMethodName();
        IdentityStore oidStore = LDAPOperations.getStoreInstance();
        User user = null;
        try {
            user = oidStore.searchUser(userId);
            if (user != null) {
                UserProfile userProfile = user.getUserProfile();
                List passwordValues =
                    userProfile.getProperty("userpassword").getValues();
             ModProperty prop =
                    new ModProperty("PASSWORD", passwordValues.get(0),
                                    ModProperty.REMOVE);
                userProfile.setProperty(prop);
                String randomPassword = generateRandomPassword();
                userProfile.setPassword(null, randomPassword.toCharArray());
            }
        } catch (IMException e) {
            LDAPLogger.severe("[" + methodName + "]" +
                            "Exception occured due to ", e);

        }
      finally  {
          try{
          oidStore.close();
              }
            catch (IMException e) {
            LDAPLogger.severe("Exception occured in closing store");
            }
          }

}


    /**
     * This nested private class is used for configuring and initializing a store instance
     * @author Ramandeep Nanda
     */
  private static final class IdentityStoreConfigurator {
                              private static final IdentityStoreFactory idStoreFactory=initializeFactory();


                             private static IdentityStoreFactory initializeFactory(){
                                       String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
                                       IdentityStoreFactoryBuilder builder = new
                                       IdentityStoreFactoryBuilder();
                                       IdentityStoreFactory oidFactory = null;
                                             try {
                                       Hashtable factEnv = new Hashtable();
                                       factEnv.put(OLdapIdentityStoreFactory.ST_SECURITY_PRINCIPAL,rb.getString("oidusername"));
                                       factEnv.put(OLdapIdentityStoreFactory.ST_SECURITY_CREDENTIALS, rb.getString("oiduserpassword"));
                                       factEnv.put(OLdapIdentityStoreFactory.ST_SUBSCRIBER_NAME,rb.getString("oidsubscribername"));
                                       factEnv.put(OLdapIdentityStoreFactory.ST_LDAP_URL,rb.getString("ldap.url"));
                                       factEnv.put(OLdapIdentityStoreFactory.ST_USER_NAME_ATTR,rb.getString("username.attr"));
                                       oidFactory = builder.getIdentityStoreFactory("oracle.security.idm.providers.openldap.OLdapIdentityStoreFactory", factEnv);
                                          }
                                       catch (IMException e) {
                                               LDAPLogger.severe("Exception in "+methodName + " " +e.getMessage() +" ", e);
                                     //re throw exception here
                                       }
                                      return oidFactory;
                                     }
                             private static  IdentityStore initializeDefaultStore(){
                                       IdentityStore store=null;
                                       String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
                                       String[] userSearchBases=   {rb.getString("user.search.bases")};
                                       String[] groupCreateBases=     {rb.getString("group.search.bases")};
                                       String []usercreate={rb.getString("user.create.bases")};
                                       String [] groupClass={rb.getString("GROUP_CLASSES")};
                                       Hashtable storeEnv=new Hashtable();
                                       storeEnv.put(OLdapIdentityStoreFactory.ADF_IM_SUBSCRIBER_NAME,rb.getString("oidsubscribername"));
                                       storeEnv.put(OLdapIdentityStoreFactory.RT_USER_SEARCH_BASES,userSearchBases);
                                       storeEnv.put(OLdapIdentityStoreFactory.RT_GROUP_SEARCH_BASES,groupCreateBases);
                                       storeEnv.put(OLdapIdentityStoreFactory.RT_USER_CREATE_BASES,usercreate);
                                       storeEnv.put(OLdapIdentityStoreFactory.RT_USER_SELECTED_CREATEBASE,rb.getString("user.create.bases"));
                                       storeEnv.put(OLdapIdentityStoreFactory.RT_GROUP_OBJECT_CLASSES,groupClass);
                                       try{
                                       store = model.LDAPOperations.IdentityStoreConfigurator.idStoreFactory.getIdentityStoreInstance(storeEnv);
                                       }
                               catch (IMException e) {
                                       LDAPLogger.severe("Exception in "+methodName + " " +e.getMessage() +" ", e);
                                     // re throw exception here

            }
          return  store;

        }


}
}

The resource bundle properties used in this code are mentioned below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ldap.url=ldap://localhost:389
user.create.bases=ou=Users,dc=ramannanda,dc=blogspot,dc=com
username.attr=uid
oidusername=cn=Manager,dc=ramannanda,dc=blogspot,dc=com
GROUP_CLASSES=groupOfUniqueNames
#not safe
oiduserpassword=oracle
user.search.bases=ou=users,dc=ramannanda,dc=blogspot,dc=com
group.search.bases=cn=groups,dc=ramannanda,dc=blogspot,dc=com
oidsubscribername=dc=ramannanda,dc=blogspot,dc=com

Here’s the link to earlier post: OPSS API's with OID



Let’s hope they offer the use of these API’s at a nominal cost or include it with ADF essentials for free.

Go Top
comments powered by Disqus