Created
September 15, 2011 14:04
-
-
Save lwille/1219313 to your computer and use it in GitHub Desktop.
register a new CouchDB user via a simple CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Internal tool script for easy creation of unprivileged CouchDB Users via a simple CLI | |
# prerequisites: | |
# npm install coffee prompt cradle | |
# | |
# invocation: coffee useradd.coffee | |
prompt = require 'prompt' | |
crypto = require 'crypto' | |
cradle = require 'cradle' | |
# connect to couchDB using admin user | |
conn = new cradle.Connection 'localhost', 5984, auth: | |
username: 'admin' | |
password: 'password' | |
prompt.start() | |
prompt.message = 'CouchDB' | |
console.log "Create a CouchDB User" | |
fields = [ | |
message: 'Username' | |
name: 'name' | |
empty: false | |
, | |
message: 'Password' | |
name: 'password' | |
hidden: true | |
empty: false | |
, | |
message: 'Role - reader,admin' | |
name: 'role' | |
default: 'reader' | |
validator: /(reader|admin)/ | |
] | |
prompt.get fields, (err, result)-> | |
salt = crypto.createHash('md5').update(new Date().toString()).update(Math.random().toString()).digest('hex') | |
user = | |
_id: "org.couchdb.user:#{result.name}" | |
type: "user" | |
name: result.name | |
roles: [ result.role ] | |
salt: salt | |
password_sha: crypto.createHash('sha1').update(result.password+salt).digest('hex') | |
console.log "\nGoing to create CouchDB User:", JSON.stringify(user) | |
prompt.get message: 'is this okay?', name: 'ok', default: 'yes', (er, result)-> | |
if result.ok is 'yes' | |
conn.database('_users').save user, (er, res)-> | |
if er | |
console.error "Error creating user:", er | |
process.exit 1 | |
else | |
console.log "Created:", result.ok | |
process.exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment