Search code examples
javascriptjsonopenidm

OpenIDM update users attributes over custom endpoint


I'm trying to develop a custom js endpoint in OpenIDM in which I update the user searched with two attributes that I generate (otpexpiry and otpvalue) in my script.

I added a json conf in openidm/conf/endpoint-otp.json to link:

{
 "context" : "endpoint/otp/*",
 "type" : "text/javascript",
 "file" : "script/otp.js"
}

And this is my script openidm/script/otp.js:

(function() {
 if(request.method === "update") {
  var five_minutes = 5 * 60 * 1000;
  var timestamp = new Date().getTime();
  var otpexpiry = timestamp + five_minutes;
  var otpvalue = Math.floor(Math.random() * 9999);


  /* Not sure of this code below I have to update the user searched with " otpdate : otpexpiry " and "otp : otpvalue "*/
  var u = request.value;
  var id = "managed/user/" + u._id;
  if (id != null) {
    openidm['update']( ... );
  }

  return {
       method: "update",
       resourceName: request.resourcePath,
       revision: request.revision,
       parameters: request.additionalParameters,
       patch: request.patchpperations,
       context: context.current
       };
  } else {
       throw { code: 500, message: "Unknown request type " + request.method};
  }
})();

How can I update two variables of the user searched?


Solution

  • For anyone interested in how I achieved this, here below is my code. I simply pass data from a POST, I query for the user and then I use openidm.patch method for update the attributes.

    (function(){
        var content = request.content;
            java.lang.System.out.println(content);
    
            var userID = content.id;
            var userEmail = content.email;
            java.lang.System.out.println("User ID: " + userID);
            java.lang.System.out.println("User Email: " + userEmail);
    
            var qry = {
                    '_queryFilter' : '/mail eq "' + userEmail + '"'
            };
            var userFound = openidm.query("managed/user", qry);
    
            if (userFound != null) {
                    java.lang.System.out.println(userFound);
    
                    /*Generate OTP and Expiry */
                    var five_minutes = 5 * 60 * 1000;
                    var timestamp = new Date().getTime();
                    var otpexpiry = timestamp + five_minutes;
                    var otpvalue = Math.floor(Math.random() * 9999);
    
                    java.lang.System.out.println("OTP Valore: " + otpvalue);
                    java.lang.System.out.println("Scadenza: " + otpexpiry);
    
                    var patch = [{ "operation" : "replace", "field" : "/otp", "value" : otpvalue },{ "operation" : "replace", "field" : "/otpdate" , "value" : otpexpiry }];
    
                    openidm.patch("managed/user/" + userID, null, patch);
    
                    return true;
    
            }
    
    })();
    

    You can trigger this endpoint with a POST method:

    curl -k \
    --header "X-OpenIDM-Username: xxx" \
    --header "X-OpenIDM-Password: xxx" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" \
    --data '{ \
    "id" : "2d141539-50b6-4637-bfe8-24a1d61fa556", \
    "email" : "[email protected]" }' \
    --request POST "https://localhost:8443/openidm/endpoint/otp"