在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):auth0/nginx-jwt开源软件地址(OpenSource Url):https://github.com/auth0/nginx-jwt开源编程语言(OpenSource Language):JavaScript 45.3%开源软件介绍(OpenSource Introduction):JWT Auth for Nginxnginx-jwt is a Lua script for the Nginx server (running the HttpLuaModule) that will allow you to use Nginx as a reverse proxy in front of your existing set of HTTP services and secure them (authentication/authorization) using a trusted JSON Web Token (JWT) in the Contents
Key Features
Install
Install steps:
Configuration
UsageNow we can start using the script in reverse-proxy scenarios to secure our backing service. This is done by using the access_by_lua directive to call the # nginx.conf:
server {
location /secure_this {
access_by_lua '
local jwt = require("nginx-jwt")
jwt.auth()
';
proxy_pass http://my-backend.com$uri;
}
} If you attempt to cURL the above curl -i http://your-nginx-server/secure_this
To create a valid JWT, we've included a handy tool that will generate one given a payload and a secret. The payload must be in JSON format and at a minimum should contain a test/sign '{"sub": "flynn"}' 'My JWT secret'
You can then use the above curl -i http://your-nginx-server/secure_this -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwZXRlIiwiaWF0IjoxNDMwNjc3NjYzfQ.Zt4qnQyljbqLvAN7BQSuu14z5PjKcPpZZY85hDFVN3E'
In this case the Nginx server has authorized the caller and performed a reverse proxy call to the backing service's endpoint. Notice too that the nginx-jwt script has tacked on an extra response header called The API ReferenceauthSyntax: Authenticates the current request, requiring a JWT bearer token in the If authentication succeeds, then by default the current request is authorized by virtue of a valid user identity. More specific authorization can be accomplished via the optional For example if we wanted to ensure that the JWT had an # nginx.conf:
server {
location /secure {
access_by_lua '
local jwt = require("nginx-jwt")
jwt.auth({
aud="^foo:",
role=function (val) return jwt.table_contains(val, "marketing") end
})
';
proxy_pass http://my-backend.com$uri;
}
} and if our JWT's payload of claims looked something like this, the above {
"aud": "foo:user",
"roles": [ "sales", "marketing" ]
} NOTE: the auth function should be called within the access_by_lua or access_by_lua_file directive so that it can occur before the Nginx content phase. table_containsSyntax: A helper function that checks to see if array = { "foo", "bar" }
table_contains(array, "foo") --> true TestsThe best way to develop and test the nginx-jwt script is to run it in a virtualized development environment. This allows you to run Ngnix separate from your host machine (i.e. your Mac) in a controlled execution environment. It also allows you to easily test the script with any combination of Nginx proxy host configurations and backing services that Nginx will reverse proxy to. This repo contains everything you need to do just that. It's set up to run Nginx as well as a simple backend server in individual Docker containers. PrerequisitesMac OS
UbuntuBesides being able to install Docker and run Docker directly in the host OS, the other different between Ubuntu (and more specifically Linux) and Mac OS is that all Docker commands need to be called using sudo ./build run Ubuntu on MacOS (via Vagrant)If your host OS is Mac OS but you'd like to test that the build scripts run on Ubuntu, you can use the provided Vagrant scripts to spin up an Ubuntu VM that has all the necessary tools installed. First, if you haven't already, install Vagrant either by installing the package or using Homebrew. Then in the repo directory, start the VM: vagrant up And then SSH into it: vagrant ssh Once in, you'll need to use git to clone this repo and git clone THIS_REPO_URL
cd nginx-jwt All other tools should be installed. And like with the Ubuntu host OS, you'll need to prefix all calls to the sudo ./build run Build and run the default containersIf you just want to see the nginx-jwt script in action, you can run the ./build run
You can then run cURL commands against the endpoints exposed by the backend through Nginx. The root URL of the proxy is reported back by the script when it is finished. It will look something like this:
Notice the proxy container (which is running in the Docker Machine VM) is listening on port 80. The actual backend container is not directly accessible via the VM. All calls are configured to reverse-proxy through the Nginx host and the connection between the two is done via docker container linking. If you issue the above cURL command, you'll hit the proxy's root ( curl -i http://192.168.59.103
However, if you attempt to cURL the proxy's curl -i http://192.168.59.103/secure
Just like we showed in the Usage section, we can use the included test/sign '{"sub": "flynn"}' 'JWTs are the best!'
curl -i http://192.168.59.103/secure -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwZXRlIiwiaWF0IjoxNDMwNjc3NjYzfQ.Zt4qnQyljbqLvAN7BQSuu14z5PjKcPpZZY85hDFVN3E'
The proxy exposes other endpoints, which have different JWT requirements. To see them all, take a look at the default proxy's If you want to run the script with one of the other proxy containers, simply pass the name of the desired container. Example: ./build run base64-secret Build the containers and run integration testsThis script is similar to ./build tests Use this script while developing new features. Clean everything upIf you need to simply stop/delete all running Docker containers and remove their associated images, use this command: ./build clean Updating dependenciesIt's always nice to keep dependencies up to date. This library (and the tools used to test it) has three sources of dependencies that should be maintained: Lua dependencies, test script Node.js dependencies, and updates to the proxy base Docker image. Lua dependenciesThese are the Lua scripts that this library uses. They are maintained in the Since these dependencies don't have any built-in versioning (like npm), we download a specific GitHub commit instead. We also check that a previously downloaded script is current by examining its SHA-1 digest hash. All this is done via the included If a Lua dependency needs to be updated, find its associated openssh sha1 NEW_SCRIPT To add a new dependency simply add a new Test script Node.js dependenciesAll Node.js dependencies (npm packages) for tests are maintained in this Proxy base Docker imageThe proxy base Docker image may need to be updated periodically, usually to just rev the version of OpenResty that its using. This can be done by modifying the image's PackagingWhen a new version of the script needs to be released, the following should be done:
git tag v$(cat version.txt)
./build package Issue ReportingIf you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues. ContributorsCheck them out here. LicenseThis project is licensed under the MIT license. See the LICENSE file for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论