> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON Web Token Authentication

> From v3.5.0 onwards, the database supports authenticating client access using JSON Web Tokens.

This topic describes how to enable JSON Web Token authentication in the database.

From v3.5.0 onwards, the database supports authenticating client access using JSON Web Tokens.

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

This topic describes how to manually create and authenticate users using JWT in the database. For instructions on how to integrate the database with JWT authentication using security integration, see [Authenticate with Security Integration](/documents/4.2/administration/user_privs/authentication/security_integration.md). For more information on how to authenticate user groups using JWT, see [Authenticate User Groups](/documents/4.2/administration/user_privs/group_provider.md).

## Prerequisites

If you want to connect to the database from a MySQL client, the MySQL client version must be 9.1 or later. From 9.1, the `authentication_openid_connect_client` plugin ships with all MySQL client distributions, community distributions included.

## Create a user with JWT

When creating a user, specify the authentication method as JWT by `IDENTIFIED WITH authentication_jwt [AS '{xxx}']`. `{xxx}` is the JWT properties of the user. In addition to the following method, you can configure the default JWT properties in the FE configuration file. You need to manually modify all **fe.conf** files and restart all FEs for configuration to take effect. After the FE configurations are set, the database will use the default properties specified in your configuration file and you can omit the `AS '{xxx}'` part.

Syntax:

```SQL theme={null}
CREATE USER <username> IDENTIFIED WITH authentication_jwt [AS 
'{
  "jwks_url": "<jwks_url>",
  "principal_field": "<principal_field>",
  "required_issuer": "<required_issuer>",
  "required_audience": "<required_audience>"
}']
```

| Property | Corresponding FE Configuration | Description |
| - | - | - |
| `jwks_url` | `jwt_jwks_url` | The URL to the JSON Web Key Set (JWKS) service or the path to the public key local file under the `fe/conf` directory. |
| `principal_field` | `jwt_principal_field` | The field in the JWT that identifies the subject. The default value is `sub`. The value of this field must be identical with the username for logging in to the database. To extract a value from a nested claim, specify an [RFC 6901 JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901) (a path starting with `/`). Use `~1` to escape `/` and `~0` to escape `~` in key names. |
| `required_issuer` | `jwt_required_issuer` | (Optional) The list of strings used to identify the issuers (`iss`) in the JWT. The JWT is considered valid only if one of the values in the list match the JWT issuer. |
| `required_audience` | `jwt_required_audience` | (Optional) The list of strings used to identify the audience (`aud`) in the JWT. The JWT is considered valid only if one of the values in the list match the JWT audience. |

Example:

```SQL theme={null}
CREATE USER tom IDENTIFIED WITH authentication_jwt AS
'{
  "jwks_url": "http://localhost:38080/realms/master/protocol/jwt/certs",
  "principal_field": "preferred_username",
  "required_issuer": "http://localhost:38080/realms/master",
  "required_audience": "starrocks"
}';
```

If you have set the JWT properties in the FE configuration files, you can directly execute the following statement:

```SQL theme={null}
CREATE USER tom IDENTIFIED WITH authentication_jwt;
```

## Connect from MySQL client with JWT

To connect from a MySQL client to the database using JWT, you need to enable the `authentication_openid-connect_client` plugin, and pass the necessary token (using the path to the token file) to authenticate the mapped user.

Syntax:

```Bash theme={null}
mysql -h <hostname> -P <query_port> --authentication-openid-connect-client-id-token-file=<path_to_token_file> -u <username>
```

Example:

```Bash theme={null}
mysql -h 127.0.0.1 -P 9030 --authentication-openid-connect-client-id-token-file=/path/to/token/file -u tom
```

## Connect over HTTP or Arrow Flight with JWT

Users of a JWT security integration can also send the token as `Authorization: Bearer <token>` to the HTTP API (SQL API, Stream Load, Web UI) and to Arrow Flight SQL, with no user name. See [Connect over HTTP and Arrow Flight](/documents/4.2/administration/user_privs/authentication/security_integration.md#connect-over-http-and-arrow-flight).

> A user created with `IDENTIFIED WITH authentication_jwt` can present its token only over the MySQL protocol, where the handshake carries the user name. To use a JWT on the HTTP API or Arrow Flight SQL, create a JWT [security integration](/documents/4.2/administration/user_privs/authentication/security_integration.md) instead, and see [Where each authentication method works](/documents/4.2/administration/user_privs/authentication/authentication.md#where-each-authentication-method-works).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.