NGINX Modern Apps > Class 3 - NGINX Dataplane Scripting Source | Edit on
Extract JWT Payload into NGINX Variable [http/authorization/jwt]¶
JSON Web Tokens (JWT) are a common way to authenticate to web applications. In addition to authentication, JWTs can also be used to pass information, called claims, about the user to the application. The commercial version of NGINX, NGINX Plus, has built-in JWT handling features. Using njs, we can parse JWTs and extract claim data even in the open source version of NGINX.
Step 1: Use the following commands to start your NGINX container with this lab’s files:
Step 2: Now let’s use curl to test our NGINX server:
Code Snippets¶
This NGINX configuration uses js_set to invoke our JavaScript to extract the JWT claim into a variable we can return back to the user.
In our JavaScript we are leveraging the string processing features of njs to decode and parse the JWT into a JSON object. We then extract the claim we want called “sub.”