Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change password in couchdb 1.2 via the api?

Tags:

couchdb

Does anyone know if it's possible for me to change a password in couchdb 1.2? To create a user I have a form that gets a user's information and then posts to the _users database like this ('users' in the url below is proxied):

// Create a user
var userObj = {
     _id: "org.couchdb.user:test",
     type: "user",
     name: "test",
     roles: ["user"],
     emailAddress: "[email protected]",
     firstName: "Test",
     lastName: "Test",
     password: "password"
};
$.ajax({
    url: "/users/org.couchdb.user:test",
    type: "PUT",
    dataType: "json",
    contentType:"application/json",
    data: JSON.stringify(userObj)
});

Couchdb 1.2 generates the password hash and salt for me and stores the user. It works great. To update the password, I tried retrieving the user, deleting the password_sha and salt fields, adding a password field and then reposting the document. I was hoping Couch would just recalculate the password_sha and salt fields for me and update the document, but it doesn't. The password_sha and salt fields aren't updated.

// Update a user
$.get("/users/org.couchdb.user:test")
.done(function(userDoc){
  delete userDoc.password_sha;
  delete userDoc.salt
  userDoc.password = "test";
  $.ajax({
    url: "/users/org.couchdb.user:test",
    type: "PUT",
    dataType: "json",
    contentType:"application/json",
    data: userDoc
  });
});

I suspect couch will only generate the password_sha and salt fields when the document is created. If that's the case, should I just generate my own password_sha and salt fields and post those in the updated doc instead? Am I missing something here?

Thanks!

like image 694
Troy Avatar asked Nov 04 '22 01:11

Troy


1 Answers

Never mind. I was trying to update a JSON string...doh! Just need to parse userDoc, add a password, and then resubmit.

like image 181
Troy Avatar answered Nov 13 '22 02:11

Troy