Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(rest api): Add authentication method in REST API #2705

Open
wants to merge 2 commits into
base: release-5.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 72 additions & 26 deletions en_US/admin/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,41 @@ The section introduces how to work with EMQX REST API.

EMQX has version control on the REST API, all API paths from EMQX 5.0.0 start with `/api/v5`.

## HTTP Headers

Most API requests require the `Accept` header to be set to `application/json`, and then the response will be returned in JSON format unless otherwise specified.

## HTTP Response Status Code

EMQX follows the [HTTP Response Status Code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) standard. The possible status codes are as follows:

| Codes | Description |
| ----- | ------------------------------------------------------------ |
| 200 | Request successfully, and the returned JSON data will provide more details |
| 201 | Created successfully, and the new object will be returned in the Body |
| 204 | Request successfully. Usually used for delete and update operations, and the returned Body will be empty |
| 400 | Bad Request. Usually request body or parameter error |
| 401 | Unauthorized. API key expires or does not exist. |
| 403 | Forbidden. Check if the object is in use or has dependency constraints. |
| 404 | Not Found. You can refer to the `message` field in the Body to check the reason |
| 409 | Conflict. The object already exists or the number limit is exceeded |
| 500 | Internal Server Error. Check the reason in the Body and logs |

## Authentication

EMQX's REST API uses [HTTP Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#the_general_http_authentication_framework) with API keys as authentication credentials. Before using the EMQX REST API, you need to create an API key.
EMQX's REST API supports two main methods for authentication: basic Authentication using API keys and bearer token authentication.

### Basic Authentication Using API Keys

In this method, you use API keys and secret keys as the username and password to authenticate your API requests. EMQX's REST API follows [HTTP Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#the_general_http_authentication_framework), where these credentials are required. Before using the EMQX REST API, you must create an API key.

::: tip Note

For security reasons, starting from EMQX 5.0.0, you cannot use Dashboard user credentials to authenticate REST API requests. Instead, you need to create and use API keys for authentication. Note that role-based API credentials are available only in the EMQX Enterprise edition.

:::tip
For security reasons, from EMQX 5.0.0 onwards, Dashboard users cannot be used for REST API authentication. Role-based API credentials are a feature of the EMQX Enterprise edition.
:::

### Create API Keys
#### Create API Keys

You can manually create API keys for authentication on the Dashboard by navigating to **System** -> **API Key**. For instructions, see [System - API Keys](../dashboard/system.md#api-keys).

Expand All @@ -32,9 +58,9 @@ api_key = {

In the specified file, add multiple API keys in the format `{API Key}:{Secret Key}:{?Role}`, separated by new lines:

- API Key: Any string as the key identifier.
- Secret Key: Use a random string as the secret key.
- Role (optional): Specify the key's [role](#roles-and-permissions), applicable only in the Enterprise edition.
- **API Key**: Any string as the key identifier.
- **Secret Key**: Use a random string as the secret key.
- **Role (optional)**: Specify the key's [role](#roles-and-permissions), applicable only in the Enterprise edition.

For example:

Expand All @@ -48,17 +74,19 @@ API keys created this way are valid indefinitely.

Each time EMQX starts, it will add the data set in the file to the API key list. If an API key already exists, its Secret Key and Role will be updated.

### Roles and Permissions
#### Roles and Permissions (Enterprise Edition)

In the EMQX Enterprise edition, the REST API implements role-based access control. When creating an API key, you can assign one of the following 3 predefined roles:
In the EMQX Enterprise edition, the REST API implements role-based access control. When creating an API key, you can assign one of the following three predefined roles:

- **Administrator**: This role can access all resources and is the default value if no role is specified. The corresponding role identifier is `administrator`.
- **Viewer**: This role can only view resources and data, corresponding to all GET requests in the REST API. The corresponding role identifier is `viewer`.
- **Publisher**: Designed specifically for MQTT message publishing, this role is limited to accessing APIs related to message publishing. The corresponding role identifier is `publisher`.

### Authentication Method
#### Authentication Method Using API Keys

Once you have your API key and secret key, you can use them to authenticate your requests. The API key is used as the username and the secret key as the password for Basic Authentication.

You can use the generated API Key and Secret Key as the username and password for Basic authentication:
Examples in different languages:

:::: tabs type:card
:::tab cURL
Expand Down Expand Up @@ -203,25 +231,43 @@ axios
:::
::::

## HTTP Headers
### Authentication Using Bearer Token

Unless otherwise specified, most API requests require the `Accept` header to be set to `application/json`, and then the response will be returned in JSON format.
As an alternative to API key-based authentication, you can use bearer tokens for secure and programmatic access to the EMQX REST API. To obtain a bearer token, send a request to the login API endpoint as described below.

## HTTP Response Status Code
#### Obtain a Bearer Token

EMQX follows the [HTTP Response Status Code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) standard, and the possible status codes are as follows:
To request a bearer token, make an HTTP `POST` request to the following login API endpoint:

| Codes | Description |
| ----- | ------------------------------------------------------------ |
| 200 | Request successfully, and the returned JSON data will provide more details |
| 201 | Created successfully, and the new object will be returned in the Body |
| 204 | Request successfully. Usually used for delete and update operations, and the returned Body will be empty |
| 400 | Bad Request. Usually request body or parameter error |
| 401 | Unauthorized. API key expires or does not exist. |
| 403 | Forbidden. Check if the object is in use or has dependency constraints. |
| 404 | Not Found. You can refer to the `message` field in the Body to check the reason |
| 409 | Conflict. The object already exists or the number limit is exceeded |
| 500 | Internal Server Error. Check the reason in the Body and logs |
```bash
POST http://your-emqx-address:8483/api/v5/login
```

**Headers:**

- `Content-Type: application/json`

**Request Body:**

```json
{
"username": "admin",
"password": "yourpassword"
}
```

- Replace `your-emqx-address` with the address or IP of your EMQX node.
- Replace `"admin"` and `"yourpassword"` with your EMQX Dashboard credentials.

The response will include the bearer token, which you can use to authenticate API requests.

#### Use Bearer Token for Authentication

Once you have the bearer token, include it in the `Authorization` header of your API requests, like this:

```bash
--header "Authorization: Bearer <your-token>"
```

## Pagination

Expand Down
2 changes: 1 addition & 1 deletion en_US/dashboard/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ On the API Keys page, you can generate an API key and secret key for accessing t

1. Click the **+ Create** button on the top right corner of the page to bring up the Create API Key pop-up dialog.

2. On the Create API Key dialog, configure in the detailed information for the API key.
2. On the Create API Key dialog, configure the detailed information for the API key.

- The API key will never expire if the Expire At box is left empty.
- Select a role for API key (optional), applicable only in the Enterprise edition. For more information about roles, see [Roles and Permissions](../admin/api.md#roles-and-permissions).
Expand Down
91 changes: 66 additions & 25 deletions zh_CN/admin/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@ EMQX 服务启动后,您可以访问 [http://localhost:18083/api-docs/index.ht

EMQX 在 REST API 上做了版本控制,EMQX 5.0.0 以后的所有 API 调用均以 `/api/v5` 开头。

## HTTP 请求头

除非有特殊说明,绝大多数 API 要求请求头中 `Accept` 值设置为 `application/json`,响应内容将以 JSON 格式返回。

## HTTP 响应状态码

EMQX 遵循 [HTTP 响应状态码](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)标准,可能的状态码如下:

| 状态码 | 描述 |
| ------ | ------------------------------------------------------------ |
| 200 | 请求成功,返回的 JSON 数据将提供更多信息 |
| 201 | 创建成功,新建的对象将在 Body 中返回 |
| 204 | 请求成功,常用于删除与更新操作,Body 不会返回内容 |
| 400 | 请求无效,例如请求体或参数错误 |
| 401 | 未通过服务端认证,API 密钥过期或不存在时可能会发生 |
| 403 | 无权操作,检查操作对象是否正在使用或有依赖约束 |
| 404 | 找不到请求路径或请求的对象不存在,可参照 Body 中的 `message` 字段判断具体原因 |
| 409 | 请求的资源已存在或数量超过限制 |
| 500 | 服务端处理请求时发生内部错误,可通过 Body 返回内容与日志判断具体原因 |

## 认证

EMQX 的 REST API 使用 [HTTP Basic 认证](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Authentication#%E9%80%9A%E7%94%A8%E7%9A%84_http_%E8%AE%A4%E8%AF%81%E6%A1%86%E6%9E%B6) 携带 API 密钥作为认证凭据。在开始使用 EMQX REST API 之前,您需要创建 API 密钥。
EMQX 的 REST API 支持两种主要的认证方法:使用 API 密钥的基本认证和 Bearer Token 认证。

### 使用 API 密钥的基本认证

::: tip
出于安全考虑,从 EMQX 5.0.0 开始 Dashboard 用户无法用于 REST API 认证。
API 凭证区分角色是 EMQX 企业版中的功能。
在这种方法中,您通过使用 API 密钥和密钥作为用户名和密码来对 API 请求进行身份验证。EMQX 的 REST API 基于 HTTP 基本认证框架,要求提供这些凭据。使用 EMQX REST API 之前,您需要先创建一个 API 密钥。

::: tip 注意
出于安全考虑,从 EMQX 5.0.0 开始 Dashboard 用户凭据无法用于 REST API 认证。您需要创建并使用 API 密钥进行认证。请注意,基于角色的 API 凭据仅适用于 EMQX 企业版。
:::

### 创建 API 密钥
#### 创建 API 密钥

您可以在 Dashboard **系统设置** -> **API 密钥** 页面中手动创建用于认证的 API 密钥,详细操作请参考 [Dashboard - API 密钥](../dashboard/system.md#api-密钥)。

Expand All @@ -33,9 +56,9 @@ api_key = {

在指定的文件中通过多行分割的 `{API Key}:{Secret Key}:{?Role}` 的格式添加多个 API 密钥:

- API Key:任意字符串作为密钥标识。
- Secret Key:使用随机字符串作为密钥。
- Role (可选):指定密钥的[角色](#角色与权限),仅适用于企业版。
- **API Key**:任意字符串作为密钥标识。
- **Secret Key**:使用随机字符串作为密钥。
- **Role (可选)**:指定密钥的[角色](#角色与权限),仅适用于企业版。

例如:

Expand All @@ -49,15 +72,15 @@ foo:3CA92E5F-30AB-41F5-B3E6-8D7E213BE97E:publisher

每次 EMQX 启动时,会将文件中设置的数据将添加到 API 密钥列表中,如果存在相同的 API Key,则将更新其 Secret Key 与 Role。

### 角色与权限
#### 角色与权限

在 EMQX 企业版中,REST API 实现了基于角色的访问控制,API 密钥创建时,可以分配以下3个预定义的角色:

- **管理员**:此角色可以访问所有资源,未指定角色时默认使用此值。对应的角色标识为 `administrator`。
- **查看者**:此角色只能查看资源和数据,对应于 REST API 中的所有 GET 请求。对应的角色标识为 `viewer`。
- **发布者**:专门为 MQTT 消息发布定制,此角色仅限于访问与消息发布相关的 API。对应的角色标识为 `publisher`。

### 认证方式
#### 认证方式

使用生成的 API Key 以及 Secret Key 分别作为 Basic 认证的用户名与密码,请求示例如下:

Expand Down Expand Up @@ -204,25 +227,43 @@ axios
:::
::::

## HTTP 请求头
### 使用 Bearer Token 认证

除非有特殊说明,绝大多数 API 要求请求头中 `Accept` 值设置为 `application/json`,响应内容将以 JSON 格式返回
除了基于 API 密钥的身份验证外,您还可以使用 Bearer Token 来实现对 EMQX REST API 的安全和程序化访问。要获取 Bearer Token,请按照以下说明向登录 API 端点发送请求

## HTTP 响应状态码
#### 获取 Bearer Token

EMQX 遵循 [HTTP 响应状态码](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)标准,可能的状态码如下
要请求 Bearer Token,请向以下登录 API 端点发送 HTTP `POST ` 请求

| 状态码 | 描述 |
| ------ | ----------------------------------------------------------------------------- |
| 200 | 请求成功,返回的 JSON 数据将提供更多信息 |
| 201 | 创建成功,新建的对象将在 Body 中返回 |
| 204 | 请求成功,常用于删除与更新操作,Body 不会返回内容 |
| 400 | 请求无效,例如请求体或参数错误 |
| 401 | 未通过服务端认证,API 密钥过期或不存在时可能会发生 |
| 403 | 无权操作,检查操作对象是否正在使用或有依赖约束 |
| 404 | 找不到请求路径或请求的对象不存在,可参照 Body 中的 `message` 字段判断具体原因 |
| 409 | 请求的资源已存在或数量超过限制 |
| 500 | 服务端处理请求时发生内部错误,可通过 Body 返回内容与日志判断具体原因 |
```bash
POST http://your-emqx-address:8483/api/v5/login
```

**请求头:**

- `Content-Type: application/json`

**请求体:**

```json
{
"username": "admin",
"password": "yourpassword"
}
```

- 将 `your-emqx-address` 替换为您的 EMQX 节点的地址或 IP。
- 将 `"admin"` 和 `"yourpassword"` 替换为您的 EMQX Dashboard 凭证。

响应中将包含 Bearer Token,您可以使用该 Token 对 API 请求进行身份验证。

#### 使用 Bearer Token 进行身份认证

获取 Bearer Token 后,将其包含在您的 API 请求的 `Authorization` 标头中,如下所示:

```bash
--header "Authorization: Bearer <your-token>"
```

## 分页

Expand Down
2 changes: 1 addition & 1 deletion zh_CN/data-integration/data-bridge-mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MySQL 数据集成是 EMQX 企业版功能。

:::

[MySQL](https://www.mysql.com/) 是一个广泛使用的关系数据库,具备高度的可靠性和稳定性,能够快速安装和配置使用。MySQL Sink 能够将 MQTT 消息高效地存储至 MySQL 数据库中,同时也支持通过事件触发实时更新或删除 MySQL 中的数据。借助 MySQl 数据集成,用户能够轻松实现消息存储、设备在线状态更新以及设备行为记录等功能,实现灵活的物联网数据存储与设备管理功能。
[MySQL](https://www.mysql.com/) 是一个广泛使用的关系数据库,具备高度的可靠性和稳定性,能够快速安装和配置使用。MySQL Sink 能够将 MQTT 消息高效地存储至 MySQL 数据库中,同时也支持通过事件触发实时更新或删除 MySQL 中的数据。借助 MySQL 数据集成,用户能够轻松实现消息存储、设备在线状态更新以及设备行为记录等功能,实现灵活的物联网数据存储与设备管理功能。

本页详细介绍了 EMQX 与 MySQL 的数据集成并提供了实用的规则和 Sink 创建指导。

Expand Down