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
|
"use strict";
const regexes = {
token: /^[a-zA-Z0-9-_]{6,128}$/, // UUID
};
const get_data = async (req, res, next) => {
const token = String(req.get("X-VAULT-SESSION") || "");
if (!token.match(/^[a-zA-Z0-9-_]{6,128}$/)) {
res.status(400).json({
status: "error",
error: "missing session key",
});
req.app.locals.logger.warn(`Vault.account: blocked an attempt to bypass authentication [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
return;
}
const session = req.app.locals.session.get(token);
if (session === null || session === undefined) {
res.status(410).json({
status: "error",
error: "session expired",
});
req.app.locals.cooldown(req, 5e3);
return;
}
if (session.authenticated !== true) {
res.status(401).json({
status: "error",
error: "not authenticated",
});
req.app.locals.logger.warn(`Vault.account: blocked an attempt to bypass authentication [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
return;
}
res.status(200).json({
status: "success",
data: {
// TODO: make this a method of Session
primaryIdentity: session.primaryIdentity,
allowNonPrimary: session.allowNonPrimary,
strictIPCheck: session.strictIPCheck,
vaultId: session.vault,
},
});
req.app.locals.cooldown(req, 1e3);
};
const update_account = async (req, res, next) => {
const token = String(req.get("X-VAULT-SESSION") || "");
if (!token.match(regexes.token)) {
res.status(400).json({
status: "error",
error: "missing session key",
});
req.app.locals.logger.warn(`Vault.account: blocked an attempt to bypass authentication [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
return;
}
if (!req.body || !Reflect.has(req.body, "primary") || !Reflect.has(req.body, "allow") ||
!Reflect.has(req.body, "strict") || !Number.isInteger(req.body.primary)) {
res.status(400).json({
status: "error",
error: "invalid format",
});
req.app.locals.cooldown(req, 5e3);
return;
}
const session = req.app.locals.session.get(token);
if (session === null || session === undefined) {
res.status(410).json({
status: "error",
error: "session expired",
});
req.app.locals.cooldown(req, 5e3);
return;
}
if (session.authenticated !== true) {
res.status(401).json({
status: "error",
error: "not authenticated",
});
req.app.locals.logger.warn(`Vault.account: blocked an attempt to bypass authentication [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
return;
}
if (session.strictIPCheck && session.ip !== req.ip) {
// the ip is not the same
res.status(401).json({
status: "error",
error: "ip address mismatch",
});
req.app.locals.logger.warn(`Vault.account: ip address mismatch <${session.vault}@vault> [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
return;
}
const update_fields = {};
if (session.primaryIdentity !== req.body.primary) {
// update primary identity
let new_primary = null;
for (const ident of session.identities) {
if (ident.id === req.body.primary) {
new_primary = ident.id;
break;
}
}
if (new_primary === null) {
res.status(404).json({
status: "error",
error: "not owned by you",
});
req.app.locals.logger.warn(`Vault.account: blocked an attempt to bypass authentication [${req.ip}]`);
req.app.locals.cooldown(req, 3e5);
}
update_fields.primaryIdentity = new_primary;
}
if (session.allowNonPrimary !== !!req.body.allow) {
// update allow non-primary
update_fields.allowNonPrimary = !!req.body.allow;
}
if (session.strictIPCheck !== !!req.body.strict) {
// update allow non-primary
update_fields.strictIPCheck = !!req.body.strict;
}
// update SQL
if (Object.keys(update_fields).length) {
await req.app.locals.vault.login.update(update_fields, {
where: { id: session.vault }
});
}
// now update our cache
session.allowNonPrimary = !!req.body.allow;
session.strictIPCheck = !!req.body.strict;
session.primaryIdentity = +req.body.primary;
for (const ident of session.identities) {
if (ident.id === session.primaryIdentity) {
ident.primary = true;
} else if (ident.primary === true) {
ident.primary = false;
}
}
res.status(200).json({
status: "success",
});
req.app.locals.cooldown(req, 1e3);
};
module.exports = exports = async (req, res, next) => {
switch(req.method) {
case "GET":
// get account data
return await get_data(req, res, next);
case "PATCH":
// change account data
return await update_account(req, res, next);
default:
next(); // fallthrough to default endpoint (404)
}
};
|