Add Basic Authentication to a Service or a Route with username and password protection. The plugin
checks for valid credentials in the Proxy-Authorization
and Authorization
headers (in that order).
Configuration Reference
This plugin is partially compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Consumers and Credentials can be created with declarative configuration.
Admin API endpoints which do POST, PUT, PATCH or DELETE on Credentials are not available on DB-less mode.
Enable the plugin on a service
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a service by
making the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/plugins \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: <basic-auth-example>
config:
hide_credentials: true
plugin: basic-auth
Next, apply the KongPlugin resource to a
Service by annotating the
Service as follows:
apiVersion: v1
kind: Service
metadata:
name: {SERVICE}
labels:
app: {SERVICE}
annotations:
konghq.com/plugins: <basic-auth-example>
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: {SERVICE}
selector:
app: {SERVICE}
{SERVICE}
is the id
or name
of the service that this plugin
configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
For example, configure this plugin on a service by
adding this section to your declarative configuration file:
plugins:
- name: basic-auth
service: {SERVICE}
config:
hide_credentials: true
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
Configure this plugin on a service:
- In Konnect Cloud, select the service on the ServiceHub page.
- Scroll down to Versions and select the version.
- Scroll down to Plugins and click New Plugin.
- Find and select the Basic Authentication plugin.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Hide Credentials: select checkbox
- Click Create.
Configure this plugin on a service:
- In Kong Manager, select the workspace.
- From the Dashboard, scroll down to Services and click View for the
service row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the Basic Authentication plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already prefilled.
-
Enter the following parameters, updating the default or sample values as needed:
- Config.Hide Credentials: select checkbox
- Click Create.
Enable the plugin on a route
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a route with:
$ curl -X POST http://{HOST}:8001/routes/{ROUTE}/plugins \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: <basic-auth-example>
config:
hide_credentials: true
plugin: basic-auth
Then, apply it to an ingress (Route or Routes)
by annotating the ingress as follows:
apiVersion: networking/v1beta1
kind: Ingress
metadata:
name: {ROUTE}
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: <basic-auth-example>
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
serviceName: echo
servicePort: 80
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
For example, configure this plugin on a route by
adding this section to your declarative configuration file:
plugins:
- name: basic-auth
route: <route>
config:
hide_credentials: true
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Configure this plugin on a route:
- In Konnect Cloud, select the service from the ServiceHub page.
- Scroll down to Versions and select the version.
- Select the route.
- Scroll down to Plugins and click Add Plugin.
- Find and select the Basic Authentication plugin.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Hide Credentials: select checkbox
- Click Create.
Configure this plugin on a route:
- In Kong Manager, select the workspace.
- From the Dashboard, select Routes in the left navigation.
- Click View for the route row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the Basic Authentication plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the Route ID if it is not already prefilled.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Hide Credentials: select checkbox
- Click Create.
Enable the plugin globally
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
For example, configure this plugin globally with:
$ curl -X POST http://{HOST}:8001/plugins/ \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-basic-auth>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
hide_credentials: true
plugin: basic-auth
For example, configure this plugin using the plugins:
entry in the declarative
configuration file:
plugins:
- name: basic-auth
config:
hide_credentials: true
Configure this plugin globally:
- In Kong Manager, select the workspace.
- From the Dashboard, select Plugins in the left navigation.
- Click New Plugin.
-
Find and select the Basic Authentication plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, set the plugin scope to Global.
-
Enter the following parameters, updating the
default/sample values as needed:
- Config.Hide Credentials: select checkbox
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case basic-auth . |
service.id
Type: string |
The ID of the Service the plugin targets. |
route.id
Type: string |
The ID of the Route the plugin targets. |
enabled
required
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.hide_credentials
required
Type: boolean
Default value: false
|
An optional boolean value telling the plugin to show or hide the credential from the upstream service. If true , the plugin will strip the credential from the request (i.e. the Authorization header) before proxying it.
|
config.anonymous
optional
Type: string
|
An optional string (consumer uuid) value to use as an “anonymous” consumer if authentication fails. If empty (default), the request will fail with an authentication failure 4xx . Please note that this value must refer to the Consumer id attribute which is internal to Kong, and not its custom_id .
|
Once applied, any user with a valid credential can access the Service.
To restrict usage to only some of the authenticated users, also add the
ACL plugin (not covered here) and create allowed or
denied groups of users.
Usage
To use the plugin, you first need to create a Consumer to associate one or more credentials to. The Consumer represents a developer or an application consuming the upstream service.
Create a Consumer
You need to associate a credential to an existing Consumer object.
A Consumer can have many credentials.
With a Database
Without a Database
To create a Consumer, you can execute the following request:
curl -d "username={USER123}&custom_id={SOME_CUSTOM_ID}" http://kong:8001/consumers/
Your declarative configuration file needs to have one or more Consumers.
You can create them using a consumers:
YAML section:
consumers:
- username: {USER123}
custom_id: {SOME_CUSTOM_ID}
Consumer parameters:
Parameter |
Description |
username semi-optional |
The username of the consumer. Either this field or custom_id must be specified. |
custom_id semi-optional |
A custom identifier used to map the consumer to another database. Either this field or username must be specified. |
If you are also using the ACL plugin and allow lists with this
service, you must add the new consumer to the allowed group. See
ACL: Associating Consumers for details.
Create a Credential
With a Database
Without a Database
You can provision new username/password credentials by making the following HTTP request:
curl -X POST http://kong:8001/consumers/{CONSUMER}/basic-auth \
--data "username=Aladdin" \
--data "password=OpenSesame"
You can add credentials on your declarative config file with the basicauth_credentials
YAML entry:
basicauth_credentials:
- consumer: {CONSUMER}
username: Aladdin
password: OpenSesame
Consumer credential parameters:
field/parameter |
description |
consumer |
The id or username property of the Consumer entity to associate the credentials to. |
username |
The username to use in the basic authentication credential. |
password |
The password to use in the basic authentication credential. |
Using the Credential
The authorization header must be base64 encoded. For example, if the credential
uses Aladdin
as the username and OpenSesame
as the password, then the field’s
value is the base64-encoding of Aladdin:OpenSesame
, or QWxhZGRpbjpPcGVuU2VzYW1l
.
The Authorization
(or Proxy-Authorization
) header must appear as:
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
Make a request with the header:
curl http://kong:8000/{PATH_MATCHING_CONFIGURED_ROUTE} \
-H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l'
gRPC clients are supported too:
grpcurl -H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l' ...
When a client has been authenticated, the plugin appends some headers to the request before proxying it to the upstream service, so that you can identify the Consumer in your code:
X-Consumer-ID
: the ID of the Consumer in Kong Gateway
X-Consumer-Custom-ID
: the custom_id
of the Consumer (if set)
X-Consumer-Username
: the username
of the Consumer (if set)
X-Credential-Identifier
: the identifier of the Credential (only if the consumer is not the ‘anonymous’ consumer)
X-Anonymous-Consumer
: set to true
if authentication fails, and the ‘anonymous’ consumer is set instead
You can use this information on your side to implement additional logic. Use the X-Consumer-ID
value to query the Kong Admin API and retrieve more information about the Consumer.
Important: X-Credential-Username
was deprecated in favor of X-Credential-Identifier
in Kong 2.1.
Paginate through the basic-auth Credentials
You can paginate through the basic-auth Credentials for all Consumers using the
following request:
curl -X GET http://kong:8001/basic-auths
{
"total": 3,
"data": [
{
"created_at": 1511379926000,
"id": "805520f6-842b-419f-8a12-d1de8a30b29f",
"password": "37b1af03d3860acf40bd9c681aa3ef3f543e49fe",
"username": "baz",
"consumer": { "id": "5e52251c-54b9-4c10-9605-b9b499aedb47" }
},
{
"created_at": 1511379863000,
"id": "8edfe5c7-3151-4d92-971f-3faa5e6c5d7e",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foo",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
},
{
"created_at": 1511379877000,
"id": "f11cb0ea-eacf-4a6b-baea-a0e0b519a990",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foobar",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
}
]
}
You can filter the list by Consumer with the following endpoint:
curl -X GET http://kong:8001/consumers/{USERNAME_OR_ID}/basic-auth
{
"total": 1,
"data": [
{
"created_at": 1511379863000,
"id": "8edfe5c7-3151-4d92-971f-3faa5e6c5d7e",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foo",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
}
]
}
username
or id
: The username or id of the consumer whose credentials need
to be listed.
Retrieve the Consumer associated with a Credential
It is possible to retrieve a Consumer associated with a
basic-auth Credential using the following request:
curl -X GET http://kong:8001/basic-auths/{USERNAME_OR_ID}/consumer
{
"created_at":1507936639000,
"username":"foo",
"id":"c0d92ba9-8306-482a-b60d-0cfdd2f0e880"
}
username or id
: The id
or username
property of the basic-auth
Credential for which to get the associated Consumer.
Note that the username
accepted here is not the username
property of a
Consumer.
Changelog
2.2.0
- Starting with Kong Gateway 2.7.0.0, if keyring encryption is enabled
and you are using basic authentication, the
basicauth_credentials.password
field will be encrypted.