Spaces:
Running
Running
Ig0tU commited on
Commit ·
bac09d2
1
Parent(s): 632cfb8
Add Basic Auth middleware to protect UI
Browse files
server.js
CHANGED
|
@@ -23,7 +23,31 @@ const port = process.env.PORT || 3000;
|
|
| 23 |
app.use(cors());
|
| 24 |
app.use(bodyParser.json());
|
| 25 |
app.use(bodyParser.urlencoded({ extended: true }));
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
// Google Sheets Setup
|
| 29 |
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
|
|
|
|
| 23 |
app.use(cors());
|
| 24 |
app.use(bodyParser.json());
|
| 25 |
app.use(bodyParser.urlencoded({ extended: true }));
|
| 26 |
+
|
| 27 |
+
// Basic Auth for UI Protection
|
| 28 |
+
app.use((req, res, next) => {
|
| 29 |
+
// Always permit the API and integration scripts
|
| 30 |
+
if (req.path.startsWith('/api/') || req.path === '/embed.js' || req.path === '/live-site-integration.js') {
|
| 31 |
+
return next();
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// For HTML pages and other assets, require a password
|
| 35 |
+
const b64auth = (req.headers.authorization || '').split(' ')[1] || '';
|
| 36 |
+
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':');
|
| 37 |
+
|
| 38 |
+
// Let them use 'admin' as the username, and the SMTP password (or fallback to 'admin' if not deployed yet)
|
| 39 |
+
const expectedPassword = process.env.UI_PASSWORD || process.env.SMTP_PASS || 'admin';
|
| 40 |
+
|
| 41 |
+
// We allow 'admin' user or ANY user as long as the password matches. Browsers usually prompt for both.
|
| 42 |
+
if (password === expectedPassword) {
|
| 43 |
+
return next();
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
res.set('WWW-Authenticate', 'Basic realm="Restricted WallAPI Space"');
|
| 47 |
+
res.status(401).send('Authentication required. Username: admin. Password matches your SMTP_PASS Secret (or "admin" if not set).');
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
app.use(express.static('public')); // Serve the frontend assets
|
| 51 |
|
| 52 |
// Google Sheets Setup
|
| 53 |
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
|