-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (112 loc) · 3.14 KB
/
index.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
import 'dotenv/config';
import express from 'express';
import { expressjwt } from 'express-jwt';
import cryptoRandomString from 'crypto-random-string';
import jwt from 'jsonwebtoken';
import fs from 'fs/promises';
import data from './src/hass-bambulab.js';
import { v4 as uuidv4 } from 'uuid';
import cors from 'cors';
const app = express();
// Generate a random secret for JWT signing
const secret = process.env.DEBUG ? 'unsecure' : cryptoRandomString({ length: 64 });
// Add json body parser
app.use(express.json());
// Add CORS
app.use(cors());
// Use Express JWT to validate JWT tokens
// Disable JWT validation for /oauth/token / and all static files
app.use(
expressjwt({
secret,
algorithms: ['HS256'],
}).unless((path) => {
var url = path.originalUrl;
if (url === '/oauth/token') {
return true;
}
if (url === '/') {
return true;
}
if (url.startsWith('/assets/')) {
return true;
}
if (url.startsWith('/favicon.ico')) {
return true;
}
if (url === '/login') {
return true;
}
return false;
})
);
// Implement (deprecated) OAuth 2.0 password grant
app.post('/oauth/token', (req, res) => {
if (req.body.grant_type !== 'password') {
res.status(400).send('Invalid grant type');
return;
}
if (req.body.username !== process.env.AUTH_USER || req.body.password !== process.env.AUTH_PASSWORD) {
res.status(401).send('Invalid credentials');
return;
}
res.json({
access_token: jwt.sign({ sub: req.body.username }, secret, {
algorithm: 'HS256',
expiresIn: process.env.DEBUG ? '10y' : '8h',
}),
token_type: 'Bearer',
expires_in: process.env.DEBUG ? 315360000 : 28800
});
});
app.get('/filaments', async (req, res) => {
res.send(Object.keys(data).map(key => data[key]));
});
app.post('/delete', async (req, res) => {
const { tag_uid } = req.body;
delete data[tag_uid];
await fs.writeFile('./data/hass-data.json', JSON.stringify(data));
res.send(Object.keys(data).map(key => data[key]));
});
app.post('/update', async (req, res) => {
let { tag_uid } = req.body;
if (!tag_uid) {
tag_uid = uuidv4();
}
if (req.body.color?.length === 7) {
req.body.color = req.body.color + 'FF';
}
req.body.color = req.body.color.toUpperCase();
if (!data[tag_uid]) {
data[tag_uid] = {
tag_uid,
type: 'Unknown',
manufacturer: 'Unknown',
remain: 0,
color: '#FFFFFFFF',
empty: true,
name: 'Unknown',
size: 1000,
colorname: '',
...req.body,
tracking: false
};
} else {
data[tag_uid] = {
...data[tag_uid],
...req.body
};
}
await fs.writeFile('./data/hass-data.json', JSON.stringify(data));
res.send(Object.keys(data).map(key => data[key]));
});
// deliver static files from frontend/dist
app.use(express.static('frontend/dist'));
// for vue-router we need to redirect all unknown routes to index.html
app.get('*', async (req, res) => {
let content = await fs.readFile('./frontend/dist/index.html', 'utf-8');
res.send(content);
});
app.listen(process.env.PORT, () => {
console.log(`App listening on port ${process.env.PORT}!`);
});