title: Detect and rewrite HTTP Basic user/password headers into custom headers with Nginx/Lua
body: <p>I am working with a historic API which grants access via a key/secret combo, which the original API designer specified should be passed as the user name &amp; password in an HTTP Basic auth header, e.g.:</p>
  
  <pre><code>curl -u api_key:api_secret http://api.example.com/....
  </code></pre>
  
  <p>Now that our API client base is going to be growing, we're looking to using <a href="http://www.3scale.net/" rel="nofollow">3scale</a> to handle both authentication, rate limiting and other functions. As per 3scale's instructions and advice, we'll be using an Nginx proxy in front of our API server, which authenticates against 3scale's services to handle all the access control systems.</p>
  
  <p>We'll be exporting our existing clients' keys and secrets into 3scale and keeping the two systems in sync. We need our existing app to continue to receive the key &amp; secret in the existing manner, as some of the returned data is client-specific. However, I need to find a way of converting that HTTP basic auth request, which 3scale doesn't natively support as an authentication method, into rewritten custom headers which they do.</p>
  
  <p>I've been able to set up the proxy using the Nginx and Lua configs that 3scale configures for you. This allows the <code>-u key:secret</code> to be passed through to our server, and correctly processed. At the moment, though, I need to additionally add the same authentication information either as query params or custom headers, so that 3scale can manage the access.
   I want my Nginx proxy to handle that for me, so that users provide one set of auth details, in the pre-existing manner, and 3scale can also pick it up.</p>
  
  <p>In a language I know, e.g., Ruby, I can decode the HTTP_AUTHORIZATION header, pick out the Base64-encoded portion, and decode it to find the key &amp; secret components that have been supplied. But I'm an Nginx newbie, and don't know how to achieve the same within Nginx (I also don't know if 3scale's supplied Lua script can/will be part of a solution)...</p>