forked from hanchon-live/ethermint-faucet-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
58 lines (50 loc) · 1.77 KB
/
utils.ts
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
import jwtAuthz from "express-jwt-authz";
import { BlockedAddress, latestTransactionSince } from "./database";
import * as faucet from "./faucet";
import client from "prom-client";
const counterBlockedAddress = new client.Counter({
name: "faucet_blocked_address_count",
help: "faucet_blocked_address_count is the number of times the a block address requested a drip",
});
const counterCooldown = new client.Counter({
name: "faucet_cool_down_count",
help: "faucet_cool_down_count is the number of times the an address needed to cool down",
});
const counterForbidden = new client.Counter({
name: "faucet_forbidden_count",
help: "faucet_forbidden_count is the number of times the authorization was forbidden",
});
export const ensurePermission = jwtAuthz(["manage:faucet"], {
customScopeKey: "permissions",
});
export async function ensureAuthenticated(req: any, res: any, next: any) {
if (req.user) return next();
counterForbidden.inc();
res.status(403).send(JSON.stringify({ error: "Forbidden" }));
}
export async function rateLimit(req: any, res: any, next: any) {
if (req.user.id) {
let cooldownDate = new Date(
(new Date() as any) - (faucet as any).getWaitPeriod()
);
let transaction = await latestTransactionSince(req.user, cooldownDate);
if (transaction) {
counterCooldown.inc();
return res.status(403).send(JSON.stringify({ error: "Cooldown" }));
}
}
next();
}
export async function blockedAddresses(req: any, res: any, next: any) {
const { address } = req.body;
if (address) {
let blocked = await BlockedAddress.findOne({
where: { address: address.trim() },
});
if (blocked) {
counterBlockedAddress.inc();
return res.status(403).send(JSON.stringify({ error: "Blocked address" }));
}
}
next();
}