Question

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 & password in an HTTP Basic auth header, e.g.:

curl -u api_key:api_secret http://api.example.com/....

Now that our API client base is going to be growing, we're looking to using 3scale 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.

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 & 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.

I've been able to set up the proxy using the Nginx and Lua configs that 3scale configures for you. This allows the -u key:secret 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.

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 & 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)...

Was it helpful?

Solution

Reusing the HTTP Authorization header for the 3scale keys can be supported with a small tweak in your Nginx configuration files. As you were rightly pointing out, the Lua script that you download is the place to do this.

However, I would suggest a slightly different approach regarding the keys that you import to 3scale. Instead of using the app_id/app_key authentication pattern, you could use the user_key mode (which is a single key). Then what you would import to 3scale for each application would be the base64 string of api_key+api_secret combined.

This way the changes you will need to do to the configuration files will be fewer and simpler.

The steps you will need to follow are:

  • in your 3scale admin portal, set the authentication mode to API key (https://support.3scale.net/howtos/api-configuration/authentication-patterns)
  • go to the proxy configuration screen (where you set your API backend, mappings and where you download the Nginx files).
  • under "Authentication Settings", set the location of the credentials to HTTP headers.
  • download the Nginx config files and open the Lua script
  • find the following line (should be towards the end of the file):

local parameters = get_auth_params("headers", string.split(ngx.var.request, " ")[1] )

  • replace it with:

local parameters = get_auth_params("basicauth", string.split(ngx.var.request, " ")[1] )

I hope this approach suits your needs. You can also contact at support@3scale.net if you need more help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top