I want to use HTTP Basic auth on one particular function. The username and password can be hardcoded in the source for all I care, or be in env vars.
However, when I pass back the WWW-Authenticate
header in my handler’s response it is remapped when it goes through API Gateway to x-amzn-Remapped-WWW-Authenticate
, which obviously then doesn’t work. I found some Amazon documentation which confirms this is what is happening, though it doesn’t explain why or if I can get around it.
I can’t find anything about this in the Serverless docs.
Is there any way to do this? I have a feeling it might involve switching from a lambda-proxy
function to the other type, but I don’t know where to start.
Edit:
I’ve worked through some of the docs for lambda
integration (as opposed to lambda-proxy
) and got far enough that I’m attempting once again to set the WWW-Authenticate
header, but it’s still being transformed to x-amzn-Remapped-WWW-Authenticate
. I find this particular documentation very difficult to follow and I haven’t managed to actually set the response header to 401. I’m not sure whether that would make a difference.
What I currently have is this:
functions:
report:
handler: handler.report
events:
- http:
path: report
method: get
integration: lambda
request:
passThrough: WHEN_NO_TEMPLATES
response:
headers:
Content-Type: integration.response.body.headers.Content-Type
WWW-Authenticate: integration.response.body.headers.WWW-Authenticate
template: $input.path('$.body')
statusCodes:
401:
pattern: '.*statusCode.*401.*'
And my implementation looks like this:
callback(null, {
statusCode: 401,
headers: {
'WWW-Authenticate': 'Basic realm="My realm", encoding="UTF-8"',
'Content-Type': 'application/json',
},
body: "no auth",
});
One other possible issue here is that no matter what I try with the quoting in my Basic realm
… it comes out in the response header with unwanted extra escaping, for example like this: x-amzn-Remapped-WWW-Authenticate: Basic realm=\"My realm\", encoding=\"UTF-8\"
, which doesn’t look valid.