Generating UUIDs for WSO2 Identity Server creating/provisioning users

Username is the default identifier for users created/provisioned by the WSO2 Identity Server. If you need UUIDs for users you'll have write a custom listener by implementing the "doPostAddUser" method in "UserOperationEventListener" interface to generate UUIDs.

The new implementation of the interface should be bundled as a carbon component so that it can be registered by copying the jar to [IS_HOME]/repository/components/dropins.

Following code chuck provides a sample implementation of the method using the "AbstractUserOperationEventListener"

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.user.core.UserRealm;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.common.AbstractUserOperationEventListener;

import java.util.Map;

public class UserAttributeListener extends AbstractUserOperationEventListener {

    public static final int ID_LENGTH = 30; // set the length of the unique identifier
    private static final Log log = LogFactory.getLog(UserAttributeListener.class);
    private static final int EXECUTION_ORDER = 520; // this is for internal use, can keep as it is
    private static final String CLAIM_URI = "http://wso2.org/claims/uniqueId"; // Claim URI for unique identifier as
    // configured in management console

    @Override
    public boolean doPostAddUser(String userName, Object credential, String[] roleList,
                                 Map<String, String> claims, String profile,
                                 UserStoreManager userStoreManager) throws UserStoreException {

        UserRealm realm = (UserRealm) CarbonContext.getThreadLocalCarbonContext().getUserRealm();
        if (log.isDebugEnabled()) {
            log.debug("Post add user is called in UserAttributeListener");
        }
        try {
            String uniqueId = generateUniqueId();
            realm.getUserStoreManager().setUserClaimValue(userName, CLAIM_URI, uniqueId, profile);
        } catch (UserStoreException e) {
            log.error("Error occurred while adding custom attribute to user : " + userName, e);
        }
        return true;
    }

    /*
        Generates the unique identifier. Customize this method to generate a unique identifier using a preferred way.
     */
    private String generateUniqueId() {
        return RandomStringUtils.randomAlphanumeric(ID_LENGTH);
    }

    @Override
    public int getExecutionOrderId() {
        return EXECUTION_ORDER;
    }
}
You can customize the method "generateUniqueId()" to generate the unique ID as required.
After deploying the bundle you have to configure a custom claim as follows,
  1. Navigate to Configure --> Claim Management in management console
  2. Select "http://wso2.org/claims"
  3. Add New Claim Mapping
  4. Give a display name and description accordingly. Claim URL should be "http://wso2.org/claims/uniqueId". Check both boxes of "Supported by Default" and "Required". Mapped Attribute must be a valid attribute in the underlying user store and the uniquer identifier will be stored in the User store under this attribute of the User
  5. Add the claim
Now when adding Users using any of the methods (Federeated with JIT provisioning, Using Managerment Console and etc), custom listener will be triggered and a Unique identifier will be generated.

Comments

Popular Posts