forked from realdadfish/sparkleshare-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.js
142 lines (129 loc) · 3.76 KB
/
middleware.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
var userProvider = null;
var deviceProvider = null;
var folderProvider = null;
var linkCodeProvider = null;
var errors = require('./error');
module.exports = {
setup: function(up, dp, fp, lcp) {
userProvider = up;
deviceProvider = dp;
folderProvider = fp;
linkCodeProvider = lcp;
},
isLogged: function(req, res, next) {
if (req.isAuthenticated()) {
next();
} else {
res.redirect('/login');
}
},
isAdmin: function(req, res, next) {
if (req.user.admin) {
next();
} else {
next(new errors.Permission('Only admin can do this!'));
}
},
owningDevice: function(req, res, next) {
if (req.user.admin || req.loadedDevice.ownerUid == req.user.uid) {
next();
} else {
next(new errors.Permission('You are not admin nor you own this device!'));
}
},
checkFolderAcl: function(req, res, next) {
if (!req.params.folderId || req.user.admin) {
next();
} else {
if (req.user.acl.indexOf(req.params.folderId) >= 0) {
next();
} else {
next(new errors.Permission('You do not have a permission to access this folder'));
}
}
},
loadUser: function(req, res, next) {
if (!req.params.uid) {
next(new errors.NotFound('No user ID specified'));
} else {
userProvider.findByUid(req.params.uid, function(error, user) {
if (error || !user) { return next(new errors.NotFound('User not found!')); }
req.loadedUser = user;
next();
});
}
},
loadDevice: function(req, res, next) {
if (!req.params.did) {
next(new errors.NotFound('No device ID specified'));
} else {
deviceProvider.findById(req.params.did, function(error, device) {
if (error || !device) { return next(new errors.NotFound('Device not found')); }
req.loadedDevice = device;
next();
});
}
},
loadFolder: function(req, res, next) {
if (!req.params.folderId) {
next(new errors.NotFound('No folder specified'));
} else {
folderProvider.findById(req.params.folderId, function(error, folder) {
if (error || !folder) { next(new errors.NotFound('Folder not found')); }
req.loadedFolder = folder;
next();
});
}
},
userDbEmpty: function(req, res, next) {
userProvider.getUserCount(function(error, count) {
if (count < 1) {
next();
} else {
req.flash('error', 'There are already some users. Ask admin for an account');
res.redirect('/login');
}
});
},
validateLinkCode: function(req, res, next) {
var code = req.body.code;
if (code) {
var valid = linkCodeProvider.isCodeValid(code);
if (valid[0]) {
req.linkCodeForUid = valid[1];
next();
} else {
res.send('Invalid link code', 403);
}
} else {
res.send('Invalid link code', 403);
}
},
validateAuthCode: function(req, res, next) {
var ident = req.header('X-SPARKLE-IDENT');
var authCode = req.header('X-SPARKLE-AUTH');
if (!ident || !authCode) {
res.status(403).send('Missing auth code');
} else {
deviceProvider.findByIdent(ident, function(error, device) {
if (!device) {
res.status(403).send('Invalid ident');
} else if (!device.ownerUid) {
res.status(500).send('No device owner');
} else if (device.checkAuthCode(authCode)) {
userProvider.findByUid(device.ownerUid, function(error, user) {
if (error || !user) {
res.status(403).send('Invalid owner');
} else {
req.user = user;
req.currentDevice = device;
next();
}
});
} else {
res.status(403).send('Invalid auth code');
}
});
}
}
};