[Solved] Retrieve Basic auth header from webhook

I set up a webhook with basic auth and would like to retrieve the username and password sent for the request. I tried using a Function node to get the value but was unsuccessful.

Any thoughts or suggestions for me?

You were on the right track with the function node - because string splitting is involved, the easiest way is with a function node. I put together the following code that will work in a function node given a webhook triggered flow:

if(!payload.data ||
   !payload.data.headers ||
   !payload.data.headers.authorization)
{ return; }

var parts = String(payload.data.headers.authorization).split(/\s+/);
if(parts.length !== 2)
{ return; }
if(parts[0].toLowerCase() !== 'basic')
{ return; }

var credentials = Buffer.from(parts[1], 'base64').toString();
var sep = credentials.indexOf(':');
if (sep === -1) { return; }

payload.data.username = credentials.slice(0, sep);
payload.data.password = credentials.slice(sep + 1);

If a valid basic auth header is sent along with the request, the above code in a function node will place the username at data.username and the password at data.password. Hope that helps!

1 Like

Amazing, thank you so much!