Modifying headers #434

ghost opened this Issue Jun 20, 2013 · 3 comments


None yet
4 participants

ghost commented Jun 20, 2013

What is the current way to modify headers, say, to inject Basic Authentication into a proxied request?

I am having trouble, and tried two different ways. Am I missing something?

ruquay commented Jul 1, 2013

I answered your question on StackOverflow, but here's the code below as well:

# Demo server requiring basic authentication
servAuth = require("http").createServer (req, res) ->
  if auth = req.headers?.authorization
    res.statusCode = 200
    res.end "Good job, you sent '#{auth}'"
    res.statusCode = 401
    res.end "How about you authenticate first?"

# Proxy server which checks the IP address and then proxies the request
servProxy = require("http-proxy").createServer (req, res, proxy) ->
  checkIP req, (err, isOK) ->
    # something wrong happened even with the IP address checking
    if err
      res.statusCode = 500
      res.end "Sorry, everything got fargled", "ascii"
    # IP address not allowed
    else if not isOK
      res.statusCode = 403
      res.end "You ain't from around here, are you?", "ascii"
    # all good, proxy the request with basic auth added
      userPass = new Buffer("#{process.env.USERNAME}:#{process.env.PASSWORD}", "ascii")
      userPass = userPass.toString("base64")
      req.headers.authorization = "Basic #{userPass}"
      proxy.proxyRequest req, res, {
        host: "localhost"
        port: 8090

# asynchronous IP address checking
checkIP = (req, done) ->
  # TODO: implement whatever custom IP checking
  # this example just says everything is OK
  done( null, true )

mmalecki closed this Sep 7, 2013

Thanks @ruquay, that fixed my problem too.

thanks it solved my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment