view serve-files.js @ 49:1bd17c2e5517 default tip

video.py must sign video urls for serve-files.js to serve them
author drewp@bigasterisk.com
date Fri, 06 Dec 2024 17:13:51 -0800
parents ed16fdbb3996
children
line wrap: on
line source

const express = require('express')
const decodeSig = async (sig) => {
    const { decodeSig } = await import('./signature_decode.mjs');
    return decodeSig(sig);
};
const app = express()

async function checkSig(sig, user, reqPath) {
    const msg = await decodeSig(sig);
    if (msg[0] !== user) {
        throw new Error('user mismatch ' + msg[0] + ' ' + user);
    }
    if (msg[1] !== reqPath) {
        throw new Error('path mismatch');
    }
    const now = new Date() / 1000;
    if (msg[2] < now) {
        throw new Error('expired');
    }
}


// e.g. /video/files/video-download/movie1/part1.webm
app.use('/video/files',
    async (req, res, next) => {
        try {
            await checkSig(req.query.sig || '', req.headers['x-pomerium-email'], req.path);
        } catch (e) {
            console.error(e);
            res.status(403).send('403 Forbidden');
            return;
        }
        next();
    },
    express.static('/data'), // serves file content
)

app.listen(8003)