From 1b64b60257e0cafd55f94dc651f786f4dc0b374b Mon Sep 17 00:00:00 2001 From: Jeffrey Dowdle Date: Tue, 14 Jan 2025 12:44:03 +1100 Subject: [PATCH] feat(@dpc-sdp/ripple-tide-webform): send basic auth for webform submit if provided --- .../api/tide/webform_submission/[formId].ts | 19 +++++++++++++++++-- packages/ripple-tide-webform/nuxt.config.ts | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/nuxt-ripple/server/api/tide/webform_submission/[formId].ts b/packages/nuxt-ripple/server/api/tide/webform_submission/[formId].ts index de1a0aebdd..a1f2804140 100644 --- a/packages/nuxt-ripple/server/api/tide/webform_submission/[formId].ts +++ b/packages/nuxt-ripple/server/api/tide/webform_submission/[formId].ts @@ -5,7 +5,7 @@ import { createProxyMiddleware } from 'http-proxy-middleware' import verifyCaptcha from '../../../utils/verifyCaptcha' export const createWebformProxyHandler = async (event: H3Event) => { - const { public: config } = useRuntimeConfig() + const nuxtConfig = useRuntimeConfig() const formId = event.context.params?.formId if (!formId) { @@ -30,10 +30,25 @@ export const createWebformProxyHandler = async (event: H3Event) => { } const proxyMiddleware = createProxyMiddleware({ - target: config.tide.baseUrl, + target: nuxtConfig.public.tide.baseUrl, pathRewrite: { '^/api/tide/': '/api/v1/' }, + on: { + proxyReq(proxyReq) { + const basicAuthUser = nuxtConfig.tide.webformSubmit.username + const basicAuthPass = nuxtConfig.tide.webformSubmit.password + + // if a username and password is provided, set the basic Authorization header + if (basicAuthUser && basicAuthPass) { + const basicAuthBase64 = Buffer.from( + `${basicAuthUser}:${basicAuthPass}` + ).toString('base64') + + proxyReq.setHeader('Authorization', `Basic ${basicAuthBase64}`) + } + } + }, logger: logger, changeOrigin: true }) diff --git a/packages/ripple-tide-webform/nuxt.config.ts b/packages/ripple-tide-webform/nuxt.config.ts index d27463d40b..20ab4a757e 100644 --- a/packages/ripple-tide-webform/nuxt.config.ts +++ b/packages/ripple-tide-webform/nuxt.config.ts @@ -3,6 +3,10 @@ import { defineNuxtConfig } from 'nuxt/config' export default defineNuxtConfig({ runtimeConfig: { tide: { + webformSubmit: { + username: '', + password: '' + }, captchaSecret: { // Placeholder for captcha id: secret mapping }