Warning - This plugin is not meant to be used in production. This is a POC. There are more feature complete authentication plugins available within Kong.
This is a Kong plugin to check authentication for a user. It assumes that the user sends a request to the preconfigured Kong API gateway endpoint with a header that is used for authentication. The name of the header item is variable (default is 'token'). The value, however, must be a base64 encoded string of the format - ":".
This header value is decoded to be passed to a remote authentication server which authenticates the user. If the request is authentic, the remote server returns a 200 OK as well as a JWT access token along with other identifying information about the user in a JSON body. This access token is then added to the upstream request as a header item. The name of the upstream header item is also configurable (default is 'token').
For all cases where the authentication server does not respond with a 200 OK, the plugin makes Kong return HTTP Status code 401 (Unauthorized) with the error message "Invalid authentication credentials". This is part of the reason why this plugin should not be used in production yet. The error handling can and should be more detailed, specifically around cases like the authentication server being unavailable, and the user not providing the request header.
| Name | Usage | Default |
|---|---|---|
| auth_server_url | This is the URL of the authentication server. The server should take an email and password as a payload and return a JWT. Tested with json-server and json-server-auth. | http://host.docker.internal:3000/login (this is because in manual testing, Kong is running in docker and json-server-auth is running externally. Ideally should be a remote URL) |
| auth_server_response_body_field_name | This is the field in the body of the authentication server's successful response that would then be passed on to the upstream server as a JWT. | accessToken |
| request_header_field_name | This is the header name that the user must include in their initial request to Kong. The format should be base64 encoded "email:password" | token |
| upstream_request_header_field_name | This is the header name that will be included by the authChecker plugin in the upstream request, after succesful authentication from the authentication server. | token |
- Assuming the system has a relatively new version of node and Python 3.x
- Install python requirements
- either directly to the system using
pip install -r requirements.txtor - install a virtual environment using
python3 -m venv /temp/kong-py-pdkand activate it usingsource /temp/kong-py-pdk/bin/activateand then install using pip as above
- either directly to the system using
- Run
setup_instruction.shto create the network, build the docker image from the Dockerfile, create and setup the postgres database to be used by Kong, as well as setupjson-server-authwith the included db.json file. The db is initialized with the email mail@nitinkhanna.com and the passwordbestPassw0rd. You may replace the same by hitting the /register endpoint. Instructions for the same are here. - Run
start-kong.shto start Kong API Gateway. Note that we run kong-gateway because we like the webUI. - Also note that we run Kong with the preconfigured kong.yml file. This allows for a faster setup. You can modify the file as required. Leave it empty if you want to configure Kong from scratch.
- Also note that the webUI is on port 8002 as standard in Kong but the public endpoint is on 8003 instead of 8000. This is because the developer uses portainer, which runs on port 8000.
- Once you're done testing Kong, run the
stop-kong.shscript to only stop Kong, or directly run thecleanup_instructions.shfile to remove all artifacts - Kong gateway, postgres container, docker network, as well as to stop thejson-server-auth
- Assuming the system has a relatively new version of node and Python 3.x
- Install python requirements
- either directly to the system using
pip install -r requirements.txtor - install a virtual environment using
python3 -m venv /temp/kong-py-pdkand activate it usingsource /temp/kong-py-pdk/bin/activateand then install using pip as above
- either directly to the system using
- Once inside, you can run the unit testing using the commands given below. Note that the integration test only requires docker as the setup and execution of tests happens inside the purpose-built docker container.
To run the unit tests and generate code coverage information for the authChecker plugin -
python -m coverage run --include plugins/authChecker.py -m unittest tests/unit.py
To display the coverage report as well as fail if under 80% coverage, which is the industry standard -
python -m coverage report --fail-under 80
To run the integration test, simply run the following command -
bash tests/integration.sh
- Improve HTTP status codes
- Test with more authentication backends
- Add TTL for JWT. Currently DAOs are not supported by the Kong python PDK. So the plugin can use
sqlite-cachefor the purpose.json-server-authresponds with an JWT that expires in 1 hour.