First some context: Here's a typical authentication scenario:
req.headers.cookieNow that we've set the context, here's what I want to do: I want to build an API feature to the server, but instead of adding additional API key generation and management logic, I would like to use the existing cookie.
Basically I want to do this:
req.headers.cookie attribute.req.headers.cookie back to the user as a responseFor example, here's a sample route handler for the API Key page:
app.get("/api_keys", authenticate, (req, res) => {
res.render("api_keys", {
cookie: req.headers.cookie
})
})
This will display the cookie string on the /api_keys page.
Then the user may be able to copy and paste the full cookie string into the following code:
fetch(API_URL, {
headers: { cookie: <PASTE_COOKIE_HERE> }
}).then((response) => {
console.log(response)
})
and it should work. And it does (I just checked).
My question is: Is this OK to secure APIs this way?