default button name
This commit is contained in:
23
server.js
23
server.js
@@ -14,6 +14,7 @@ const vapidPublicKey = process.env.VAPID_PUBLIC_KEY;
|
||||
const vapidPrivateKey = process.env.VAPID_PRIVATE_KEY;
|
||||
const vapidSubject = process.env.VAPID_SUBJECT; // mailto: or https:
|
||||
const subscriptionsFilePath = process.env.SUBSCRIPTIONS_FILE || path.join(__dirname, 'subscriptions.json');
|
||||
const defaultButtonName = process.env.DEFAULT_BUTTON_NAME || 'game-button';
|
||||
// Basic Authentication Credentials
|
||||
const basicAuthUsername = process.env.BASIC_AUTH_USERNAME;
|
||||
const basicAuthPassword = process.env.BASIC_AUTH_PASSWORD;
|
||||
@@ -233,20 +234,22 @@ const authenticateBasic = (req, res, next) => {
|
||||
// Subscribe endpoint: Add a new button->subscription mapping
|
||||
// Apply Basic Authentication
|
||||
app.post('/subscribe', authenticateBasic, async (req, res) => {
|
||||
const { button_id, subscription } = req.body;
|
||||
let { button_id, subscription } = req.body;
|
||||
|
||||
logger.debug('All headers received:');
|
||||
Object.keys(req.headers).forEach(headerName => {
|
||||
logger.debug(` ${headerName}: ${req.headers[headerName]}`);
|
||||
});
|
||||
|
||||
// If button_id is not provided, use defaultButtonName from environment variable
|
||||
if (!button_id || typeof button_id !== 'string' || button_id.trim() === '') {
|
||||
button_id = defaultButtonName;
|
||||
logger.info(`No button_id provided, using default button name: ${button_id}`);
|
||||
}
|
||||
|
||||
logger.info(`Received subscription request for button: ${button_id}`);
|
||||
|
||||
// Basic Validation
|
||||
if (!button_id || typeof button_id !== 'string' || button_id.trim() === '') {
|
||||
logger.warn('Subscription Error: Missing or invalid button_id');
|
||||
return res.status(400).json({ message: 'Bad Request: Missing or invalid button_id' });
|
||||
}
|
||||
// Basic Validation - now we only validate subscription since button_id will use default if not provided
|
||||
if (!subscription || typeof subscription !== 'object' || !subscription.endpoint || !subscription.keys || !subscription.keys.p256dh || !subscription.keys.auth) {
|
||||
logger.warn('Subscription Error: Missing or invalid subscription object structure');
|
||||
return res.status(400).json({ message: 'Bad Request: Missing or invalid subscription object' });
|
||||
@@ -273,7 +276,7 @@ app.post('/subscribe', authenticateBasic, async (req, res) => {
|
||||
// Apply Basic Authentication
|
||||
app.get('/webhook/:click_type', authenticateBasic, async (req, res) => {
|
||||
// Get buttonName from Header 'Button-Name' and timestamp from Header 'Timestamp'
|
||||
const buttonName = req.headers['button-name'];
|
||||
const buttonName = req.headers['button-name'] || defaultButtonName;
|
||||
const timestamp = req.headers['timestamp'];
|
||||
// Get click_type from URL path
|
||||
const click_type = req.params.click_type;
|
||||
@@ -291,9 +294,9 @@ app.get('/webhook/:click_type', authenticateBasic, async (req, res) => {
|
||||
logger.info(`Received GET webhook: Button=${buttonName}, Type=${click_type}, Timestamp=${timestamp || 'N/A'}`);
|
||||
|
||||
// Basic validation
|
||||
if (!buttonName || !click_type) {
|
||||
logger.warn(`Webhook Error: Missing Button-Name header or click_type query parameter`);
|
||||
return res.status(400).json({ message: 'Bad Request: Missing Button-Name header or click_type query parameter' });
|
||||
if (!click_type) {
|
||||
logger.warn(`Webhook Error: Missing click_type query parameter`);
|
||||
return res.status(400).json({ message: 'Bad Request: Missing click_type query parameter' });
|
||||
}
|
||||
|
||||
const normalizedButtonName = buttonName.toLowerCase(); // Use lowercase for lookup consistency
|
||||
|
||||
Reference in New Issue
Block a user