(req, res, targetUrl)
| 204 | } |
| 205 | |
| 206 | function forwardAuthRequest(req, res, targetUrl) { |
| 207 | const parsedTarget = new URL(targetUrl); |
| 208 | const transport = parsedTarget.protocol === "https:" ? https : http; |
| 209 | const cookies = parseCookies(req); |
| 210 | |
| 211 | if ( |
| 212 | req.parsedPathname === "/api/user/refresh_token" && |
| 213 | !cookies[COOKIE_NAMES.REFRESH_TOKEN] |
| 214 | ) { |
| 215 | res.writeHead(204); |
| 216 | res.end(); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | collectRequestBody(req) |
| 221 | .then((rawBody) => { |
| 222 | const body = prepareAuthRequestBody(req.parsedPathname, rawBody, cookies, res); |
| 223 | |
| 224 | // If body is null, prepareAuthRequestBody already sent the error response |
| 225 | if (body === null) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | const forwardHeaders = { ...req.headers, host: parsedTarget.host }; |
| 230 | |
| 231 | // Inject access_token from cookie as Authorization header for the backend |
| 232 | if ( |
| 233 | cookies[COOKIE_NAMES.ACCESS_TOKEN] && |
| 234 | !forwardHeaders["authorization"] |
| 235 | ) { |
| 236 | forwardHeaders["authorization"] = |
| 237 | `Bearer ${cookies[COOKIE_NAMES.ACCESS_TOKEN]}`; |
| 238 | } |
| 239 | |
| 240 | if ( |
| 241 | cookies[COOKIE_NAMES.OAUTH_PENDING] && |
| 242 | (req.parsedPathname === "/api/user/oauth/pending" || |
| 243 | req.parsedPathname === "/api/user/oauth/complete") |
| 244 | ) { |
| 245 | forwardHeaders["x-oauth-pending-token"] = |
| 246 | cookies[COOKIE_NAMES.OAUTH_PENDING]; |
| 247 | } |
| 248 | |
| 249 | // Update content-length if body was modified |
| 250 | if (body.length !== rawBody.length) { |
| 251 | forwardHeaders["content-length"] = String(body.length); |
| 252 | } |
| 253 | |
| 254 | const options = { |
| 255 | hostname: parsedTarget.hostname, |
| 256 | port: parsedTarget.port, |
| 257 | path: req.url, |
| 258 | method: req.method, |
| 259 | headers: forwardHeaders, |
| 260 | }; |
| 261 | |
| 262 | const proxyReq = transport.request(options, (proxyRes) => { |
| 263 | const responseChunks = []; |
no test coverage detected