forked from digideskio/shasthojoy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couch-auth.js
174 lines (161 loc) · 5.07 KB
/
couch-auth.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
var config = require('./config.js'),
nano = require('nano')(config.couch_auth_db_url),
serializer = require('serializer'),
users = nano.use('_users');
function create_oauth_tokens(secret_base, user, callback) {
var consumer_key = serializer.randomString(96);
var token_key = serializer.randomString(96);
user.oauth = {
consumer_keys: {},
tokens: {}
};
user.oauth.consumer_keys[consumer_key] = create_secret(secret_base);
user.oauth.tokens[token_key] = create_secret(secret_base);
users.insert(user, user._id, function(err, response) {
if (err || !response.ok) {
console.log("ERROR Updating user with credentials:");
console.dir(response);
callback(response);
} else {
callback(null, denormalize_oauth(user));
}
});
}
function create_secret(secret_base) {
var encrypt_key = serializer.randomString(48);
var validate_key = serializer.randomString(48);
var secret_string = serializer.secureStringify(secret_base, encrypt_key, validate_key);
if (secret_string.length > 80) {
secret_string = secret_string.substr(30,50);
}
return secret_string;
}
function denormalize_oauth(user) {
var key;
for (key in user.oauth.consumer_keys) {
user.consumer_key = key;
user.consumer_secret = user.oauth.consumer_keys[key];
break;
}
for (key in user.oauth.tokens) {
user.token_key = key;
user.token_secret = user.oauth.tokens[key];
break;
}
return user;
}
function is_admin(user) {
var isAdmin = false;
if (user.roles) {
user.roles.forEach(function(role) {
if (role === 'admin') {
isAdmin = true;
}
});
}
return isAdmin;
}
function validate_oauth(oauth) {
try {
if (Object.keys(oauth.consumer_keys).length > 0 && Object.keys(oauth.tokens).length > 0) {
return true;
}
} catch (ex) {
//oauth is bad, just let the false return;
}
return false;
}
module.exports = {
delete_user: function(user, idToDelete, rev, res) {
if(is_admin(user)) {
users.destroy(idToDelete, rev, function(err, body) {
if (err) {
res.json({error:true, errorResult: err});
} else {
res.json(body);
}
});
}
},
find_oauth_user: function(accessToken, refreshToken, profile, callback) {
var user_key = 'org.couchdb.user:'+profile.emails[0].value;
users.get(user_key, {}, function(err, body) {
if (err) {
if (err.status_code === 404) {
callback();
} else {
callback(err);
}
return;
}
if (validate_oauth(body.oauth)) {
return callback(null, denormalize_oauth(body));
} else {
create_oauth_tokens(accessToken, body, callback);
}
});
},
find_user: function(userName, callback) {
var user_key = userName;
if (user_key.indexOf('org.couchdb.user:') !== 0) {
user_key = 'org.couchdb.user:' + user_key;
}
users.get(user_key, {}, function(err, body) {
if (err) {
callback(err);
return;
}
callback(null, body);
});
},
get_user: function(user, id, res) {
if(is_admin(user)) {
this.find_user(id, function(err, body) {
if (err) {
res.json({error:true, errorResult: err});
} else {
res.json(body);
}
});
}
},
get_users: function(user, res) {
if(is_admin(user)) {
var options = {
include_docs: true,
startkey: 'org.couchdb.user'
};
users.list(options, function (err, body) {
if (err) {
res.json({error:true, errorResult: err});
} else {
res.json(body);
}
});
} else {
res.json({error:true, errorResult: 'Unauthorized'});
}
},
get_primary_role: function(user) {
var primaryRole = '';
if (user.roles) {
user.roles.forEach(function(role) {
if (role !== 'user' && role !== 'admin') {
primaryRole = role;
}
});
}
return primaryRole;
},
update_user: function(user, userData, updateParams, res) {
if(is_admin(user)) {
users.insert(userData, updateParams, function(err, body) {
if (err) {
res.json({error:true, errorResult: err});
} else {
res.json(body);
}
});
}
},
};