/
utils.js
110 lines (98 loc) · 3.48 KB
/
utils.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
import { createReadStream } from 'fs';
import { resolve } from 'path';
import { verify } from './jwt';
export const filterByCompany = !!process.env.SHARED_DASHBOARD;
export const deniedCompanies = (process.env.DENIED_COMPANY_TOKENS || '').split(',');
export const deniedDevices = (process.env.DENIED_DEVICE_TOKENS || '').split(',');
export const ddosBombCompanies = (process.env.DDOS_BOMB_COMPANY_TOKENS || '').split(',');
export const isProduction = process.env.NODE_ENV === 'production';
export const isPostgres = !!process.env.DATABASE_URL;
const check = (list, item) => list
.find(x => !!x && (item || '').toLowerCase().startsWith(x.toLowerCase()));
export const isDDosCompany = companyToken => check(ddosBombCompanies, companyToken);
export const isDeniedCompany = companyToken => check(deniedCompanies, companyToken);
export const isDeniedDevice = companyToken => check(deniedDevices, companyToken);
export const isAdmin = companyToken => !!filterByCompany &
!!process.env.ADMIN_TOKEN &&
companyToken === process.env.ADMIN_TOKEN;
export const jsonb = data => isPostgres ? (data || null) : JSON.stringify(data);
export class AccessDeniedError extends Error {};
export const raiseError = (res, message, error) => {
const result = new AccessDeniedError(message);
res.status(403).json({ status: 401, error: message });
return error || result;
};
export function hydrate (row) {
const record = row.toJSON();
['data']
.filter(x => typeof record[x] === 'string')
.forEach(x => {
if (typeof record[x] === 'string') {
try {
record[x] = JSON.parse(record[x]);
} catch (e) {
console.error(`could not parse ${x} ${record.id}`, e);
delete record[x];
}
}
});
const { data, device } = record;
const result = {
...device,
activity_type: data.activity && data.activity.type,
activity_confidence: data.activity && data.activity.confidence,
battery_level: data.battery && data.battery.level,
battery_is_charging: data.battery && data.battery.is_charging,
...data,
...data.coords,
...record,
uuid: data.uuid,
};
[
'data',
'device',
'activity',
'battery',
'coords',
].forEach(x => delete result[x]);
return result;
}
export function return1Gbfile (res) {
const file1gb = resolve(__dirname, '..', '..', '..', 'text.null.gz');
console.log('file1gb', file1gb);
res.setHeader('Content-Encoding', 'gzip, deflate');
createReadStream(file1gb).pipe(res);
}
export const checkAuth = (req, res, next) => {
const auth = (req.get('Authorization') || '').split(' ');
if (auth.length < 2 || auth[0] !== 'Bearer') {
return next(new AccessDeniedError('Authorization Bearer not found'));
}
const [, jwt] = auth;
try {
const decoded = verify(jwt);
req.jwt = decoded;
if (!decoded) {
return next(raiseError(res, 'Could not decode JWT'));
}
return next();
} catch (e) {
return next(raiseError(res, 'Wrong JWT', e));
}
};
export const checkCompany = ({ org, model }) => {
if (isDeniedCompany(org)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server1/v2?\n' +
'Please email me at chris@transistorsoft.com.'
);
}
if (isDeniedDevice(model)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server2/v2?\n' +
'Please email me at chris@transistorsoft.com.'
);
}
};