The idea is to get information about behind the scenes of access token, so before access the resources, you need to add jwt token, then token will pass to the 3rd party application then the return of the BODY will attach to the request in backend service, it can be in two ways:
- Authorization Headers
- Cookie ( if you implement CSRF protection, you might be attach automatically the access token)
token_header
name of the header which hold access token- type: string
- default: Authorization
cookie_name
name of the cookie of which hold the access token- type: string
- default: access-token
authentication_endpoint
url of the 3rd party authentication service- type: URL
- required: true
- method: GET
payload body will attach in:
- headers
X-User-Payload
For this example we used docker to install
docker build -f ./Dockerfile -t <your-username>/kong-gateway .
docker network create kong-quickstart-net
docker run -d --name "kong-quickstart-database" --network="kong-quickstart-net" -e "POSTGRES_DB=kong" -e "POSTGRES_USER=kong" -e "POSTGRES_PASSWORD=kong" postgres:13
docker run --rm --network=kong-quickstart-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-quickstart-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_PASSWORD=test" \
<your-username>/kong-gateway kong migrations bootstrap
docker run -d --name=kong-gateway \
--network=kong-quickstart-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-quickstart-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
-e "KONG_VERSION=" \
-e "KONG_PREFIX=/usr/local/kong" \
-p 8000:8000 \
-p 8001:8001 \
-p 8002:8002 \
-p 8003:8003 \
-p 8004:8004 \
<your-username>/kong-gateway
This application will return all headers, and this app will run in port 3700
cd example/simple-app
yarn start
You can use this template as authentication service. Change the .env.example
to .env
# Running the dependencies
docker-compose up -d --build
# Migrate the DB
yarn && yarn db:migrate
yarn prisma:sync
yarn db:seed
# Running the app in dev mode
yarn dev
curl -X POST http://localhost:8001/services \
-H "Content-Type: application/json" \
-d '{"name": "example-service", "host": "host.docker.internal", "port": 3700, "path": "/"}'
curl -X POST http://localhost:8001/services/example-service/routes/ \
-H "Content-Type: application/json" \
-d '{"name": "example-route", "paths": [ "/" ], "protocols": [ "http", "https" ], "methods": [ "GET", "PUT", "PATCH", "POST", "DELETE", "OPTIONS", "HEAD", "TRACE", "CONNECT" ]}'
curl -X POST http://localhost:8001/services/example-service/plugins \
--data "name=kong-auth-integration" \
--data config.token_header=Authorization \
--data config.cookie_name=access-token \
--data config.authentication_endpoint=http://host.docker.internal:4500/api/v1/session/me
- Access the
localhost:4500/openapi
then login the user/api/v1/session
so you will get theaccess-token
- Try access the
localhost:8000
, you need to pass the token inAuthorization Bearer eyj8odk34...
, and you will get theX-User-Payload
headers same as body fromhttp://localhost:4500/api/v1/session/me
.
You can check how to use in kubernetes