Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default async function paywall(
req: Request,
res: Response,
next: NextFunction
): Promise {
const { headers } = req
const hodl = req.boltwallConfig ? req.boltwallConfig.hodl : false
// If missing LSAT in request to protected content
// then we need to create a new invoice and corresponding LSAT
let lsat: Lsat | undefined = undefined
if (headers.authorization) {
try {
lsat = Lsat.fromToken(headers.authorization)
} catch (e) {
req.logger.error(
'Could not create LSAT from given authorization header: %s. Error: %s',
headers.authorization,
e.message
)
}
}
if (!headers.authorization || !lsat || lsat.isExpired()) {
let invoice: InvoiceResponse
try {
invoice = await createInvoice(req)
} catch (e) {
// handle ln-service errors
if (Array.isArray(e)) {
return next({
message:
'Request malformed: Expected a 256-bit string for the payment hash',
})
}
// if no LSAT then it depends on the route for how to handle it
if (!headers.authorization || !headers.authorization.includes('LSAT')) {
return next()
}
// if we have an lsat header
// need make sure the lsat is properly encoded
let lsat: Lsat
try {
lsat = Lsat.fromToken(headers.authorization)
assert(lsat, 'Could not decode lsat from authorization header')
} catch (e) {
req.logger.debug(
`Received malformed LSAT authorization header from ${req.hostname}: ${headers.authorization}`
)
req.logger.error(e)
res.status(400)
return next({ message: `Bad Request: Malformed LSAT header.`, details: e })
}
if (lsat.isExpired()) {
req.logger.debug(
`Request made with expired LSAT for ${req.originalUrl} from ${req.hostname}`
)
res.status(401)
return next({