-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_restrict.js
166 lines (160 loc) · 5.33 KB
/
security_restrict.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
// Define the restricts function by Roney
async function restricts(opts) {
const { auth, req, res } = this;
const provider = auth[opts.provider];
let roles = [];
if (!provider) {
throw new Error('Provider not found');
}
if (provider.identity === false) {
if (opts.loginUrl) {
if (req.fragment) {
res.status(222).send(opts.loginUrl);
} else {
res.redirect(opts.loginUrl);
}
} else {
if (provider.basicAuth) {
res.set('WWW-Authenticate', `Basic Realm="${provider.basicRealm}"`);
}
res.sendStatus(401);
}
return;
}
// Dynamic Permissions
const dynamicPermissions = Array.isArray(opts.dynamicPermissions) ?
opts.dynamicPermissions :
opts.dynamicPermissions.split(',').map(permission => permission.trim());
if (opts.condition === 'OR') {
for (let permission of dynamicPermissions) {
if (!provider.perms[permission]) {
if (opts.forbiddenUrl) {
if (req.fragment) {
res.status(222).send(opts.forbiddenUrl);
} else {
res.redirect(opts.forbiddenUrl);
}
} else {
res.sendStatus(403);
}
}
let perm = provider.perms[permission];
let table = perm.table || provider.users.table;
let ident = perm.identity || provider.users.identity;
let roleColumn = perm.roleColumn || 'role'; // Assuming the role column is named 'role'
let results = await provider.db
.select(ident, roleColumn)
.from(table)
.where(ident, provider.identity)
.where(function () {
for (let condition of perm.conditions) {
if (condition.operator == 'in') {
this.orWhereIn(condition.column, condition.value);
} else if (condition.operator == 'not in') {
this.orWhereNotIn(condition.column, condition.value);
} else if (condition.operator == 'is null') {
this.orWhereNull(condition.column);
} else if (condition.operator == 'is not null') {
this.orWhereNotNull(condition.column);
} else {
this.orWhere(condition.column, condition.operator, condition.value);
}
}
});
if (results.length) {
const role = results.map((result) => result.role);
roles.push(...role);
return true;
} else {
// Forbidden
if (opts.forbiddenUrl) {
if (req.fragment) {
res.status(222).send(opts.forbiddenUrl);
} else {
res.redirect(opts.forbiddenUrl);
}
} else {
res.sendStatus(403);
}
}
}
let cookieOptions = {
domain: opts.domain || undefined,
httpOnly: true,
maxAge: (opts.expires || 30) * 24 * 60 * 60 * 1000, // from days to ms
path: opts.path || '/',
secure: !!opts.secure,
sameSite: opts.sameSite || 'Strict',
signed: true
};
provider.app.setCookie('Roles', roles.join(','), cookieOptions)
} else if (opts.condition === 'AND') {
for (let permission of dynamicPermissions) {
if (!provider.perms[permission]) {
if (opts.forbiddenUrl) {
if (req.fragment) {
res.status(222).send(opts.forbiddenUrl);
} else {
res.redirect(opts.forbiddenUrl);
}
} else {
res.sendStatus(403);
}
}
let perm = provider.perms[permission];
let table = perm.table || provider.users.table;
let ident = perm.identity || provider.users.identity;
let roleColumn = perm.roleColumn || 'role'; // Assuming the role column is named 'role'
let results = await provider.db
.select(ident, roleColumn)
.from(table)
.where(ident, provider.identity)
.where(function () {
for (let condition of perm.conditions) {
if (condition.operator == 'in') {
this.whereIn(condition.column, condition.value);
} else if (condition.operator == 'not in') {
this.whereNotIn(condition.column, condition.value);
} else if (condition.operator == 'is null') {
this.whereNull(condition.column);
} else if (condition.operator == 'is not null') {
this.whereNotNull(condition.column);
} else {
this.where(condition.column, condition.operator, condition.value);
}
}
});
const role = results.map((result) => result.role);
roles.push(...role);
if (results.length == 0) {
// Forbidden
if (opts.forbiddenUrl) {
if (req.fragment) {
res.status(222).send(opts.forbiddenUrl);
} else {
res.redirect(opts.forbiddenUrl);
}
} else {
res.sendStatus(403);
}
}
}
let cookieOptions = {
domain: opts.domain || undefined,
httpOnly: true,
maxAge: (opts.expires || 30) * 24 * 60 * 60 * 1000, // from days to ms
path: opts.path || '/',
secure: !!opts.secure,
sameSite: opts.sameSite || 'Strict',
signed: true
};
provider.app.setCookie('Roles', roles.join(','), cookieOptions)
} else {
// Handle invalid condition
throw new Error('Invalid condition specified');
}
}
// Export the AuthProvider class and restricts function
module.exports = {
restricts
};