-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
184 lines (164 loc) · 5.94 KB
/
index.js
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
// Generated by IcedCoffeeScript 108.0.9
var CloudantUser, cradle, crypto, extend, iced, __iced_k, __iced_k_noop,
__slice = [].slice;
iced = require('iced-runtime');
__iced_k = __iced_k_noop = function() {};
cradle = require("cradle");
crypto = require("crypto");
extend = (require("util"))._extend;
CloudantUser = (function() {
CloudantUser.prototype.defaultRoles = ["_default", "_reader", "_creator", "_writer"];
function CloudantUser(server) {
var _base, _base1, _ref;
this.server = server;
if (!((_ref = this.server) != null ? _ref.host : void 0)) {
throw new Error("server.host required");
}
if (!this.server.auth.username) {
throw new Error("server.auth.username required");
}
if ((_base = this.server).secure == null) {
_base.secure = /https/.exec(this.server.host);
}
(_base1 = this.server).port || (_base1.port = this.server.secure ? 443 : 80);
this.server.cache = false;
this.server.timeout = 5000;
this.connect();
}
CloudantUser.prototype.couchUser = function(name) {
return "org.couchdb.user:" + name;
};
CloudantUser.prototype.connect = function() {
this.conn = new cradle.Connection(this.server);
return this.db = this.conn.database("_users");
};
CloudantUser.prototype.create = function() {
var cb, name, password, roles, _i;
name = arguments[0], password = arguments[1], roles = 4 <= arguments.length ? __slice.call(arguments, 2, _i = arguments.length - 1) : (_i = 2, []), cb = arguments[_i++];
return this.createWithMeta.apply(this, [name, password].concat(__slice.call(roles), [null], [cb]));
};
CloudantUser.prototype.createWithMeta = function() {
var cb, doc, err, hashAndSalt, metadata, name, password, roles, user, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
name = arguments[0], password = arguments[1], roles = 5 <= arguments.length ? __slice.call(arguments, 2, _i = arguments.length - 2) : (_i = 2, []), metadata = arguments[_i++], cb = arguments[_i++];
if (metadata == null) {
metadata = {};
}
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/cloudant-user/src/index.coffee",
funcname: "CloudantUser.createWithMeta"
});
_this.exists(name, __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return doc = arguments[1];
};
})(),
lineno: 33
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
if (doc !== false) {
return cb(err || {
error: "user_exists"
});
}
if (!roles.length) {
roles = _this.defaultRoles;
}
hashAndSalt = _this.generatePasswordHash(password);
user = {
name: name,
password_sha: hashAndSalt[0],
salt: hashAndSalt[1],
password_scheme: "simple",
type: "user",
roles: roles
};
extend(user, metadata);
return _this.db.save(_this.couchUser(name), user, cb);
};
})(this));
};
CloudantUser.prototype.npmCreate = function() {
var cb, email, name, password, roles, _i;
name = arguments[0], password = arguments[1], email = arguments[2], roles = 5 <= arguments.length ? __slice.call(arguments, 3, _i = arguments.length - 1) : (_i = 3, []), cb = arguments[_i++];
return this.createWithMeta.apply(this, [name, password].concat(__slice.call(roles), [{
email: email
}], [cb]));
};
CloudantUser.prototype.get = function(name, callback) {
return this.db.get(this.couchUser(name), callback);
};
CloudantUser.prototype.exists = function(name, cb) {
var doc, err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
__iced_k = __iced_k_noop;
___iced_passed_deferral = iced.findDeferral(arguments);
(function(_this) {
return (function(__iced_k) {
__iced_deferrals = new iced.Deferrals(__iced_k, {
parent: ___iced_passed_deferral,
filename: "/home/charles/source/cloudant-user/src/index.coffee",
funcname: "CloudantUser.exists"
});
_this.db.get(_this.couchUser(name), __iced_deferrals.defer({
assign_fn: (function() {
return function() {
err = arguments[0];
return doc = arguments[1];
};
})(),
lineno: 56
}));
__iced_deferrals._fulfill();
});
})(this)((function(_this) {
return function() {
if ((typeof err !== "undefined" && err !== null ? err.error : void 0) === "not_found") {
return cb(err, false);
} else if (doc) {
return cb(null, doc);
} else {
return cb(err);
}
};
})(this));
};
CloudantUser.prototype.update = function(name, props, cb) {
var hashAndSalt, password;
if (!props) {
cb({
error: "properties for user required"
});
}
password = props.password;
if (password) {
delete props.password;
hashAndSalt = this.generatePasswordHash(password);
props.password_sha = hashAndSalt[0];
props.salt = hashAndSalt[1];
props.password_scheme = "simple";
}
return this.db.merge(this.couchUser(name), props, cb);
};
CloudantUser.prototype.remove = function(name, callback) {
return this.db.remove(name, callback);
};
CloudantUser.prototype.generatePasswordHash = function(password) {
var hash, salt;
salt = (crypto.randomBytes(16)).toString("hex");
hash = crypto.createHash("sha1");
hash.update(password + salt);
return [hash.digest("hex"), salt];
};
return CloudantUser;
})();
module.exports = CloudantUser;
//# sourceMappingURL=index.js.map