1
0
Fork 0

feat(plugins): add req.rawBody for routes

Stripe webhooks endpoints requires to read the raw request body.
https://docs.stripe.com/webhooks#verify-webhook-signatures-with-official-libraries
This commit is contained in:
kontrollanten 2024-03-29 08:22:26 +01:00 committed by Chocobozzz
parent 107e6e73a6
commit 1b323f4f65
5 changed files with 37 additions and 1 deletions

View file

@ -9,6 +9,10 @@ async function register ({
router.post('/form/post/mirror', (req, res) => {
res.json(req.body)
})
router.post('/form/post/mirror-raw-body', (req, res) => {
res.json(JSON.parse(req.rawBody))
})
}
async function unregister () {

View file

@ -80,6 +80,24 @@ describe('Test plugin helpers', function () {
}
})
it('Should mirror the raw post body', async function () {
const body = {
torso: 'arms',
legs: 'feet'
}
for (const path of basePaths) {
const res = await makePostBodyRequest({
url: server.url,
path: path + 'form/post/mirror-raw-body',
fields: body,
expectedStatus: HttpStatusCode.OK_200
})
expect(res.body).to.deep.equal(body)
}
})
it('Should remove the plugin and remove the routes', async function () {
await server.plugins.uninstall({ npmName: 'peertube-plugin-test-five' })

View file

@ -54,6 +54,7 @@ declare module 'express' {
export interface Request {
query: any
method: HttpMethodType
rawBody: Buffer // Allow plugin routes to access the raw body
}
// ---------------------------------------------------------------------------

View file

@ -211,6 +211,10 @@ app.use(express.json({
message: 'Invalid digest'
})
}
if (req.originalUrl.startsWith('/plugins/')) {
req.rawBody = buf
}
}
}))

View file

@ -247,7 +247,7 @@ function register ({
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
// Users are automatically authenticated
router.get('/auth', async (res, res) => {
router.get('/auth', async (req, res) => {
const user = await peertubeHelpers.user.getAuthUser(res)
const isAdmin = user.role === 0
@ -261,6 +261,15 @@ function register ({
isUser
})
})
router.post('/webhook', async (req, res) => {
const rawBody = req.rawBody // Buffer containing the raw body
handleRawBody(rawBody)
res.status(204)
})
}
```