Score:0

Nginx How to Extract a field from a JSON body response to an OAuth server?

sa flag

I would like Nginx to call an External OAuth server to validate a bearer token and additionally extract the client_id from the OAuth validation response. I need the client_id to apply rate limiting.

However, I have not been able to find a native way in Nginx to simply extract an attribute from a response body. I was hoping to do something simple like regexing a variable such as $response_body.

I did see a couple of alternative options, such as installing a Luo module or a Javascript module.

Is there any way to do this natively in Nginx or do I have to use a custom option?

The response body from the OAuth server would look something like:

{
    "active": true,
    "client_id": "abc123"
}

I've gotten this far:

        location / {
            set $bearer_token "";
            if ($http_authorization ~* "^Bearer\s(.+)$") {
                set $bearer_token $1;
            }

            auth_request /oauth_validate;

            proxy_pass https://my-server;

            # I would like to extract the "active" and "client_id" values from the response body here
            # And then approve/disprove and apply rate limiting on the client_id
        }

        location /oauth_validate {
            internal;
            proxy_method POST;
            proxy_pass_request_headers off;
            proxy_set_header Content-Type "application/x-www-form-urlencoded";
            proxy_set_body "token=$bearer_token&client_credentials&client_id=foo&client_secret=bar";
            
            proxy_pass https://external-oauth.com/oauth2/default/v1/introspect;
        }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.