forked from Wyverald/net9-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappman.js
88 lines (78 loc) · 2.95 KB
/
appman.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
/* vim: set sw=2 ts=2 nocin si: */
var appbase = require('./appbase-mongo.js'),
crypto = require('crypto');
exports.getAllByUser = function (username, callback) {
appbase.getAllByUser(username, function (success, appsOrErr) {
if (success) callback({ success: true, apps: appsOrErr });
else callback({ success: false, error: appsOrErr });
});
};
exports.checkByName = function (appname, callback) {
appbase.checkByName(appname, function (occupied) {
callback({ name: appname, occupied: occupied });
});
};
exports.generateClientID = function (username, appname) {
var hasher = crypto.createHash('sha1');
hasher.update(username + '/' + appname);
var digest = hasher.digest('base64');
// In base64 notation the digest always ends with '='. So we lose that.
// Also + and / might not be very URL-friendly. We replace those with - and _.
return digest.slice(0, -1).replace(/\+/g, '-').replace(/\//g, '_');
};
exports.register = function (username, appinfo, callback) {
// First make sure that no app by the same name exists.
appbase.checkByName(appinfo.name, function (occupied) {
if (occupied) callback({ success: false, appinfo: appinfo, error: 'app-name-taken' });
else {
appbase.create({
name: appinfo.name,
desc: appinfo.desc,
owners: [username],
clientid: exports.generateClientID(username, appinfo.name),
secret: appinfo.secret
}, function (success, appOrErr) {
if (success) callback({ success: true, appinfo: appOrErr });
else callback({ success: false, appinfo: appinfo, error: appOrErr });
});
}
});
};
exports.getByID = function (clientid, callback) {
appbase.getByID(clientid, function (success, appOrErr) {
if (success) callback({ success: true, appinfo: appOrErr });
else callback({ success: false, error: appOrErr });
});
};
exports.deleteByID = function (clientid, callback) {
appbase.deleteByID(clientid, function (success, err) {
if (success) callback({ success: true });
else callback({ success: false, error: err });
});
};
exports.authenticate = function (appinfo, callback) {
appbase.authenticate(appinfo.clientid, appinfo.secret, function (success, appOrErr) {
if (success) callback({ success: true, appinfo: appOrErr });
else callback({ success: false, error: appOrErr });
});
};
exports.updateInfo = function (appinfo, callback) {
appbase.authenticate(appinfo.clientid, appinfo.oldsecret, function (success, appOrErr) {
if (success) {
appbase.update({
clientid: appinfo.clientid,
name: appinfo.name,
secret: appinfo.newsecret,
desc: appinfo.desc
}, function (success, appOrErr) {
if (success) callback({ success: true, appinfo: appOrErr });
else callback({ success: false, error: appOrErr });
});
} else {
callback({
success: false,
error: appOrErr === 'wrong-secret' ? 'wrong-old-secret' : appOrErr
});
}
});
};